-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathinvalidate.tsx
More file actions
145 lines (134 loc) · 3.54 KB
/
invalidate.tsx
File metadata and controls
145 lines (134 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { CacheProvider } from '@data-client/react';
import { FixtureEndpoint } from '@data-client/test';
import { FutureArticleResource, GetPhoto } from '__tests__/new';
import nock from 'nock';
import { useEffect } from 'react';
import { useCache, useController } from '../..';
import { makeRenderDataClient, act, renderHook } from '../../../../../test';
export const payload = {
id: 5,
title: 'hi ho',
content: 'whatever',
tags: ['a', 'best', 'react'],
};
export const createPayload = {
id: 1,
title: 'hi ho',
content: 'whatever',
tags: ['a', 'best', 'react'],
};
export const detail: FixtureEndpoint = {
endpoint: FutureArticleResource.get,
args: [5],
response: payload,
};
export const nested: FixtureEndpoint = {
endpoint: FutureArticleResource.getList,
args: [],
response: [
{
id: 5,
title: 'hi ho',
content: 'whatever',
tags: ['a', 'best', 'react'],
author: {
id: 23,
username: 'bob',
},
},
{
id: 3,
title: 'the next time',
content: 'whatever',
author: {
id: 23,
username: 'charles',
email: 'bob@bob.com',
},
},
],
};
let renderDataClient: ReturnType<typeof makeRenderDataClient>;
let mynock: nock.Scope;
beforeEach(() => {
renderDataClient = makeRenderDataClient(CacheProvider);
mynock = nock(/.*/).defaultReplyHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
});
});
afterEach(() => {
nock.cleanAll();
});
describe('invalidate', () => {
it('should not invalidate anything if params is null', () => {
const { result } = renderDataClient(
() => {
return {
data: useCache(FutureArticleResource.get, 5),
controller: useController(),
};
},
{ initialFixtures: [detail] },
);
expect(result.current.data).toBeDefined();
act(() => {
result.current.controller.invalidate(FutureArticleResource.get, null);
});
expect(result.current.data).toBeDefined();
});
it('should result in useCache having no entry', () => {
const { result } = renderDataClient(
() => {
return {
data: useCache(FutureArticleResource.get, 5),
controller: useController(),
};
},
{ initialFixtures: [detail] },
);
expect(result.current.data).toBeDefined();
act(() => {
result.current.controller.invalidate(FutureArticleResource.get, 5);
});
expect(result.current.data).toBeUndefined();
});
it('should return the same === function each time', () => {
const track = jest.fn();
const { rerender } = renderHook(() => {
const invalidate = useController().invalidate;
useEffect(track, [invalidate]);
});
expect(track.mock.calls.length).toBe(1);
for (let i = 0; i < 4; ++i) {
rerender();
}
expect(track.mock.calls.length).toBe(1);
});
it('should work with ArrayBuffer shapes', () => {
const userId = '5';
const response = new ArrayBuffer(10);
const { result, waitForNextUpdate } = renderDataClient(
() => {
return {
data: useCache(GetPhoto, { userId }),
controller: useController(),
};
},
{
initialFixtures: [
{
endpoint: GetPhoto,
response,
args: [{ userId }],
},
],
},
);
expect(result.current.data).toEqual(response);
act(() => {
result.current.controller.invalidate(GetPhoto, { userId });
});
expect(result.current.data).toBeUndefined();
});
});