|
| 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