Skip to content

Commit 565ebb8

Browse files
committed
Add more tests
1 parent 924c119 commit 565ebb8

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

src/components/Slot/index.test.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,14 @@ describe('<Slot />', () => {
1818
fallback: {title: 'fallback'},
1919
};
2020

21-
const result = {content: {title: 'result'}} satisfies FetchResponse;
21+
const result = {
22+
metadata: {
23+
version: '1.0',
24+
},
25+
content: {
26+
title: 'result',
27+
},
28+
} satisfies FetchResponse;
2229

2330
jest.mocked(useContent).mockReturnValue(result);
2431

src/hooks/useContent.test.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ describe('useContent (CSR)', () => {
3434
});
3535

3636
it('should fetch the content', () => {
37-
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
38-
content: {},
39-
});
37+
const fetch: Plug['fetch'] = jest.fn();
38+
39+
const response: FetchResponse<{title: string}> = {
40+
metadata: {
41+
version: '1.0',
42+
},
43+
content: {
44+
title: 'foo',
45+
},
46+
};
4047

4148
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
42-
jest.mocked(useLoader).mockReturnValue({
43-
title: 'foo',
44-
});
49+
jest.mocked(useLoader).mockReturnValue(response);
4550

4651
const slotId = 'home-banner@1';
4752
const preferredLocale = 'en';
@@ -78,15 +83,15 @@ describe('useContent (CSR)', () => {
7883
attributes: attributes,
7984
});
8085

81-
expect(result.current).toEqual({title: 'foo'});
86+
expect(result.current).toEqual(response);
8287
});
8388

8489
it('should use the initial value when the cache key changes if the stale-while-loading flag is false', async () => {
8590
const key = {
8691
current: 'initial',
8792
};
8893

89-
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({content: {}});
94+
const fetch: Plug['fetch'] = jest.fn();
9095

9196
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
9297

@@ -147,7 +152,7 @@ describe('useContent (CSR)', () => {
147152
current: 'initial',
148153
};
149154

150-
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({content: {}});
155+
const fetch: Plug['fetch'] = jest.fn();
151156

152157
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
153158

@@ -283,9 +288,7 @@ describe('useContent (CSR)', () => {
283288
const slotId = 'slot-id';
284289
const preferredLocale = 'en';
285290

286-
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
287-
content: {},
288-
});
291+
const fetch: Plug['fetch'] = jest.fn();
289292

290293
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
291294

@@ -310,9 +313,7 @@ describe('useContent (CSR)', () => {
310313
});
311314

312315
it('should normalize an empty preferred locale to undefined', () => {
313-
const fetch: Plug['fetch'] = jest.fn().mockResolvedValue({
314-
content: {},
315-
});
316+
const fetch: Plug['fetch'] = jest.fn();
316317

317318
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
318319

@@ -329,4 +330,35 @@ describe('useContent (CSR)', () => {
329330

330331
expect(jest.mocked(fetch).mock.calls[0][1]).toStrictEqual({});
331332
});
333+
334+
it('should return the metadata along with the content', () => {
335+
const fetch: Plug['fetch'] = jest.fn();
336+
337+
const response: FetchResponse<{title: string}> = {
338+
metadata: {
339+
version: '1.0',
340+
},
341+
content: {
342+
title: 'foo',
343+
},
344+
};
345+
346+
jest.mocked(useCroct).mockReturnValue({fetch: fetch} as Plug);
347+
jest.mocked(useLoader).mockReturnValue(response);
348+
349+
const slotId = 'home-banner@1';
350+
351+
const {result} = renderHook(
352+
() => useContent<{title: string}>(slotId),
353+
);
354+
355+
jest.mocked(useLoader)
356+
.mock
357+
.calls[0][0]
358+
.loader();
359+
360+
expect(fetch).toHaveBeenCalledWith(slotId, {});
361+
362+
expect(result.current).toEqual(response);
363+
});
332364
});

0 commit comments

Comments
 (0)