Skip to content

Commit 9b488f3

Browse files
committed
Introduce useEntityRecords unit tests
1 parent 736eedf commit 9b488f3

File tree

3 files changed

+128
-2
lines changed

3 files changed

+128
-2
lines changed

sources/js/src/hooks/use-entity-records.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { Set } from 'immutable';
33

44
import { useEntityRecords as useCoreEntityRecords } from '@wordpress/core-data';
55

6-
// TODO `useEntityRecords` needs tests
6+
/**
7+
* The hook will return an empty collection while resolving.
8+
* This is to guarantee a better flow in the data manipulation, therefore do not count on the data returned by the
9+
* `records()` as an indicator of the hook status.
10+
*
11+
* @internal
12+
* @param kind The kind of entity to fetch. E.g. 'root', 'postType', 'taxonomy', etc.
13+
* @param name The name of the entity to fetch. E.g. 'post', 'page', 'category', etc.
14+
* @param queryArgs The query args to pass to the entity fetch. E.g. { per_page: 100 }
15+
*/
716
export function useEntityRecords<Entity>(
817
kind: string,
918
name: string,

sources/js/src/hooks/use-query-viewable-post-types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Set } from 'immutable';
33

44
import { useEntityRecords } from './use-entity-records';
55

6-
// TODO `useQueryViewablePostTypes` require more unit tests
76
export function useQueryViewablePostTypes(): EntitiesSearch.EntitiesRecords<EntitiesSearch.ViewablePostType> {
87
const entitiesRecords = useEntityRecords<EntitiesSearch.PostType<'edit'>>(
98
'root',
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { fromPartial } from '@total-typescript/shoehorn';
2+
import EntitiesSearch from '@types';
3+
4+
import { describe, it, jest, expect } from '@jest/globals';
5+
6+
import { useEntityRecords as useCoreEntityRecords } from '@wordpress/core-data';
7+
8+
import { useEntityRecords } from '../../../../sources/js/src';
9+
10+
jest.mock('@wordpress/core-data', () => ({
11+
useEntityRecords: jest.fn(),
12+
}));
13+
14+
describe('useEntityRecords', () => {
15+
/*
16+
* We want to guarantee the Records are returned and the hook succeeds.
17+
* Shouldn't be possible to obtain a succeed status if the records are resolving or errored.
18+
*/
19+
it('return set of records and succeed', () => {
20+
const records = [
21+
fromPartial<EntitiesSearch.PostType<'edit'>>({
22+
name: 'foo',
23+
}),
24+
fromPartial<EntitiesSearch.PostType<'edit'>>({
25+
name: 'foo-3',
26+
}),
27+
];
28+
29+
jest.mocked(useCoreEntityRecords).mockReturnValue({
30+
records,
31+
hasResolved: true,
32+
isResolving: false,
33+
// @ts-ignore
34+
status: EntitiesSearch.ResolveStatus.SUCCESS,
35+
});
36+
37+
const result = useEntityRecords<EntitiesSearch.PostType<'edit'>>(
38+
'root',
39+
'postType'
40+
);
41+
42+
expect(result.records().size).toEqual(2);
43+
for (const record of records) {
44+
expect(result.records()).toContain(record);
45+
}
46+
47+
expect(result.isResolving()).toEqual(false);
48+
expect(result.errored()).toEqual(false);
49+
expect(result.succeed()).toEqual(true);
50+
});
51+
52+
/*
53+
* We want to guarantee `records()` returns null when the hook is resolving.
54+
*/
55+
it('return an empty collection of records when resolving', () => {
56+
jest.mocked(useCoreEntityRecords).mockReturnValue({
57+
records: null,
58+
hasResolved: false,
59+
isResolving: true,
60+
// @ts-ignore
61+
status: EntitiesSearch.ResolveStatus.RESOLVING,
62+
});
63+
64+
const result = useEntityRecords<EntitiesSearch.PostType<'edit'>>(
65+
'root',
66+
'postType'
67+
);
68+
69+
expect(result.records().isEmpty()).toEqual(true);
70+
expect(result.isResolving()).toEqual(true);
71+
expect(result.errored()).toEqual(false);
72+
expect(result.succeed()).toEqual(false);
73+
});
74+
75+
/*
76+
* We want to errored and succeed are not returning the same value.
77+
*/
78+
it('return errored and succeed as different values', () => {
79+
jest.mocked(useCoreEntityRecords).mockReturnValue({
80+
records: null,
81+
hasResolved: true,
82+
isResolving: false,
83+
// @ts-ignore
84+
status: EntitiesSearch.ResolveStatus.ERROR,
85+
});
86+
87+
const result = useEntityRecords<EntitiesSearch.PostType<'edit'>>(
88+
'root',
89+
'postType'
90+
);
91+
92+
expect(result.isResolving()).toEqual(false);
93+
expect(result.errored()).toEqual(true);
94+
expect(result.succeed()).toEqual(false);
95+
});
96+
97+
/*
98+
* We want to guarantee the errored and success statuses are not returning `true` while resolving.
99+
*/
100+
it('return errored and succeed as false while resolving', () => {
101+
jest.mocked(useCoreEntityRecords).mockReturnValue({
102+
records: null,
103+
hasResolved: false,
104+
isResolving: true,
105+
// @ts-ignore
106+
status: EntitiesSearch.ResolveStatus.RESOLVING,
107+
});
108+
109+
const result = useEntityRecords<EntitiesSearch.PostType<'edit'>>(
110+
'root',
111+
'postType'
112+
);
113+
114+
expect(result.isResolving()).toEqual(true);
115+
expect(result.errored()).toEqual(false);
116+
expect(result.succeed()).toEqual(false);
117+
});
118+
});

0 commit comments

Comments
 (0)