Skip to content

Commit 317b0c1

Browse files
committed
test: refactor testing fot app provider, theme core hook, theme variants hook
1 parent b73bc06 commit 317b0c1

File tree

4 files changed

+30
-106
lines changed

4 files changed

+30
-106
lines changed

src/react/AppProvider.test.jsx

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,19 @@ describe('AppProvider', () => {
142142
});
143143

144144
describe('paragon theme and brand', () => {
145+
let Component = (
146+
<AppProvider>
147+
<div>Child One</div>
148+
<div>Child Two</div>
149+
</AppProvider>
150+
);
151+
145152
it('calls trackColorSchemeChoice', () => {
146-
const Component = (
147-
<AppProvider>
148-
<div>Child One</div>
149-
<div>Child Two</div>
150-
</AppProvider>
151-
);
152153
render(Component);
153154
expect(useTrackColorSchemeChoice).toHaveBeenCalled();
154155
});
155156

156157
it('calls useParagonTheme', () => {
157-
const Component = (
158-
<AppProvider>
159-
<div>Child One</div>
160-
<div>Child Two</div>
161-
</AppProvider>
162-
);
163158
render(Component);
164159
expect(useParagonTheme).toHaveBeenCalled();
165160
expect(useParagonTheme).toHaveBeenCalledWith(
@@ -180,12 +175,6 @@ describe('AppProvider', () => {
180175
{ isThemeLoaded: false },
181176
jest.fn(),
182177
]);
183-
const Component = (
184-
<AppProvider>
185-
<div>Child One</div>
186-
<div>Child Two</div>
187-
</AppProvider>
188-
);
189178
const { container } = render(Component);
190179
expect(container).toBeEmptyDOMElement();
191180
});
@@ -196,7 +185,7 @@ describe('AppProvider', () => {
196185
{ isThemeLoaded: true, themeVariant: 'light' },
197186
mockUseParagonThemeDispatch,
198187
]);
199-
const Component = (
188+
Component = (
200189
<AppProvider>
201190
<AppContext.Consumer>
202191
{({ paragonTheme }) => (
@@ -235,12 +224,12 @@ describe('AppProvider', () => {
235224
});
236225

237226
describe('useAppEvent', () => {
227+
const Component = (
228+
<AppProvider>
229+
<div>Child</div>
230+
</AppProvider>
231+
);
238232
it('subscribes to `AUTHENTICATED_USER_CHANGED`', async () => {
239-
const Component = (
240-
<AppProvider>
241-
<div>Child</div>
242-
</AppProvider>
243-
);
244233
render(Component);
245234
expect(useAppEvent).toHaveBeenCalledWith(AUTHENTICATED_USER_CHANGED, expect.any(Function));
246235
const useAppEventMockCalls = useAppEvent.mock.calls;
@@ -252,11 +241,6 @@ describe('AppProvider', () => {
252241
});
253242

254243
it('subscribes to `CONFIG_CHANGED`', async () => {
255-
const Component = (
256-
<AppProvider>
257-
<div>Child</div>
258-
</AppProvider>
259-
);
260244
render(Component);
261245
expect(useAppEvent).toHaveBeenCalledWith(CONFIG_CHANGED, expect.any(Function));
262246
const useAppEventMockCalls = useAppEvent.mock.calls;
@@ -268,11 +252,6 @@ describe('AppProvider', () => {
268252
});
269253

270254
it('subscribes to `LOCALE_CHANGED`', async () => {
271-
const Component = (
272-
<AppProvider>
273-
<div>Child</div>
274-
</AppProvider>
275-
);
276255
render(Component);
277256
expect(useAppEvent).toHaveBeenCalledWith(LOCALE_CHANGED, expect.any(Function));
278257
const useAppEventMockCalls = useAppEvent.mock.calls;

src/react/hooks/paragon/useParagonTheme.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('useParagonTheme', () => {
6060
});
6161

6262
it('should configure theme variants according with system preference and add the change event listener', () => {
63-
const { unmount } = renderHook(() => useParagonTheme(getConfig()));
63+
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
6464
const themeLinks = document.head.querySelectorAll('link');
6565
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
6666
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');
@@ -70,13 +70,15 @@ describe('useParagonTheme', () => {
7070
expect(mockAddEventListener).toHaveBeenCalled();
7171
expect(darkLink.rel).toBe('stylesheet');
7272
expect(lightLink.rel).toBe('alternate stylesheet');
73+
expect(result.current[0]).toEqual({ isThemeLoaded: true, themeVariant: 'dark' });
74+
7375
unmount();
7476
expect(mockRemoveEventListener).toHaveBeenCalled();
7577
});
7678

7779
it('should configure theme variants according with user preference if is defined (localStorage)', () => {
7880
window.localStorage.getItem.mockReturnValue('light');
79-
const { unmount } = renderHook(() => useParagonTheme(getConfig()));
81+
const { result, unmount } = renderHook(() => useParagonTheme(getConfig()));
8082
const themeLinks = document.head.querySelectorAll('link');
8183
const darkLink = document.head.querySelector('link[data-paragon-theme-variant="dark"]');
8284
const lightLink = document.head.querySelector('link[data-paragon-theme-variant="light"]');
@@ -87,6 +89,8 @@ describe('useParagonTheme', () => {
8789

8890
expect(darkLink.rel).toBe('alternate stylesheet');
8991
expect(lightLink.rel).toBe('stylesheet');
92+
expect(result.current[0]).toEqual({ isThemeLoaded: true, themeVariant: 'light' });
93+
9094
unmount();
9195
expect(mockRemoveEventListener).toHaveBeenCalled();
9296
});

src/react/hooks/paragon/useParagonThemeCore.test.js

Lines changed: 11 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ jest.mock('../../../logging');
99

1010
describe('useParagonThemeCore', () => {
1111
const themeOnLoad = jest.fn();
12+
let coreConfig;
1213

13-
afterEach(() => {
14+
beforeEach(() => {
1415
document.head.innerHTML = '';
15-
jest.clearAllMocks();
16-
});
17-
18-
it('should load the core url and change the loading state to true', () => {
19-
const coreConfig = {
16+
coreConfig = {
2017
themeCore: {
21-
urls: { default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css' },
18+
urls: {
19+
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
20+
},
2221
},
2322
onLoad: themeOnLoad,
2423
};
24+
jest.clearAllMocks();
25+
});
2526

27+
it('should load the core url and change the loading state to true', () => {
2628
renderHook(() => useParagonThemeCore(coreConfig));
2729
const createdLinkTag = document.head.querySelector('link');
2830
act(() => createdLinkTag.onload());
@@ -31,15 +33,7 @@ describe('useParagonThemeCore', () => {
3133
});
3234

3335
it('should load the core default and brand url and change the loading state to true', () => {
34-
const coreConfig = {
35-
themeCore: {
36-
urls: {
37-
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
38-
brandOverride: 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0Version/dist/core.min.css',
39-
},
40-
},
41-
onLoad: themeOnLoad,
42-
};
36+
coreConfig.themeCore.urls.brandOverride = 'https://cdn.jsdelivr.net/npm/@edx/brand@$2.0.0Version/dist/core.min.css';
4337

4438
renderHook(() => useParagonThemeCore(coreConfig));
4539
const createdLinkTag = document.head.querySelector('link[data-paragon-theme-core="true"]');
@@ -52,33 +46,6 @@ describe('useParagonThemeCore', () => {
5246
});
5347

5448
it('should dispatch a log error and fallback to PARAGON_THEME if can not load the core theme link', () => {
55-
global.PARAGON_THEME = {
56-
paragon: {
57-
version: '1.0.0',
58-
themeUrls: {
59-
core: {
60-
fileName: 'core.min.css',
61-
},
62-
defaults: {
63-
light: 'light',
64-
},
65-
variants: {
66-
light: {
67-
fileName: 'light.min.css',
68-
},
69-
},
70-
},
71-
},
72-
};
73-
const coreConfig = {
74-
themeCore: {
75-
urls: {
76-
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
77-
},
78-
},
79-
onLoad: themeOnLoad,
80-
};
81-
8249
renderHook(() => useParagonThemeCore(coreConfig));
8350
const createdLinkTag = document.head.querySelector('link[data-paragon-theme-core="true"]');
8451

@@ -90,14 +57,6 @@ describe('useParagonThemeCore', () => {
9057

9158
it('should not create a new link if the core theme is already loaded', () => {
9259
document.head.innerHTML = '<link rel="preload" as="style" href="https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css" onerror="this.remove();">';
93-
const coreConfig = {
94-
themeCore: {
95-
urls: {
96-
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
97-
},
98-
},
99-
onLoad: themeOnLoad,
100-
};
10160

10261
renderHook(() => useParagonThemeCore(coreConfig));
10362
const themeCoreLinks = document.head.querySelectorAll('link');
@@ -107,7 +66,7 @@ describe('useParagonThemeCore', () => {
10766
});
10867

10968
it('should not create any core link if can not find themeCore urls definition', () => {
110-
const coreConfig = {
69+
coreConfig = {
11170
themeCore: {
11271
default: 'https://cdn.jsdelivr.net/npm/@edx/paragon@$21.0.0/dist/core.min.css',
11372
},

src/react/hooks/paragon/useParagonThemeVariants.test.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,6 @@ describe('useParagonThemeVariants', () => {
4040
});
4141

4242
it('should dispatch a log error and fallback to PARAGON_THEME if can not load the variant theme link', () => {
43-
global.PARAGON_THEME = {
44-
paragon: {
45-
version: '1.0.0',
46-
themeUrls: {
47-
core: {
48-
fileName: 'core.min.css',
49-
},
50-
defaults: {
51-
light: 'light',
52-
},
53-
variants: {
54-
light: {
55-
fileName: 'light.min.css',
56-
},
57-
},
58-
},
59-
},
60-
};
6143
const themeVariants = {
6244
light: {
6345
urls: {

0 commit comments

Comments
 (0)