|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +import { getStoredTheme, setStoredTheme } from './theme-storage'; |
| 6 | + |
| 7 | +describe('theme-storage', () => { |
| 8 | + beforeEach(() => { |
| 9 | + localStorage.clear(); |
| 10 | + }); |
| 11 | + |
| 12 | + it('returns light when localStorage is unavailable', () => { |
| 13 | + jest.isolateModules(() => { |
| 14 | + jest.doMock('./storage', () => ({ |
| 15 | + __esModule: true, |
| 16 | + default: { |
| 17 | + factory: () => ({ |
| 18 | + get: () => { |
| 19 | + throw new Error('localStorage is disabled'); |
| 20 | + }, |
| 21 | + set: jest.fn(), |
| 22 | + }), |
| 23 | + }, |
| 24 | + })); |
| 25 | + |
| 26 | + const { getStoredTheme } = require('./theme-storage'); |
| 27 | + expect(getStoredTheme()).toBe('light'); |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it('does not throw on setStoredTheme when localStorage is unavailable', () => { |
| 32 | + jest.isolateModules(() => { |
| 33 | + jest.doMock('./storage', () => ({ |
| 34 | + __esModule: true, |
| 35 | + default: { |
| 36 | + factory: () => ({ |
| 37 | + get: jest.fn(), |
| 38 | + set: () => { |
| 39 | + throw new Error('localStorage is disabled'); |
| 40 | + }, |
| 41 | + }), |
| 42 | + }, |
| 43 | + })); |
| 44 | + |
| 45 | + const { setStoredTheme } = require('./theme-storage'); |
| 46 | + expect(() => setStoredTheme('dark')).not.toThrow(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('round-trips a stored theme', () => { |
| 51 | + setStoredTheme('dark'); |
| 52 | + expect(getStoredTheme()).toBe('dark'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('returns light for unknown stored values', () => { |
| 56 | + expect(getStoredTheme()).toBe('light'); |
| 57 | + }); |
| 58 | +}); |
0 commit comments