|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +// @ts-nocheck |
| 4 | +var react_1 = require("@testing-library/react"); |
| 5 | +var client_1 = require("../src/client"); |
| 6 | +var device_mock_1 = require("./mocks/device.mock"); |
| 7 | +var storage_adapter_1 = require("../src/adapter/storage.adapter"); |
| 8 | +var ThemeAutoToggle_1 = require("./assets/ThemeAutoToggle"); |
| 9 | +var ThemeManualToggle_1 = require("./assets/ThemeManualToggle"); |
| 10 | +var ThemeSwitcher_1 = require("./assets/ThemeSwitcher"); |
| 11 | +var env_helper_1 = require("../src/helper/env.helper"); |
| 12 | +beforeAll(function () { |
| 13 | + (0, device_mock_1.mockLocalStorage)(); |
| 14 | + (0, device_mock_1.mockMatchMedia)(); |
| 15 | +}); |
| 16 | +beforeEach(function () { |
| 17 | + (0, storage_adapter_1.clear)(); |
| 18 | + document.documentElement.style.colorScheme = ''; |
| 19 | + document.documentElement.removeAttribute('class'); |
| 20 | +}); |
| 21 | +describe('ThemeProvider', function () { |
| 22 | + test('should set storage key according to the specified value', function () { |
| 23 | + var storageKey = 'theme-test'; |
| 24 | + var expectedTheme = 'light'; |
| 25 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={expectedTheme}> |
| 26 | + <ThemeAutoToggle_1.default /> |
| 27 | + </client_1.ThemeProvider>); |
| 28 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(expectedTheme); |
| 29 | + }); |
| 30 | + test('should use default storage key when none is specified value', function () { |
| 31 | + var expectedThemeType = 'light'; |
| 32 | + (0, react_1.render)(<client_1.ThemeProvider defaultTheme={expectedThemeType}> |
| 33 | + <ThemeAutoToggle_1.default /> |
| 34 | + </client_1.ThemeProvider>); |
| 35 | + expect((0, storage_adapter_1.read)(env_helper_1.DEFAULT_STORAGE_KEY)).toEqual(expectedThemeType); |
| 36 | + }); |
| 37 | + test.each(['light', 'dark'])('should use the `defaultTheme` when nothing is stored in `localStorage`', function (theme) { |
| 38 | + var storageKey = 'test'; |
| 39 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={theme}> |
| 40 | + <ThemeAutoToggle_1.default /> |
| 41 | + </client_1.ThemeProvider>); |
| 42 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(theme); |
| 43 | + expect(document.documentElement.classList[0]).toBe(theme); |
| 44 | + expect(document.documentElement.style.colorScheme).toBe(theme); |
| 45 | + }); |
| 46 | + test.each(['light', 'dark'])('should auto-determine theme color when nothing is stored in `localStorage` and `defaultTheme` is set to "auto"', function (color) { |
| 47 | + var storageKey = 'test'; |
| 48 | + (0, device_mock_1.mockPreferredColorScheme)(color); |
| 49 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme="auto"> |
| 50 | + <ThemeAutoToggle_1.default /> |
| 51 | + </client_1.ThemeProvider>); |
| 52 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual('auto'); |
| 53 | + expect(document.documentElement.classList[0]).toBe(color); |
| 54 | + expect(document.documentElement.style.colorScheme).toBe(color); |
| 55 | + }); |
| 56 | + test.each(['light', 'dark'])('should set `color-scheme` and `class` to "%s" theme color according to saved theme preference', function (color) { |
| 57 | + var storageKey = 'test'; |
| 58 | + (0, storage_adapter_1.write)(storageKey, color); |
| 59 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey}> |
| 60 | + <ThemeAutoToggle_1.default /> |
| 61 | + </client_1.ThemeProvider>); |
| 62 | + expect(document.documentElement.classList[0]).toBe(color); |
| 63 | + expect(document.documentElement.style.colorScheme).toBe(color); |
| 64 | + }); |
| 65 | + test.each(['light', 'dark', 'auto'])('should use system resolved "%s" color and "auto" theme when no `defaultTheme` is provided and nothing is stored in `localStorage`', function (color) { |
| 66 | + var storageKey = 'sys-resolved-theme'; |
| 67 | + var prefColor = color === 'auto' ? 'dark' : color; |
| 68 | + (0, device_mock_1.mockPreferredColorScheme)(prefColor); |
| 69 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey}> |
| 70 | + <ThemeAutoToggle_1.default /> |
| 71 | + </client_1.ThemeProvider>); |
| 72 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual('auto'); |
| 73 | + expect(document.documentElement.classList[0]).toBe(prefColor); |
| 74 | + expect(document.documentElement.style.colorScheme).toBe(prefColor); |
| 75 | + }); |
| 76 | + test.each(['light', 'dark'])('should set theme color automatically based on user system preference', function (sysPrefColor) { |
| 77 | + var storageKey = 'sys-resolved-theme'; |
| 78 | + (0, device_mock_1.mockPreferredColorScheme)(sysPrefColor); |
| 79 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme="auto"> |
| 80 | + <ThemeAutoToggle_1.default /> |
| 81 | + </client_1.ThemeProvider>); |
| 82 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual('auto'); |
| 83 | + expect(document.documentElement.classList[0]).toBe(sysPrefColor); |
| 84 | + expect(document.documentElement.style.colorScheme).toBe(sysPrefColor); |
| 85 | + }); |
| 86 | + test.each([ |
| 87 | + ['light', 'dark'], |
| 88 | + ['dark', 'light'], |
| 89 | + ])('should ignore nested `ThemeProvider`', function (expectedTheme, nestedTheme) { |
| 90 | + var storageKey = 'test'; |
| 91 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={expectedTheme}> |
| 92 | + <client_1.ThemeProvider storageKey={storageKey} defaultTheme={nestedTheme}> |
| 93 | + <ThemeAutoToggle_1.default /> |
| 94 | + </client_1.ThemeProvider> |
| 95 | + </client_1.ThemeProvider>); |
| 96 | + expect(document.documentElement.classList[0]).toBe(expectedTheme); |
| 97 | + }); |
| 98 | + test.each([ |
| 99 | + ['light', 'dark'], |
| 100 | + ['dark', 'light'], |
| 101 | + ])('should update value in storage when toggling from "%s" to "%s" theme', function (themeFrom, themeTo) { |
| 102 | + var storageKey = 'test'; |
| 103 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={themeFrom}> |
| 104 | + <ThemeAutoToggle_1.default /> |
| 105 | + </client_1.ThemeProvider>); |
| 106 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(themeFrom); |
| 107 | + react_1.fireEvent.click(react_1.screen.getByText(/toggle theme/i)); |
| 108 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(themeTo); |
| 109 | + }); |
| 110 | + test.each([ |
| 111 | + ['light', 'dark'], |
| 112 | + ['dark', 'light'], |
| 113 | + ])('should update value in storage when manually setting theme from "%s" to "%s"', function (themeFrom, themeTo) { |
| 114 | + var storageKey = 'test'; |
| 115 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={themeFrom}> |
| 116 | + <ThemeManualToggle_1.default /> |
| 117 | + </client_1.ThemeProvider>); |
| 118 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(themeFrom); |
| 119 | + react_1.fireEvent.click(react_1.screen.getByText(/toggle theme/i)); |
| 120 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(themeTo); |
| 121 | + }); |
| 122 | + test.each(['light', 'dark'])('should switch from "auto" to "%s"', function (theme) { |
| 123 | + var storageKey = 'sys-resolved-theme'; |
| 124 | + var oppositeTheme = theme === 'dark' ? 'light' : 'dark'; |
| 125 | + (0, device_mock_1.mockPreferredColorScheme)(oppositeTheme); |
| 126 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme="auto"> |
| 127 | + <ThemeSwitcher_1.default /> |
| 128 | + </client_1.ThemeProvider>); |
| 129 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual('auto'); |
| 130 | + expect(document.documentElement.classList[0]).toBe(oppositeTheme); |
| 131 | + expect(document.documentElement.style.colorScheme).toBe(oppositeTheme); |
| 132 | + react_1.fireEvent.click(react_1.screen.getByText(new RegExp("".concat(theme, " theme"), 'i'))); |
| 133 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(theme); |
| 134 | + expect(document.documentElement.classList[0]).toBe(theme); |
| 135 | + expect(document.documentElement.style.colorScheme).toBe(theme); |
| 136 | + }); |
| 137 | + test.each(['light', 'dark'])('should switch from "%s" to "auto"', function (theme) { |
| 138 | + var storageKey = 'sys-resolved-theme'; |
| 139 | + var oppositeTheme = theme === 'dark' ? 'light' : 'dark'; |
| 140 | + (0, device_mock_1.mockPreferredColorScheme)(oppositeTheme); |
| 141 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme={theme}> |
| 142 | + <ThemeSwitcher_1.default /> |
| 143 | + </client_1.ThemeProvider>); |
| 144 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual(theme); |
| 145 | + expect(document.documentElement.classList[0]).toBe(theme); |
| 146 | + expect(document.documentElement.style.colorScheme).toBe(theme); |
| 147 | + react_1.fireEvent.click(react_1.screen.getByText('Auto Theme')); |
| 148 | + expect((0, storage_adapter_1.read)(storageKey)).toEqual('auto'); |
| 149 | + expect(document.documentElement.classList[0]).toBe(oppositeTheme); |
| 150 | + expect(document.documentElement.style.colorScheme).toBe(oppositeTheme); |
| 151 | + }); |
| 152 | + test('should not set `colorScheme` and class name to "auto"', function () { |
| 153 | + var storageKey = 'sys-resolved-theme'; |
| 154 | + (0, react_1.render)(<client_1.ThemeProvider storageKey={storageKey} defaultTheme="auto"> |
| 155 | + <ThemeSwitcher_1.default /> |
| 156 | + </client_1.ThemeProvider>); |
| 157 | + expect(document.documentElement.classList[0]).not.toBe('auto'); |
| 158 | + expect(document.documentElement.style.colorScheme).not.toBe('auto'); |
| 159 | + }); |
| 160 | +}); |
0 commit comments