Skip to content

Commit 251e86b

Browse files
committed
refactor: delete local storage theme migration code
1 parent 53cd45a commit 251e86b

File tree

2 files changed

+12
-49
lines changed

2 files changed

+12
-49
lines changed

Diff for: src/App/App.tsx

+8-21
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,16 @@ export const App = () => {
99
const [isReady, setIsReady] = useState(false);
1010
const [isMobile, setIsMobile] = useState(false);
1111

12-
const init = () => {
13-
if (
14-
window.matchMedia(
12+
useEffect(() => {
13+
const init = () => {
14+
const matchesMobileQuery = window.matchMedia(
1515
'(max-device-width: 820px) and (-webkit-min-device-pixel-ratio: 2)',
16-
)?.matches
17-
) {
18-
setIsMobile(true);
19-
}
20-
21-
// before the state refactoring, 'theme' had a boolean-ish ('true', 'false')
22-
// value in localStorage, now 'theme' has a theme value ('dark', 'light'),
23-
// to prevent the site from breaking, older 'theme' entries should be updated
24-
const localStorageTheme = localStorage.getItem('theme');
25-
if (localStorageTheme === 'true') {
26-
localStorage.setItem('theme', 'dark');
27-
} else if (localStorageTheme === 'false') {
28-
localStorage.setItem('theme', 'light');
29-
}
16+
)?.matches;
3017

31-
setIsReady(true);
32-
};
18+
setIsMobile(matchesMobileQuery);
19+
setIsReady(true);
20+
};
3321

34-
useEffect(() => {
3522
if (!isReady) init();
3623
}, [isReady]);
3724

@@ -46,6 +33,6 @@ export const App = () => {
4633
</main>
4734
</AppProvider>
4835
) : (
49-
<></>
36+
<div>Loading...</div>
5037
);
5138
};

Diff for: src/Index.test.tsx

+4-28
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,14 @@ describe('application tests', () => {
145145

146146
// site should default to the dark theme
147147
expect(toggle).toBeChecked();
148-
expect(particles).toHaveStyle({ backgroundColor: '#000' });
148+
expect(particles).toHaveStyle('background-color: #000');
149149

150150
// click the toggle
151151
fireEvent.click(toggle);
152152

153153
// the light theme should be visible
154154
expect(toggle).not.toBeChecked();
155-
expect(particles).toHaveStyle({ backgroundColor: '#fff' });
155+
expect(particles).toHaveStyle('background-color: #fff');
156156
});
157157

158158
it('should render full footer on desktop', () => {
@@ -180,7 +180,6 @@ describe('app context tests', () => {
180180
const footer = screen.getByTestId('footer');
181181

182182
expect(footer).toHaveTextContent(/^Designed and built by Adam Alston$/);
183-
expect(footer).not.toHaveTextContent(/Source/);
184183
});
185184

186185
describe('reducer tests', () => {
@@ -203,29 +202,6 @@ describe('local storage tests', () => {
203202
localStorage.clear();
204203
});
205204

206-
it("should show the dark theme when 'theme' is set to 'true' in local storage", async () => {
207-
// set local storage item and render the app
208-
localStorage.setItem('theme', 'true');
209-
await act(async () => render(<App />));
210-
211-
// check that the local storage item has been updated correctly
212-
expect(localStorage.getItem('theme')).toEqual('dark');
213-
const particles = screen.getByTestId('particles');
214-
expect(particles).toHaveStyle({ backgroundColor: '#000' });
215-
});
216-
217-
it("should show the light theme when 'theme' is set to 'false' in local storage", async () => {
218-
// set local storage item and render the app
219-
localStorage.setItem('theme', 'false');
220-
await act(async () => render(<App />));
221-
222-
// check that the local storage item has been updated correctly
223-
expect(localStorage.getItem('theme')).toEqual('light');
224-
225-
const particles = screen.getByTestId('particles');
226-
expect(particles).toHaveStyle({ backgroundColor: '#fff' });
227-
});
228-
229205
// https://testing-library.com/docs/react-testing-library/api/#rerender
230206
it('should persist the light theme through an app re-render', async () => {
231207
const { rerender } = render(<App />);
@@ -238,7 +214,7 @@ describe('local storage tests', () => {
238214
const particles = screen.getByTestId('particles');
239215

240216
expect(localStorage.getItem('theme')).toEqual('light');
241-
expect(particles).toHaveStyle({ backgroundColor: '#fff' });
217+
expect(particles).toHaveStyle('background-color: #fff');
242218
});
243219

244220
it('should change local storage value when toggle is clicked', async () => {
@@ -251,6 +227,6 @@ describe('local storage tests', () => {
251227
fireEvent.click(toggle);
252228

253229
// check that the local storage item has been changed
254-
expect(localStorage.getItem('theme')).not.toEqual('light');
230+
expect(localStorage.getItem('theme')).toEqual('dark');
255231
});
256232
});

0 commit comments

Comments
 (0)