-
-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathinvalidateAll.tsx
More file actions
149 lines (138 loc) · 3.7 KB
/
invalidateAll.tsx
File metadata and controls
149 lines (138 loc) · 3.7 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
146
147
148
149
import { CacheProvider } from '@data-client/react';
import { makeRenderDataClient, renderHook, act } from '@data-client/test';
import { FixtureEndpoint } from '@data-client/test';
import { CoolerArticleResource, GetPhoto } from '__tests__/new';
import nock from 'nock';
import { useEffect } from 'react';
import { useCache, useController } from '../..';
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: CoolerArticleResource.get,
args: [{ id: 5 }],
response: payload,
};
export const nested: FixtureEndpoint = {
endpoint: CoolerArticleResource.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('invalidateAll', () => {
it('should not invalidate anything not matching', () => {
const { result } = renderDataClient(
() => {
return {
data: useCache(CoolerArticleResource.get, { id: 5 }),
controller: useController(),
};
},
{ initialFixtures: [detail] },
);
expect(result.current.data).toBeDefined();
act(() => {
result.current.controller.invalidateAll(CoolerArticleResource.update);
});
expect(result.current.data).toBeDefined();
act(() => {
result.current.controller.invalidateAll(CoolerArticleResource.getList);
});
expect(result.current.data).toBeDefined();
});
it('should result in useCache having no entry', () => {
const { result } = renderDataClient(
() => {
return {
data: useCache(CoolerArticleResource.get, { id: 5 }),
controller: useController(),
};
},
{ initialFixtures: [detail] },
);
expect(result.current.data).toBeDefined();
act(() => {
result.current.controller.invalidateAll(CoolerArticleResource.get);
});
expect(result.current.data).toBeUndefined();
});
it('should return the same === function each time', () => {
const track = jest.fn();
const { rerender } = renderHook(() => {
const invalidateAll = useController().invalidateAll;
useEffect(track, [invalidateAll]);
});
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.invalidateAll(GetPhoto);
});
expect(result.current.data).toBeUndefined();
});
});