test: add missing edge case test for theme switcher missing previous theme class
Co-authored-by: KarelZe <12801659+KarelZe@users.noreply.github.com>
2 files modified
1 files added
| | |
| | | "prepare": "husky", |
| | | "release": "release-it", |
| | | "setup": "npm install && npm run prepare", |
| | | "test": "" |
| | | "test": "jest" |
| | | }, |
| | | "repository": { |
| | | "type": "git", |
| | |
| | | "@release-it/conventional-changelog": "^11.0.1", |
| | | "conventional-commits-filter": "^6.0.1", |
| | | "husky": "^9.1.7", |
| | | "jest": "^30.5.2", |
| | | "jest-environment-jsdom": "^30.5.2", |
| | | "prettier": "^3.8.1", |
| | | "prettier-plugin-go-template": "^0.0.15", |
| | | "release-it": "^20.2.1", |
| New file |
| | |
| | | /** |
| | | * @jest-environment jsdom |
| | | */ |
| | | |
| | | const fs = require('fs'); |
| | | const path = require('path'); |
| | | |
| | | describe('anatole-theme-switcher', () => { |
| | | beforeAll(() => { |
| | | const scriptPath = path.resolve(__dirname, '../assets/js/anatole-theme-switcher.js'); |
| | | const scriptContent = fs.readFileSync(scriptPath, 'utf8'); |
| | | |
| | | // Mock matchMedia |
| | | window.matchMedia = jest.fn().mockImplementation(query => ({ |
| | | matches: false, |
| | | media: query, |
| | | onchange: null, |
| | | addListener: jest.fn(), |
| | | removeListener: jest.fn(), |
| | | addEventListener: jest.fn(), |
| | | removeEventListener: jest.fn(), |
| | | dispatchEvent: jest.fn(), |
| | | })); |
| | | |
| | | // Evaluate the script and expose internal functions to window for testing |
| | | eval(scriptContent + '\nwindow.setThemeStyle = setThemeStyle;'); |
| | | }); |
| | | |
| | | beforeEach(() => { |
| | | document.documentElement.className = ''; |
| | | localStorage.clear(); |
| | | }); |
| | | |
| | | test('handles missing previous theme class correctly', () => { |
| | | document.documentElement.className = 'non-theme-class'; |
| | | window.setThemeStyle('dark'); |
| | | |
| | | // The new theme should be added |
| | | expect(document.documentElement.classList.contains('theme--dark')).toBe(true); |
| | | // Other classes should remain |
| | | expect(document.documentElement.classList.contains('non-theme-class')).toBe(true); |
| | | }); |
| | | }); |