Skip to content

Commit dae1fc4

Browse files
author
Nikos Katsikanis
committed
unit test for theme switcher
1 parent 38089f3 commit dae1fc4

File tree

5 files changed

+74
-184
lines changed

5 files changed

+74
-184
lines changed

js/components/2d-road.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

js/components/3d-road.js

Lines changed: 0 additions & 125 deletions
This file was deleted.

js/components/theme-switcher.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
1-
// stored in /components/theme-switcher.js
21
export default (hostComponent) => {
32
const themes = ['green', 'red', 'blue', 'grey']; // added 'grey'
43

54
const themeButtons = themes
65
.map(
7-
(theme) => `
8-
<button class="secondary squarify" data-theme="${theme}">
9-
${theme.charAt(0).toUpperCase() + theme.slice(1)}
10-
</button>
11-
`,
6+
(theme) =>
7+
`<button class="secondary squarify" data-theme="${theme}">${theme.charAt(0).toUpperCase() + theme.slice(1)}</button>`,
128
)
139
.join('');
1410

1511
hostComponent.innerHTML = `
1612
<div class="flex gap-md align-center">
17-
<span>Theme</span> ${themeButtons}
13+
<span>Theme</span>${themeButtons}
1814
</div>
19-
2015
`;
2116

2217
themes.forEach((theme) => {

js/components/theme-switcher.test.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { describe, it, expect, beforeEach, vi } from 'vitest';
2+
import themeSwitcher from './theme-switcher';
3+
4+
// Mocking localStorage and document.documentElement for the test environment
5+
const mockLocalStorage = (() => {
6+
let store = {};
7+
return {
8+
getItem: (key) => store[key] || null,
9+
setItem: (key, value) => (store[key] = value),
10+
clear: () => (store = {}),
11+
};
12+
})();
13+
14+
beforeEach(() => {
15+
vi.stubGlobal('localStorage', mockLocalStorage);
16+
document.documentElement.className = ''; // Reset class list on document element
17+
});
18+
19+
describe('themeSwitcher', () => {
20+
it('renders theme buttons correctly', () => {
21+
const hostComponent = document.createElement('div');
22+
themeSwitcher(hostComponent);
23+
24+
// Check that each theme button is rendered
25+
const buttons = hostComponent.querySelectorAll('button[data-theme]');
26+
expect(buttons.length).toBe(4); // green, red, blue, grey
27+
28+
const themes = ['Green', 'Red', 'Blue', 'Grey'];
29+
themes.forEach((theme, index) => {
30+
expect(buttons[index].textContent).toBe(theme);
31+
});
32+
});
33+
34+
it('applies the theme class to document.documentElement on button click', () => {
35+
const hostComponent = document.createElement('div');
36+
themeSwitcher(hostComponent);
37+
38+
// Simulate clicking the 'red' theme button
39+
const redButton = hostComponent.querySelector('button[data-theme="red"]');
40+
redButton.click();
41+
42+
expect(document.documentElement.classList.contains('red-theme')).toBe(true);
43+
44+
// Ensure other themes are not active
45+
expect(document.documentElement.classList.contains('green-theme')).toBe(false);
46+
expect(document.documentElement.classList.contains('blue-theme')).toBe(false);
47+
expect(document.documentElement.classList.contains('grey-theme')).toBe(false);
48+
});
49+
50+
it('stores the selected theme in localStorage on button click', () => {
51+
const hostComponent = document.createElement('div');
52+
themeSwitcher(hostComponent);
53+
54+
// Click the 'blue' theme button
55+
const blueButton = hostComponent.querySelector('button[data-theme="blue"]');
56+
blueButton.click();
57+
58+
expect(localStorage.getItem('theme')).toBe('blue');
59+
});
60+
61+
it('loads and applies the saved theme from localStorage on initialization', () => {
62+
localStorage.setItem('theme', 'grey'); // Simulate a previously selected theme
63+
64+
const hostComponent = document.createElement('div');
65+
themeSwitcher(hostComponent);
66+
67+
// Ensure the saved theme is applied on load
68+
expect(document.documentElement.classList.contains('grey-theme')).toBe(true);
69+
});
70+
});

vitest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default defineConfig({
1010
coverage: {
1111
reporter: ['text-summary'], // Only display the summary report
1212
provider: 'v8', // Use the v8 provider for coverage
13-
exclude: ['node_modules/'],
13+
exclude: ['node_modules/','examples/'], // Exclude node_modules and Vue project from coverage
1414
},
1515
},
1616
});

0 commit comments

Comments
 (0)