-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.test.js
More file actions
103 lines (90 loc) · 3.81 KB
/
Copy pathapp.test.js
File metadata and controls
103 lines (90 loc) · 3.81 KB
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
import { beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
import { state } from './shared/state.js';
const showProjects = vi.fn();
const showWorkspace = vi.fn();
const loadSession = vi.fn();
const showSearchPage = vi.fn();
vi.mock('./projects.js', () => ({ showProjects }));
vi.mock('./sessions.js', () => ({ showWorkspace, loadSession, selectSession: vi.fn(), copyAll: vi.fn() }));
vi.mock('./search.js', () => ({ showSearchPage, doSearch: vi.fn() }));
vi.mock('./export.js', () => ({ bulkExport: vi.fn(), downloadSession: vi.fn() }));
vi.mock('./shared/theme.js', () => ({
HLJS_THEME_SHEETS: {},
applyHljsTheme: vi.fn(),
applyTheme: vi.fn(),
toggleTheme: vi.fn(),
setWorkspaceMode: vi.fn(),
}));
describe('router (app.js)', () => {
beforeAll(async () => {
window.scrollTo = vi.fn();
Element.prototype.scrollIntoView = vi.fn();
await import('./app.js');
document.dispatchEvent(new Event('DOMContentLoaded'));
});
beforeEach(() => {
document.body.innerHTML = '<div id="content"></div><span id="footer-year"></span>';
state.currentProject = null;
state.cachedSessions = [];
state.navInProgress = false;
showProjects.mockClear();
showWorkspace.mockClear();
loadSession.mockClear();
showSearchPage.mockClear();
window.location.hash = '';
localStorage.clear();
});
function routeTo(hash) {
window.location.hash = hash;
window.dispatchEvent(new HashChangeEvent('hashchange'));
}
it('dispatches default hash to showProjects', () => {
routeTo('');
expect(showProjects).toHaveBeenCalled();
});
it('dispatches #search to showSearchPage on hashchange', () => {
showProjects.mockClear();
routeTo('#search');
expect(showSearchPage).toHaveBeenCalledTimes(1);
expect(showProjects).not.toHaveBeenCalled();
});
it('dispatches #project/<name> to showWorkspace', () => {
showProjects.mockClear();
routeTo('#project/my-project');
expect(showWorkspace).toHaveBeenCalledWith('my-project');
});
it('dispatches #project/<name>/<sessionId> to showWorkspace when cache is cold', () => {
routeTo('#project/my-project/sess-abc');
expect(showWorkspace).toHaveBeenCalledWith('my-project', 'sess-abc');
expect(loadSession).not.toHaveBeenCalled();
});
it('loads session from cache when project matches and sidebar exists', () => {
state.currentProject = 'my-project';
state.cachedSessions = [{ id: 'sess-abc' }];
document.body.innerHTML += '<div id="sidebar"><button class="sidebar-item" id="sidebar-sess-abc"></button></div>';
routeTo('#project/my-project/sess-abc');
expect(loadSession).toHaveBeenCalledWith('my-project', 'sess-abc');
expect(showWorkspace).not.toHaveBeenCalled();
expect(document.getElementById('sidebar-sess-abc').classList.contains('active')).toBe(true);
});
it('falls back to showProjects when project name is a malformed URI', () => {
showProjects.mockClear();
routeTo('#project/%E0%A4%A');
expect(showProjects).toHaveBeenCalled();
expect(showWorkspace).not.toHaveBeenCalled();
});
it('falls back to showProjects when session project segment is malformed', () => {
showProjects.mockClear();
routeTo('#project/%E0%A4%A/sess-1');
expect(showProjects).toHaveBeenCalled();
expect(showWorkspace).not.toHaveBeenCalled();
});
it('re-runs routing when hashchange fires', () => {
showProjects.mockClear();
routeTo('#search');
expect(showSearchPage).toHaveBeenCalledTimes(1);
showSearchPage.mockClear();
routeTo('#project/other');
expect(showWorkspace).toHaveBeenCalledWith('other');
});
});