-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavbar.test.ts
More file actions
82 lines (63 loc) · 1.9 KB
/
Copy pathnavbar.test.ts
File metadata and controls
82 lines (63 loc) · 1.9 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
import { render, screen } from '@testing-library/svelte';
import { readable } from 'svelte/store';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import Navbar from './navbar.svelte';
vi.mock('theme-change');
// Mock $app/stores to control the current page URL in tests
vi.mock('$app/stores', async () => {
return {
page: readable({ url: new URL('http://localhost/') })
};
});
vi.mock('$app/paths', () => ({
resolve: (path: string) => path
}));
vi.mock('$lib/features/awesome-privacy/components/search.svelte');
describe('Navbar component', () => {
// mock localStorage
beforeEach(() => {
globalThis.localStorage = (function () {
let store: Record<string, string> = {};
return {
getItem: (k: string) => (k in store ? store[k] : null),
setItem: (k: string, v: string) => {
store[k] = String(v);
},
removeItem: (k: string) => {
delete store[k];
},
clear: () => {
store = {};
},
length: 0,
key: (index: number) => Object.keys(store)[index] ?? null
} as Storage;
})();
});
describe('URLs', () => {
it('Privacy News link has correct href', () => {
render(Navbar);
const links = screen.getAllByRole('link', { name: /Privacy News/i });
expect(links).toHaveLength(2);
links.forEach((link) => {
expect(link.getAttribute('href')).toBe('/privacy-news');
});
});
it('Awesome Privacy link has correct href', () => {
render(Navbar);
const links = screen.getAllByRole('link', { name: /Awesome Privacy/i });
expect(links).toHaveLength(2);
links.forEach((link) => {
expect(link.getAttribute('href')).toBe('/awesome-privacy');
});
});
it('Websites link has correct href', () => {
render(Navbar);
const links = screen.getAllByRole('link', { name: /Websites/i });
expect(links).toHaveLength(2);
links.forEach((link) => {
expect(link.getAttribute('href')).toBe('/websites');
});
});
});
});