aboutsummaryrefslogtreecommitdiffstats
path: root/frontend-vanilla/src/mobile-overflow.test.ts
blob: 7463573a0fd4b6797348a268bcf5c90b004f52ac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { store } from './store';
import { renderLayout, renderItems } from './main';
import { apiFetch } from './api';

// Mock api
vi.mock('./api', () => ({
    apiFetch: vi.fn()
}));

// Mock IntersectionObserver
class MockIntersectionObserver {
    observe = vi.fn();
    unobserve = vi.fn();
    disconnect = vi.fn();
}
vi.stubGlobal('IntersectionObserver', MockIntersectionObserver);

// Read the main stylesheet once for CSS rule assertions
const cssContent = readFileSync(resolve(__dirname, 'style.css'), 'utf-8');

describe('Mobile horizontal overflow prevention', () => {
    beforeEach(() => {
        document.body.innerHTML = '<div id="app"></div>';
        vi.stubGlobal('location', {
            href: 'http://localhost/v3/',
            pathname: '/v3/',
            search: '',
            assign: vi.fn(),
            replace: vi.fn()
        });
        vi.stubGlobal('history', { pushState: vi.fn() });
        Element.prototype.scrollIntoView = vi.fn();
        vi.clearAllMocks();
        store.setFeeds([]);
        store.setTags([]);
        store.setItems([]);

        vi.mocked(apiFetch).mockResolvedValue({
            ok: true,
            status: 200,
            json: async () => []
        } as Response);
    });

    describe('CSS containment rules', () => {
        it('.item-description should have overflow-x hidden to contain wide RSS content', () => {
            // .item-description must prevent wide child elements (tables, iframes)
            // from causing horizontal viewport overflow
            const itemDescBlock = cssContent.match(
                /\.item-description\s*\{[^}]*\}/g
            );
            expect(itemDescBlock).not.toBeNull();
            const mainBlock = itemDescBlock!.find(
                block => !block.includes('img') && !block.includes('video') && !block.includes('pre') && !block.includes(' a')
            );
            expect(mainBlock).toBeDefined();
            expect(mainBlock).toMatch(/overflow-x:\s*hidden/);
        });

        it('.item-description should constrain tables with max-width', () => {
            // RSS feeds commonly contain <table> elements with explicit widths
            const tableRule = cssContent.match(
                /\.item-description\s+table[^{]*\{[^}]*max-width:\s*100%/
            );
            expect(tableRule).not.toBeNull();
        });

        it('.item-description should constrain iframes with max-width', () => {
            // RSS feeds commonly embed iframes (YouTube, etc.) with fixed widths
            const iframeRule = cssContent.match(
                /\.item-description\s+iframe[^{]*\{[^}]*max-width:\s*100%/
            );
            expect(iframeRule).not.toBeNull();
        });

        it('.main-content should explicitly set overflow-x hidden', () => {
            // .main-content must not allow horizontal scrolling
            const mainContentBlock = cssContent.match(
                /\.main-content\s*\{[^}]*\}/
            );
            expect(mainContentBlock).not.toBeNull();
            expect(mainContentBlock![0]).toMatch(/overflow-x:\s*hidden/);
        });
    });

    describe('Rendered content containment', () => {
        it('should render items with wide table content without breaking layout', () => {
            renderLayout();
            const wideTableItem = {
                _id: 1,
                title: 'Wide Table Post',
                url: 'http://example.com',
                publish_date: '2024-01-01',
                read: false,
                starred: false,
                description: '<table width="2000"><tr><td>Very wide table from RSS</td></tr></table>'
            } as any;
            store.setItems([wideTableItem]);
            renderItems();

            const desc = document.querySelector('.item-description');
            expect(desc).not.toBeNull();
            expect(desc!.innerHTML).toContain('<table');

            // The item-description element should be inside main-content
            // which constrains overflow
            const mainContent = document.getElementById('main-content');
            expect(mainContent).not.toBeNull();
            expect(mainContent!.contains(desc!)).toBe(true);
        });

        it('should render items with wide iframe content without breaking layout', () => {
            renderLayout();
            const wideIframeItem = {
                _id: 2,
                title: 'Embedded Video Post',
                url: 'http://example.com',
                publish_date: '2024-01-01',
                read: false,
                starred: false,
                description: '<iframe width="1200" height="600" src="https://example.com/embed"></iframe>'
            } as any;
            store.setItems([wideIframeItem]);
            renderItems();

            const desc = document.querySelector('.item-description');
            expect(desc).not.toBeNull();
            expect(desc!.innerHTML).toContain('<iframe');
        });

        it('should render items with wide image using inline style without breaking layout', () => {
            renderLayout();
            const wideImgItem = {
                _id: 3,
                title: 'Wide Image Post',
                url: 'http://example.com',
                publish_date: '2024-01-01',
                read: false,
                starred: false,
                description: '<img style="width: 1500px" src="https://example.com/wide.jpg">'
            } as any;
            store.setItems([wideImgItem]);
            renderItems();

            const desc = document.querySelector('.item-description');
            expect(desc).not.toBeNull();
            expect(desc!.innerHTML).toContain('<img');
        });
    });
});