aboutsummaryrefslogtreecommitdiffstats
path: root/frontend-vanilla/src/store.test.ts
blob: 799db8396e78dcf144f48e9ace41b9c9efaba78c (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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { Store } from './store';

describe('Store', () => {
    it('should store and notify about feeds', () => {
        const store = new Store();
        const mockFeeds = [
            { _id: 1, title: 'Feed 1', url: 'http://1', web_url: 'http://1', category: 'cat' }
        ];

        const callback = vi.fn();
        store.on('feeds-updated', callback);

        store.setFeeds(mockFeeds);

        expect(store.feeds).toEqual(mockFeeds);
        expect(callback).toHaveBeenCalled();
    });

    it('should handle tags', () => {
        const store = new Store();
        const mockTags = [{ title: 'Tag 1' } as any];
        const callback = vi.fn();
        store.on('tags-updated', callback);

        store.setTags(mockTags);
        expect(store.tags).toEqual(mockTags);
        expect(callback).toHaveBeenCalled();

        store.setActiveTag('Tag 1');
        expect(store.activeTagName).toBe('Tag 1');
    });

    it('should handle items and loading state', () => {
        const store = new Store();
        const mockItems = [{ _id: 1, title: 'Item 1' } as any];

        const itemCallback = vi.fn();
        const loadingCallback = vi.fn();

        store.on('items-updated', itemCallback);
        store.on('loading-state-changed', loadingCallback);

        store.setLoading(true);
        expect(store.loading).toBe(true);
        expect(loadingCallback).toHaveBeenCalled();

        store.setItems(mockItems);
        expect(store.items).toEqual(mockItems);
        expect(itemCallback).toHaveBeenCalled();

        // Test append
        const moreItems = [{ _id: 2, title: 'Item 2' } as any];
        store.setItems(moreItems, true);
        expect(store.items).toHaveLength(2);
        expect(store.items[1]._id).toBe(2);
    });

    it('should handle pagination state', () => {
        const store = new Store();
        store.setHasMore(true);
        expect(store.hasMore).toBe(true);
        store.setHasMore(false);
        expect(store.hasMore).toBe(false);
    });

    it('should notify when active feed changes', () => {
        const store = new Store();
        const callback = vi.fn();
        store.on('active-feed-updated', callback);

        store.setActiveFeed(123);
        expect(store.activeFeedId).toBe(123);
        expect(callback).toHaveBeenCalled();
    });

    it('should handle search query', () => {
        const store = new Store();
        const callback = vi.fn();
        store.on('search-updated', callback);

        store.setSearchQuery('test query');
        expect(store.searchQuery).toBe('test query');
        expect(callback).toHaveBeenCalled();
    });

    it('should handle theme changes', () => {
        const store = new Store();
        const callback = vi.fn();
        store.on('theme-updated', callback);

        store.setTheme('dark');
        expect(store.theme).toBe('dark');
        expect(localStorage.getItem('neko-theme')).toBe('dark');
        expect(callback).toHaveBeenCalled();
    });

    it('should handle font theme changes', () => {
        const store = new Store();
        const callback = vi.fn();
        store.on('theme-updated', callback);

        store.setFontTheme('serif');
        expect(store.fontTheme).toBe('serif');
        expect(localStorage.getItem('neko-font-theme')).toBe('serif');
        expect(callback).toHaveBeenCalled();
    });

    describe('sidebar cookie persistence', () => {
        beforeEach(() => {
            // Clear sidebar cookie
            document.cookie = 'neko_sidebar=; path=/; max-age=0';
        });

        it('should persist sidebar state to cookie', () => {
            const store = new Store();
            store.setSidebarVisible(false);
            expect(document.cookie).toContain('neko_sidebar=0');

            store.setSidebarVisible(true);
            expect(document.cookie).toContain('neko_sidebar=1');
        });

        it('should read sidebar state from cookie on init', () => {
            document.cookie = 'neko_sidebar=1; path=/';
            const store = new Store();
            expect(store.sidebarVisible).toBe(true);

            document.cookie = 'neko_sidebar=0; path=/';
            const store2 = new Store();
            expect(store2.sidebarVisible).toBe(false);
        });

        it('should default to closed on tablet/mobile when no cookie', () => {
            // jsdom defaults innerWidth to 0, which is <= 1024
            const store = new Store();
            expect(store.sidebarVisible).toBe(false);
        });

        it('should emit sidebar-toggle when toggling', () => {
            const store = new Store();
            const callback = vi.fn();
            store.on('sidebar-toggle', callback);
            store.toggleSidebar();
            expect(callback).toHaveBeenCalled();
        });
    });
});