aboutsummaryrefslogtreecommitdiffstats
path: root/frontend-vanilla/src/store.test.ts
blob: ccf9a1ded4c33660501e8a332c09b2fa5eeeeb29 (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
import { describe, it, expect, vi } 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 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();
    });

    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();
    });
});