@data-client/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +71 -0
- package/dist/index.js +1643 -0
- package/dist/index.umd.min.js +1 -0
- package/dist/next.js +461 -0
- package/dist/package.json +1 -0
- package/legacy/actionTypes.js +12 -0
- package/legacy/compatibleActions.js +2 -0
- package/legacy/controller/BaseController.js +289 -0
- package/legacy/controller/Controller.js +20 -0
- package/legacy/controller/createFetch.js +42 -0
- package/legacy/controller/createInvalidate.js +12 -0
- package/legacy/controller/createInvalidateAll.js +8 -0
- package/legacy/controller/createOptimistic.js +33 -0
- package/legacy/controller/createReceive.js +36 -0
- package/legacy/controller/createReset.js +8 -0
- package/legacy/controller/createSubscription.js +30 -0
- package/legacy/controller/types.js +2 -0
- package/legacy/endpoint/index.js +2 -0
- package/legacy/endpoint/shapes.js +2 -0
- package/legacy/endpoint/types.js +2 -0
- package/legacy/fsa.js +2 -0
- package/legacy/index.js +22 -0
- package/legacy/internal.js +4 -0
- package/legacy/legacyActions.js +2 -0
- package/legacy/manager/ConnectionListener.js +2 -0
- package/legacy/manager/DefaultConnectionListener.js +40 -0
- package/legacy/manager/DevtoolsManager.js +73 -0
- package/legacy/manager/LogoutManager.js +34 -0
- package/legacy/manager/NetworkManager.js +291 -0
- package/legacy/manager/PollingSubscription.js +159 -0
- package/legacy/manager/SubscriptionManager.js +117 -0
- package/legacy/manager/applyManager.js +23 -0
- package/legacy/manager/devtoolsTypes.js +2 -0
- package/legacy/manager/index.js +6 -0
- package/legacy/middlewareTypes.js +2 -0
- package/legacy/newActions.js +2 -0
- package/legacy/next/Controller.js +24 -0
- package/legacy/next/index.js +3 -0
- package/legacy/previousActions.js +2 -0
- package/legacy/state/RIC.js +3 -0
- package/legacy/state/applyUpdatersToResults.js +4 -0
- package/legacy/state/legacy-actions/createFetch.js +62 -0
- package/legacy/state/legacy-actions/createReceive.js +37 -0
- package/legacy/state/legacy-actions/createReceiveError.js +28 -0
- package/legacy/state/legacy-actions/index.js +4 -0
- package/legacy/state/reducer/createReducer.js +54 -0
- package/legacy/state/reducer/fetchReducer.js +32 -0
- package/legacy/state/reducer/invalidateReducer.js +28 -0
- package/legacy/state/reducer/setReducer.js +113 -0
- package/legacy/state/reducerInstance.js +9 -0
- package/legacy/state/selectMeta.js +4 -0
- package/legacy/types.js +8 -0
- package/lib/actionTypes.d.ts +11 -0
- package/lib/actionTypes.d.ts.map +1 -0
- package/lib/actionTypes.js +12 -0
- package/lib/compatibleActions.d.ts +47 -0
- package/lib/compatibleActions.d.ts.map +1 -0
- package/lib/compatibleActions.js +2 -0
- package/lib/controller/BaseController.d.ts +128 -0
- package/lib/controller/BaseController.d.ts.map +1 -0
- package/lib/controller/BaseController.js +289 -0
- package/lib/controller/Controller.d.ts +14 -0
- package/lib/controller/Controller.d.ts.map +1 -0
- package/lib/controller/Controller.js +20 -0
- package/lib/controller/createFetch.d.ts +12 -0
- package/lib/controller/createFetch.d.ts.map +1 -0
- package/lib/controller/createFetch.js +42 -0
- package/lib/controller/createInvalidate.d.ts +6 -0
- package/lib/controller/createInvalidate.d.ts.map +1 -0
- package/lib/controller/createInvalidate.js +12 -0
- package/lib/controller/createInvalidateAll.d.ts +3 -0
- package/lib/controller/createInvalidateAll.d.ts.map +1 -0
- package/lib/controller/createInvalidateAll.js +8 -0
- package/lib/controller/createOptimistic.d.ts +10 -0
- package/lib/controller/createOptimistic.d.ts.map +1 -0
- package/lib/controller/createOptimistic.js +33 -0
- package/lib/controller/createReceive.d.ts +20 -0
- package/lib/controller/createReceive.d.ts.map +1 -0
- package/lib/controller/createReceive.js +36 -0
- package/lib/controller/createReset.d.ts +3 -0
- package/lib/controller/createReset.d.ts.map +1 -0
- package/lib/controller/createReset.js +8 -0
- package/lib/controller/createSubscription.d.ts +9 -0
- package/lib/controller/createSubscription.d.ts.map +1 -0
- package/lib/controller/createSubscription.js +30 -0
- package/lib/controller/types.d.ts +6 -0
- package/lib/controller/types.d.ts.map +1 -0
- package/lib/controller/types.js +2 -0
- package/lib/endpoint/index.d.ts +3 -0
- package/lib/endpoint/index.d.ts.map +1 -0
- package/lib/endpoint/index.js +2 -0
- package/lib/endpoint/shapes.d.ts +25 -0
- package/lib/endpoint/shapes.d.ts.map +1 -0
- package/lib/endpoint/shapes.js +2 -0
- package/lib/endpoint/types.d.ts +45 -0
- package/lib/endpoint/types.d.ts.map +1 -0
- package/lib/endpoint/types.js +2 -0
- package/lib/fsa.d.ts +41 -0
- package/lib/fsa.d.ts.map +1 -0
- package/lib/fsa.js +2 -0
- package/lib/index.d.ts +19 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +22 -0
- package/lib/internal.d.ts +4 -0
- package/lib/internal.d.ts.map +1 -0
- package/lib/internal.js +4 -0
- package/lib/legacyActions.d.ts +92 -0
- package/lib/legacyActions.d.ts.map +1 -0
- package/lib/legacyActions.js +2 -0
- package/lib/manager/ConnectionListener.d.ts +8 -0
- package/lib/manager/ConnectionListener.d.ts.map +1 -0
- package/lib/manager/ConnectionListener.js +2 -0
- package/lib/manager/DefaultConnectionListener.d.ts +20 -0
- package/lib/manager/DefaultConnectionListener.d.ts.map +1 -0
- package/lib/manager/DefaultConnectionListener.js +40 -0
- package/lib/manager/DevtoolsManager.d.ts +24 -0
- package/lib/manager/DevtoolsManager.d.ts.map +1 -0
- package/lib/manager/DevtoolsManager.js +74 -0
- package/lib/manager/LogoutManager.d.ts +25 -0
- package/lib/manager/LogoutManager.d.ts.map +1 -0
- package/lib/manager/LogoutManager.js +34 -0
- package/lib/manager/NetworkManager.d.ts +82 -0
- package/lib/manager/NetworkManager.d.ts.map +1 -0
- package/lib/manager/NetworkManager.js +295 -0
- package/lib/manager/PollingSubscription.d.ts +45 -0
- package/lib/manager/PollingSubscription.d.ts.map +1 -0
- package/lib/manager/PollingSubscription.js +159 -0
- package/lib/manager/SubscriptionManager.d.ts +55 -0
- package/lib/manager/SubscriptionManager.d.ts.map +1 -0
- package/lib/manager/SubscriptionManager.js +117 -0
- package/lib/manager/applyManager.d.ts +10 -0
- package/lib/manager/applyManager.d.ts.map +1 -0
- package/lib/manager/applyManager.js +23 -0
- package/lib/manager/devtoolsTypes.d.ts +205 -0
- package/lib/manager/devtoolsTypes.d.ts.map +1 -0
- package/lib/manager/devtoolsTypes.js +2 -0
- package/lib/manager/index.d.ts +8 -0
- package/lib/manager/index.d.ts.map +1 -0
- package/lib/manager/index.js +6 -0
- package/lib/middlewareTypes.d.ts +18 -0
- package/lib/middlewareTypes.d.ts.map +1 -0
- package/lib/middlewareTypes.js +2 -0
- package/lib/newActions.d.ts +85 -0
- package/lib/newActions.d.ts.map +1 -0
- package/lib/newActions.js +2 -0
- package/lib/next/Controller.d.ts +14 -0
- package/lib/next/Controller.d.ts.map +1 -0
- package/lib/next/Controller.js +24 -0
- package/lib/next/index.d.ts +3 -0
- package/lib/next/index.d.ts.map +1 -0
- package/lib/next/index.js +3 -0
- package/lib/previousActions.d.ts +91 -0
- package/lib/previousActions.d.ts.map +1 -0
- package/lib/previousActions.js +2 -0
- package/lib/state/RIC.d.ts +2 -0
- package/lib/state/RIC.js +3 -0
- package/lib/state/applyUpdatersToResults.d.ts +13 -0
- package/lib/state/applyUpdatersToResults.d.ts.map +1 -0
- package/lib/state/applyUpdatersToResults.js +7 -0
- package/lib/state/legacy-actions/createFetch.d.ts +19 -0
- package/lib/state/legacy-actions/createFetch.d.ts.map +1 -0
- package/lib/state/legacy-actions/createFetch.js +62 -0
- package/lib/state/legacy-actions/createReceive.d.ts +14 -0
- package/lib/state/legacy-actions/createReceive.d.ts.map +1 -0
- package/lib/state/legacy-actions/createReceive.js +37 -0
- package/lib/state/legacy-actions/createReceiveError.d.ts +9 -0
- package/lib/state/legacy-actions/createReceiveError.d.ts.map +1 -0
- package/lib/state/legacy-actions/createReceiveError.js +28 -0
- package/lib/state/legacy-actions/index.d.ts +4 -0
- package/lib/state/legacy-actions/index.d.ts.map +1 -0
- package/lib/state/legacy-actions/index.js +4 -0
- package/lib/state/reducer/createReducer.d.ts +7 -0
- package/lib/state/reducer/createReducer.d.ts.map +1 -0
- package/lib/state/reducer/createReducer.js +55 -0
- package/lib/state/reducer/fetchReducer.d.ts +4 -0
- package/lib/state/reducer/fetchReducer.d.ts.map +1 -0
- package/lib/state/reducer/fetchReducer.js +34 -0
- package/lib/state/reducer/invalidateReducer.d.ts +37 -0
- package/lib/state/reducer/invalidateReducer.d.ts.map +1 -0
- package/lib/state/reducer/invalidateReducer.js +34 -0
- package/lib/state/reducer/setReducer.d.ts +40 -0
- package/lib/state/reducer/setReducer.d.ts.map +1 -0
- package/lib/state/reducer/setReducer.js +119 -0
- package/lib/state/reducerInstance.d.ts +7 -0
- package/lib/state/reducerInstance.d.ts.map +1 -0
- package/lib/state/reducerInstance.js +9 -0
- package/lib/state/selectMeta.d.ts +3 -0
- package/lib/state/selectMeta.d.ts.map +1 -0
- package/lib/state/selectMeta.js +4 -0
- package/lib/types.d.ts +71 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +8 -0
- package/node.mjs +1 -0
- package/package.json +127 -0
- package/src/actionTypes.ts +11 -0
- package/src/compatibleActions.ts +96 -0
- package/src/controller/BaseController.ts +508 -0
- package/src/controller/Controller.ts +32 -0
- package/src/controller/__tests__/Controller.ts +29 -0
- package/src/controller/__tests__/__snapshots__/getResponse.ts.snap +35 -0
- package/src/controller/__tests__/getResponse.ts +182 -0
- package/src/controller/createFetch.ts +54 -0
- package/src/controller/createInvalidate.ts +16 -0
- package/src/controller/createInvalidateAll.ts +11 -0
- package/src/controller/createOptimistic.ts +47 -0
- package/src/controller/createReceive.ts +85 -0
- package/src/controller/createReset.ts +9 -0
- package/src/controller/createSubscription.ts +39 -0
- package/src/controller/types.ts +22 -0
- package/src/endpoint/index.ts +14 -0
- package/src/endpoint/shapes.ts +53 -0
- package/src/endpoint/types.ts +72 -0
- package/src/fsa.ts +99 -0
- package/src/index.ts +61 -0
- package/src/internal.ts +3 -0
- package/src/legacyActions.ts +163 -0
- package/src/manager/ConnectionListener.ts +7 -0
- package/src/manager/DefaultConnectionListener.ts +54 -0
- package/src/manager/DevtoolsManager.ts +99 -0
- package/src/manager/LogoutManager.ts +57 -0
- package/src/manager/NetworkManager.ts +346 -0
- package/src/manager/PollingSubscription.ts +190 -0
- package/src/manager/SubscriptionManager.ts +156 -0
- package/src/manager/__tests__/__snapshots__/pollingSubscription-endpoint.ts.snap +49 -0
- package/src/manager/__tests__/__snapshots__/pollingSubscription.ts.snap +43 -0
- package/src/manager/__tests__/logoutManager.ts +112 -0
- package/src/manager/__tests__/manager.ts +44 -0
- package/src/manager/__tests__/networkManager-legacy.ts +394 -0
- package/src/manager/__tests__/networkManager.ts +426 -0
- package/src/manager/__tests__/pollingSubscription-endpoint.ts +423 -0
- package/src/manager/__tests__/pollingSubscription.ts +313 -0
- package/src/manager/__tests__/subscriptionManager.ts +208 -0
- package/src/manager/applyManager.ts +33 -0
- package/src/manager/devtoolsTypes.ts +210 -0
- package/src/manager/index.ts +7 -0
- package/src/middlewareTypes.ts +49 -0
- package/src/newActions.ts +140 -0
- package/src/next/Controller.ts +39 -0
- package/src/next/index.ts +2 -0
- package/src/package.json +1 -0
- package/src/previousActions.ts +159 -0
- package/src/state/RIC.d.ts +2 -0
- package/src/state/RIC.js +5 -0
- package/src/state/__tests__/RIC.web.ts +16 -0
- package/src/state/__tests__/__snapshots__/reducer.ts.snap +56 -0
- package/src/state/__tests__/applyUpdatersToResults.ts +40 -0
- package/src/state/__tests__/reducer.ts +868 -0
- package/src/state/applyUpdatersToResults.ts +29 -0
- package/src/state/legacy-actions/createFetch.ts +95 -0
- package/src/state/legacy-actions/createReceive.ts +68 -0
- package/src/state/legacy-actions/createReceiveError.ts +43 -0
- package/src/state/legacy-actions/index.ts +3 -0
- package/src/state/reducer/createReducer.ts +80 -0
- package/src/state/reducer/fetchReducer.ts +48 -0
- package/src/state/reducer/invalidateReducer.ts +39 -0
- package/src/state/reducer/setReducer.ts +157 -0
- package/src/state/reducerInstance.ts +14 -0
- package/src/state/selectMeta.ts +8 -0
- package/src/types.ts +125 -0
- package/ts3.4/actionTypes.d.ts +11 -0
- package/ts3.4/compatibleActions.d.ts +47 -0
- package/ts3.4/controller/BaseController.d.ts +170 -0
- package/ts3.4/controller/Controller.d.ts +14 -0
- package/ts3.4/controller/createFetch.d.ts +14 -0
- package/ts3.4/controller/createInvalidate.d.ts +8 -0
- package/ts3.4/controller/createInvalidateAll.d.ts +3 -0
- package/ts3.4/controller/createOptimistic.d.ts +12 -0
- package/ts3.4/controller/createReceive.d.ts +24 -0
- package/ts3.4/controller/createReset.d.ts +3 -0
- package/ts3.4/controller/createSubscription.d.ts +13 -0
- package/ts3.4/controller/types.d.ts +6 -0
- package/ts3.4/endpoint/index.d.ts +3 -0
- package/ts3.4/endpoint/shapes.d.ts +25 -0
- package/ts3.4/endpoint/types.d.ts +45 -0
- package/ts3.4/fsa.d.ts +41 -0
- package/ts3.4/index.d.ts +22 -0
- package/ts3.4/internal.d.ts +4 -0
- package/ts3.4/legacyActions.d.ts +95 -0
- package/ts3.4/manager/ConnectionListener.d.ts +8 -0
- package/ts3.4/manager/DefaultConnectionListener.d.ts +20 -0
- package/ts3.4/manager/DevtoolsManager.d.ts +24 -0
- package/ts3.4/manager/LogoutManager.d.ts +25 -0
- package/ts3.4/manager/NetworkManager.d.ts +82 -0
- package/ts3.4/manager/PollingSubscription.d.ts +45 -0
- package/ts3.4/manager/SubscriptionManager.d.ts +55 -0
- package/ts3.4/manager/applyManager.d.ts +10 -0
- package/ts3.4/manager/devtoolsTypes.d.ts +205 -0
- package/ts3.4/manager/index.d.ts +8 -0
- package/ts3.4/middlewareTypes.d.ts +18 -0
- package/ts3.4/newActions.d.ts +88 -0
- package/ts3.4/next/Controller.d.ts +14 -0
- package/ts3.4/next/index.d.ts +3 -0
- package/ts3.4/previousActions.d.ts +94 -0
- package/ts3.4/state/RIC.d.ts +2 -0
- package/ts3.4/state/applyUpdatersToResults.d.ts +13 -0
- package/ts3.4/state/legacy-actions/createFetch.d.ts +19 -0
- package/ts3.4/state/legacy-actions/createReceive.d.ts +14 -0
- package/ts3.4/state/legacy-actions/createReceiveError.d.ts +9 -0
- package/ts3.4/state/legacy-actions/index.d.ts +4 -0
- package/ts3.4/state/reducer/createReducer.d.ts +7 -0
- package/ts3.4/state/reducer/fetchReducer.d.ts +4 -0
- package/ts3.4/state/reducer/invalidateReducer.d.ts +37 -0
- package/ts3.4/state/reducer/setReducer.d.ts +40 -0
- package/ts3.4/state/reducerInstance.d.ts +7 -0
- package/ts3.4/state/selectMeta.d.ts +3 -0
- package/ts3.4/types.d.ts +73 -0
- package/typescript.svg +8 -0
|
@@ -0,0 +1,868 @@
|
|
|
1
|
+
import { DELETED, schema } from '@data-client/endpoint';
|
|
2
|
+
import {
|
|
3
|
+
ArticleResource,
|
|
4
|
+
ArticleResourceWithOtherListUrl,
|
|
5
|
+
PaginatedArticleResource,
|
|
6
|
+
Article,
|
|
7
|
+
PaginatedArticle,
|
|
8
|
+
UrlArticle,
|
|
9
|
+
} from '__tests__/new';
|
|
10
|
+
|
|
11
|
+
import { Controller } from '../..';
|
|
12
|
+
import {
|
|
13
|
+
RECEIVE_TYPE,
|
|
14
|
+
INVALIDATE_TYPE,
|
|
15
|
+
FETCH_TYPE,
|
|
16
|
+
RESET_TYPE,
|
|
17
|
+
GC_TYPE,
|
|
18
|
+
} from '../../actionTypes';
|
|
19
|
+
import { UpdateFunction, State, ActionTypes, legacyActions } from '../../types';
|
|
20
|
+
import { createReceive } from '../legacy-actions';
|
|
21
|
+
import createReducer, { initialState } from '../reducer/createReducer';
|
|
22
|
+
|
|
23
|
+
type FetchAction = legacyActions.FetchAction;
|
|
24
|
+
type ReceiveAction<T extends string | number | object | null = any> =
|
|
25
|
+
legacyActions.ReceiveAction<T>;
|
|
26
|
+
type ResetAction = legacyActions.ResetAction;
|
|
27
|
+
type InvalidateAction = legacyActions.InvalidateAction;
|
|
28
|
+
type GCAction = legacyActions.GCAction;
|
|
29
|
+
|
|
30
|
+
describe('reducer', () => {
|
|
31
|
+
let reducer: (
|
|
32
|
+
state: State<unknown> | undefined,
|
|
33
|
+
action: ActionTypes,
|
|
34
|
+
) => State<unknown>;
|
|
35
|
+
|
|
36
|
+
beforeEach(() => {
|
|
37
|
+
reducer = createReducer(new Controller());
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('singles', () => {
|
|
41
|
+
const id = 20;
|
|
42
|
+
const payload = { id, title: 'hi', content: 'this is the content' };
|
|
43
|
+
const action: ReceiveAction<typeof payload> = {
|
|
44
|
+
type: RECEIVE_TYPE,
|
|
45
|
+
payload,
|
|
46
|
+
meta: {
|
|
47
|
+
schema: Article,
|
|
48
|
+
key: ArticleResource.get.url({ id }),
|
|
49
|
+
date: 5000000000,
|
|
50
|
+
expiresAt: 5000500000,
|
|
51
|
+
fetchedAt: 5000000000,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
const partialResultAction = {
|
|
55
|
+
...action,
|
|
56
|
+
payload: { id, title: 'hello' },
|
|
57
|
+
};
|
|
58
|
+
const iniState = initialState;
|
|
59
|
+
let newState = initialState;
|
|
60
|
+
it('should update state correctly', () => {
|
|
61
|
+
newState = reducer(iniState, action);
|
|
62
|
+
expect(newState).toMatchSnapshot();
|
|
63
|
+
});
|
|
64
|
+
it('should overwrite existing entity', () => {
|
|
65
|
+
const getEntity = (state: any): Article =>
|
|
66
|
+
state.entities[Article.key][`${Article.pk(action.payload)}`];
|
|
67
|
+
const prevEntity = getEntity(newState);
|
|
68
|
+
expect(prevEntity).toBeDefined();
|
|
69
|
+
const nextState = reducer(newState, action);
|
|
70
|
+
const nextEntity = getEntity(nextState);
|
|
71
|
+
expect(nextEntity).not.toBe(prevEntity);
|
|
72
|
+
expect(nextEntity).toBeDefined();
|
|
73
|
+
});
|
|
74
|
+
it('should merge partial entity with existing entity', () => {
|
|
75
|
+
const getEntity = (state: any): Article =>
|
|
76
|
+
state.entities[Article.key][`${Article.pk(action.payload)}`];
|
|
77
|
+
const prevEntity = getEntity(newState);
|
|
78
|
+
expect(prevEntity).toBeDefined();
|
|
79
|
+
const nextState = reducer(newState, partialResultAction);
|
|
80
|
+
const nextEntity = getEntity(nextState);
|
|
81
|
+
expect(nextEntity).not.toBe(prevEntity);
|
|
82
|
+
expect(nextEntity).toBeDefined();
|
|
83
|
+
|
|
84
|
+
expect(nextEntity.title).not.toBe(prevEntity.title);
|
|
85
|
+
expect(nextEntity.title).toBe(partialResultAction.payload.title);
|
|
86
|
+
|
|
87
|
+
expect(nextEntity.content).toBe(prevEntity.content);
|
|
88
|
+
expect(nextEntity.content).not.toBe(undefined);
|
|
89
|
+
|
|
90
|
+
expect(
|
|
91
|
+
nextState.entityMeta[Article.key][`${Article.pk(action.payload)}`],
|
|
92
|
+
).toBeDefined();
|
|
93
|
+
expect(
|
|
94
|
+
nextState.entityMeta[Article.key][`${Article.pk(action.payload)}`].date,
|
|
95
|
+
).toBe(action.meta.date);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should have the latest entity date', () => {
|
|
99
|
+
const localAction = {
|
|
100
|
+
...partialResultAction,
|
|
101
|
+
meta: {
|
|
102
|
+
...partialResultAction.meta,
|
|
103
|
+
expiresAt: partialResultAction.meta.expiresAt * 2,
|
|
104
|
+
date: partialResultAction.meta.date * 2,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
const getMeta = (state: any): { expiresAt: number } =>
|
|
108
|
+
state.entityMeta[Article.key][`${Article.pk(action.payload)}`];
|
|
109
|
+
const prevMeta = getMeta(newState);
|
|
110
|
+
expect(prevMeta).toBeDefined();
|
|
111
|
+
const nextState = reducer(newState, localAction);
|
|
112
|
+
const nextMeta = getMeta(nextState);
|
|
113
|
+
|
|
114
|
+
expect(nextMeta).toBeDefined();
|
|
115
|
+
expect(nextMeta.expiresAt).toBe(localAction.meta.expiresAt);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should use existing entity with older date', () => {
|
|
119
|
+
const localAction = {
|
|
120
|
+
...partialResultAction,
|
|
121
|
+
meta: {
|
|
122
|
+
...partialResultAction.meta,
|
|
123
|
+
date: partialResultAction.meta.date / 2,
|
|
124
|
+
expiresAt: partialResultAction.meta.expiresAt / 2,
|
|
125
|
+
fetchedAt: partialResultAction.meta.date / 2,
|
|
126
|
+
},
|
|
127
|
+
};
|
|
128
|
+
const getMeta = (state: any): { date: number } =>
|
|
129
|
+
state.entityMeta[Article.key][`${Article.pk(action.payload)}`];
|
|
130
|
+
const getEntity = (state: any): Article =>
|
|
131
|
+
state.entities[Article.key][`${Article.pk(action.payload)}`];
|
|
132
|
+
const prevEntity = getEntity(newState);
|
|
133
|
+
const prevMeta = getMeta(newState);
|
|
134
|
+
expect(prevMeta).toBeDefined();
|
|
135
|
+
const nextState = reducer(newState, localAction);
|
|
136
|
+
const nextMeta = getMeta(nextState);
|
|
137
|
+
const nextEntity = getEntity(nextState);
|
|
138
|
+
|
|
139
|
+
expect(prevEntity).toEqual(nextEntity);
|
|
140
|
+
|
|
141
|
+
expect(nextMeta).toBeDefined();
|
|
142
|
+
expect(nextMeta.date).toBe(action.meta.date);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('should use entity.expiresAt()', () => {
|
|
146
|
+
class ExpiresSoon extends Article {
|
|
147
|
+
static get key() {
|
|
148
|
+
return Article.key;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static expiresAt(
|
|
152
|
+
{ expiresAt, date }: { expiresAt: number; date: number },
|
|
153
|
+
input: any,
|
|
154
|
+
): number {
|
|
155
|
+
return input.content ? expiresAt : 0;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
const spy = jest.spyOn(ExpiresSoon, 'expiresAt');
|
|
159
|
+
const localAction = {
|
|
160
|
+
...partialResultAction,
|
|
161
|
+
meta: {
|
|
162
|
+
...partialResultAction.meta,
|
|
163
|
+
schema: ExpiresSoon,
|
|
164
|
+
date: partialResultAction.meta.date * 2,
|
|
165
|
+
expiresAt: partialResultAction.meta.expiresAt * 2,
|
|
166
|
+
fetchedAt: partialResultAction.meta.date * 2,
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
const getMeta = (state: any): { date: number; expiresAt: number } =>
|
|
170
|
+
state.entityMeta[ExpiresSoon.key][`${ExpiresSoon.pk(action.payload)}`];
|
|
171
|
+
const getEntity = (state: any): ExpiresSoon =>
|
|
172
|
+
state.entities[ExpiresSoon.key][`${ExpiresSoon.pk(action.payload)}`];
|
|
173
|
+
|
|
174
|
+
const prevEntity = getEntity(newState);
|
|
175
|
+
const prevMeta = getMeta(newState);
|
|
176
|
+
expect(prevMeta).toBeDefined();
|
|
177
|
+
const nextState = reducer(newState, localAction);
|
|
178
|
+
expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1);
|
|
179
|
+
|
|
180
|
+
const nextMeta = getMeta(nextState);
|
|
181
|
+
const nextEntity = getEntity(nextState);
|
|
182
|
+
|
|
183
|
+
expect(nextMeta).toBeDefined();
|
|
184
|
+
// our new expires was larger, but custom function returned 0, so we keep old expires
|
|
185
|
+
expect(nextMeta.expiresAt).toBe(prevMeta.expiresAt);
|
|
186
|
+
|
|
187
|
+
expect(nextEntity.title).toBe('hello');
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('mutate should never change results', () => {
|
|
192
|
+
const id = 20;
|
|
193
|
+
const payload = { id, title: 'hi', content: 'this is the content' };
|
|
194
|
+
const action: ReceiveAction = {
|
|
195
|
+
type: RECEIVE_TYPE,
|
|
196
|
+
payload,
|
|
197
|
+
meta: {
|
|
198
|
+
schema: Article,
|
|
199
|
+
key: ArticleResource.getList.key(payload),
|
|
200
|
+
date: 0,
|
|
201
|
+
expiresAt: 1000000000000,
|
|
202
|
+
},
|
|
203
|
+
};
|
|
204
|
+
const iniState = {
|
|
205
|
+
...initialState,
|
|
206
|
+
results: { abc: '5', [ArticleResource.getList.key(payload)]: `${id}` },
|
|
207
|
+
};
|
|
208
|
+
const newState = reducer(iniState, action);
|
|
209
|
+
expect(newState.results).toStrictEqual(iniState.results);
|
|
210
|
+
});
|
|
211
|
+
it('purge should delete entities', () => {
|
|
212
|
+
const id = 20;
|
|
213
|
+
const action: ReceiveAction = {
|
|
214
|
+
type: RECEIVE_TYPE,
|
|
215
|
+
payload: { id },
|
|
216
|
+
meta: {
|
|
217
|
+
schema: new schema.Delete(Article),
|
|
218
|
+
key: ArticleResource.delete.key({ id }),
|
|
219
|
+
date: 0,
|
|
220
|
+
expiresAt: 0,
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
const iniState: any = {
|
|
224
|
+
...initialState,
|
|
225
|
+
entities: {
|
|
226
|
+
[Article.key]: {
|
|
227
|
+
'10': Article.fromJS({ id: 10 }),
|
|
228
|
+
'20': Article.fromJS({ id: 20 }),
|
|
229
|
+
'25': Article.fromJS({ id: 25 }),
|
|
230
|
+
},
|
|
231
|
+
[PaginatedArticle.key]: {
|
|
232
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
233
|
+
},
|
|
234
|
+
'5': undefined,
|
|
235
|
+
},
|
|
236
|
+
results: { abc: '20' },
|
|
237
|
+
};
|
|
238
|
+
const newState = reducer(iniState, action);
|
|
239
|
+
expect(newState.results.abc).toBe(iniState.results.abc);
|
|
240
|
+
const expectedEntities = { ...iniState.entities[Article.key] };
|
|
241
|
+
expectedEntities['20'] = DELETED;
|
|
242
|
+
expect(newState.entities[Article.key]).toEqual(expectedEntities);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('updaters', () => {
|
|
246
|
+
describe('Update on get (pagination use case)', () => {
|
|
247
|
+
const shape = PaginatedArticleResource.getList;
|
|
248
|
+
function makeOptimisticAction(
|
|
249
|
+
payload: {
|
|
250
|
+
results: Partial<PaginatedArticle>[];
|
|
251
|
+
},
|
|
252
|
+
updaters: {
|
|
253
|
+
[key: string]: UpdateFunction<
|
|
254
|
+
(typeof shape)['schema'],
|
|
255
|
+
(typeof shape)['schema']
|
|
256
|
+
>;
|
|
257
|
+
},
|
|
258
|
+
) {
|
|
259
|
+
return {
|
|
260
|
+
type: RECEIVE_TYPE,
|
|
261
|
+
payload,
|
|
262
|
+
meta: {
|
|
263
|
+
schema: PaginatedArticleResource.getList.schema,
|
|
264
|
+
key: PaginatedArticleResource.getList.key({
|
|
265
|
+
cursor: 2,
|
|
266
|
+
}),
|
|
267
|
+
updaters,
|
|
268
|
+
date: 5000000000,
|
|
269
|
+
expiresAt: 5000500000,
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const insertAfterUpdater = <T extends { results?: string[] } | undefined>(
|
|
275
|
+
newPage: { results: string[] },
|
|
276
|
+
oldResults: T,
|
|
277
|
+
) => ({
|
|
278
|
+
...oldResults,
|
|
279
|
+
results: [...(oldResults?.results || []), ...newPage.results],
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
const insertBeforeUpdater = <
|
|
283
|
+
T extends { results?: string[] } | undefined,
|
|
284
|
+
>(
|
|
285
|
+
newPage: { results: string[] },
|
|
286
|
+
oldResults: T,
|
|
287
|
+
) => ({
|
|
288
|
+
...oldResults,
|
|
289
|
+
results: [...newPage.results, ...(oldResults?.results || [])],
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
const iniState: any = {
|
|
293
|
+
...initialState,
|
|
294
|
+
entities: {
|
|
295
|
+
[PaginatedArticle.key]: {
|
|
296
|
+
'10': PaginatedArticle.fromJS({ id: 10 }),
|
|
297
|
+
},
|
|
298
|
+
},
|
|
299
|
+
results: {
|
|
300
|
+
[PaginatedArticleResource.getList.key()]: { results: ['10'] },
|
|
301
|
+
},
|
|
302
|
+
};
|
|
303
|
+
|
|
304
|
+
it('should insert a new page of resources into a list request', () => {
|
|
305
|
+
const newState = reducer(
|
|
306
|
+
iniState,
|
|
307
|
+
makeOptimisticAction(
|
|
308
|
+
{ results: [{ id: 11 }, { id: 12 }] },
|
|
309
|
+
{
|
|
310
|
+
[PaginatedArticleResource.getList.key()]: insertAfterUpdater,
|
|
311
|
+
},
|
|
312
|
+
),
|
|
313
|
+
);
|
|
314
|
+
expect(
|
|
315
|
+
newState.results[PaginatedArticleResource.getList.key()],
|
|
316
|
+
).toStrictEqual({ results: ['10', '11', '12'] });
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it('should insert correctly into the beginning of the list request', () => {
|
|
320
|
+
const newState = reducer(
|
|
321
|
+
iniState,
|
|
322
|
+
makeOptimisticAction(
|
|
323
|
+
{ results: [{ id: 11 }, { id: 12 }] },
|
|
324
|
+
{
|
|
325
|
+
[PaginatedArticleResource.getList.key()]: insertBeforeUpdater,
|
|
326
|
+
},
|
|
327
|
+
),
|
|
328
|
+
);
|
|
329
|
+
expect(
|
|
330
|
+
newState.results[PaginatedArticleResource.getList.key()],
|
|
331
|
+
).toStrictEqual({ results: ['11', '12', '10'] });
|
|
332
|
+
});
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
describe('rpc update on create', () => {
|
|
336
|
+
const createEndpoint = ArticleResource.create;
|
|
337
|
+
function makeOptimisticAction(
|
|
338
|
+
payload: Partial<Article>,
|
|
339
|
+
updaters: {
|
|
340
|
+
[key: string]: UpdateFunction<
|
|
341
|
+
(typeof createEndpoint)['schema'],
|
|
342
|
+
(typeof Article)[]
|
|
343
|
+
>;
|
|
344
|
+
},
|
|
345
|
+
) {
|
|
346
|
+
return {
|
|
347
|
+
type: RECEIVE_TYPE,
|
|
348
|
+
payload,
|
|
349
|
+
meta: {
|
|
350
|
+
schema: Article,
|
|
351
|
+
key: ArticleResource.create.key({}),
|
|
352
|
+
updaters,
|
|
353
|
+
date: 0,
|
|
354
|
+
expiresAt: 100000000000,
|
|
355
|
+
},
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const insertAfterUpdater = (
|
|
360
|
+
result: string,
|
|
361
|
+
oldResults: string[] | undefined,
|
|
362
|
+
) => [...(oldResults || []), result];
|
|
363
|
+
|
|
364
|
+
const insertBeforeUpdater = (
|
|
365
|
+
result: string,
|
|
366
|
+
oldResults: string[] | undefined,
|
|
367
|
+
) => [result, ...(oldResults || [])];
|
|
368
|
+
|
|
369
|
+
const iniState: any = {
|
|
370
|
+
...initialState,
|
|
371
|
+
entities: {
|
|
372
|
+
[UrlArticle.key]: {
|
|
373
|
+
'10': UrlArticle.fromJS({ id: 10 }),
|
|
374
|
+
'21': UrlArticle.fromJS({ id: 21 }),
|
|
375
|
+
},
|
|
376
|
+
},
|
|
377
|
+
results: {
|
|
378
|
+
[ArticleResourceWithOtherListUrl.getList.key()]: ['10'],
|
|
379
|
+
[ArticleResourceWithOtherListUrl.otherList.key()]: ['21'],
|
|
380
|
+
},
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
it('it should run inserts for a simple resource after the existing list entities', () => {
|
|
384
|
+
const newState = reducer(
|
|
385
|
+
iniState,
|
|
386
|
+
makeOptimisticAction(
|
|
387
|
+
{ id: 11 },
|
|
388
|
+
{
|
|
389
|
+
[ArticleResource.getList.key()]: insertAfterUpdater,
|
|
390
|
+
},
|
|
391
|
+
),
|
|
392
|
+
);
|
|
393
|
+
expect(newState.results[ArticleResource.getList.key()]).toStrictEqual([
|
|
394
|
+
'10',
|
|
395
|
+
'11',
|
|
396
|
+
]);
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
it('it should run inserts for a simple resource before the existing list entities', () => {
|
|
400
|
+
const newState = reducer(
|
|
401
|
+
iniState,
|
|
402
|
+
makeOptimisticAction(
|
|
403
|
+
{ id: 11 },
|
|
404
|
+
{
|
|
405
|
+
[ArticleResource.getList.key()]: insertBeforeUpdater,
|
|
406
|
+
},
|
|
407
|
+
),
|
|
408
|
+
);
|
|
409
|
+
expect(newState.results[ArticleResource.getList.key()]).toStrictEqual([
|
|
410
|
+
'11',
|
|
411
|
+
'10',
|
|
412
|
+
]);
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
it('it runs inserts for multiple updaters', () => {
|
|
416
|
+
const newState = reducer(
|
|
417
|
+
iniState,
|
|
418
|
+
makeOptimisticAction(
|
|
419
|
+
{ id: 11 },
|
|
420
|
+
{
|
|
421
|
+
[ArticleResourceWithOtherListUrl.getList.key()]:
|
|
422
|
+
insertAfterUpdater,
|
|
423
|
+
[ArticleResourceWithOtherListUrl.otherList.key()]:
|
|
424
|
+
insertAfterUpdater,
|
|
425
|
+
},
|
|
426
|
+
),
|
|
427
|
+
);
|
|
428
|
+
expect(
|
|
429
|
+
newState.results[ArticleResourceWithOtherListUrl.getList.key()],
|
|
430
|
+
).toStrictEqual(['10', '11']);
|
|
431
|
+
expect(
|
|
432
|
+
newState.results[ArticleResourceWithOtherListUrl.otherList.key()],
|
|
433
|
+
).toStrictEqual(['21', '11']);
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
describe('endpoint.update', () => {
|
|
439
|
+
describe('Update on get (pagination use case)', () => {
|
|
440
|
+
const endpoint = PaginatedArticleResource.getList;
|
|
441
|
+
|
|
442
|
+
const iniState: any = {
|
|
443
|
+
...initialState,
|
|
444
|
+
entities: {
|
|
445
|
+
[PaginatedArticle.key]: {
|
|
446
|
+
'10': PaginatedArticle.fromJS({ id: 10 }),
|
|
447
|
+
},
|
|
448
|
+
},
|
|
449
|
+
results: {
|
|
450
|
+
[PaginatedArticleResource.getList.key({})]: { results: ['10'] },
|
|
451
|
+
},
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
it('should insert a new page of resources into a list request', () => {
|
|
455
|
+
const action = createReceive(
|
|
456
|
+
{ results: [{ id: 11 }, { id: 12 }] },
|
|
457
|
+
{
|
|
458
|
+
...endpoint,
|
|
459
|
+
key: endpoint.key({ cursor: 2 }),
|
|
460
|
+
update: (nextpage: { results: string[] }) => ({
|
|
461
|
+
[PaginatedArticleResource.getList.key({})]: (
|
|
462
|
+
existing: { results: string[] } = { results: [] },
|
|
463
|
+
) => ({
|
|
464
|
+
...existing,
|
|
465
|
+
results: [...existing.results, ...nextpage.results],
|
|
466
|
+
}),
|
|
467
|
+
}),
|
|
468
|
+
dataExpiryLength: 600000,
|
|
469
|
+
},
|
|
470
|
+
);
|
|
471
|
+
const newState = reducer(iniState, action);
|
|
472
|
+
expect(
|
|
473
|
+
newState.results[PaginatedArticleResource.getList.key({})],
|
|
474
|
+
).toStrictEqual({
|
|
475
|
+
results: ['10', '11', '12'],
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it('should insert correctly into the beginning of the list request', () => {
|
|
480
|
+
const newState = reducer(
|
|
481
|
+
iniState,
|
|
482
|
+
createReceive(
|
|
483
|
+
{ results: [{ id: 11 }, { id: 12 }] },
|
|
484
|
+
{
|
|
485
|
+
...endpoint,
|
|
486
|
+
key: endpoint.key({ cursor: 2 }),
|
|
487
|
+
update: (nextpage: { results: string[] }) => ({
|
|
488
|
+
[PaginatedArticleResource.getList.key({})]: (
|
|
489
|
+
existing: { results: string[] } = { results: [] },
|
|
490
|
+
) => ({
|
|
491
|
+
...existing,
|
|
492
|
+
results: [...nextpage.results, ...existing.results],
|
|
493
|
+
}),
|
|
494
|
+
}),
|
|
495
|
+
dataExpiryLength: 600000,
|
|
496
|
+
},
|
|
497
|
+
),
|
|
498
|
+
);
|
|
499
|
+
expect(
|
|
500
|
+
newState.results[PaginatedArticleResource.getList.key({})],
|
|
501
|
+
).toStrictEqual({
|
|
502
|
+
results: ['11', '12', '10'],
|
|
503
|
+
});
|
|
504
|
+
});
|
|
505
|
+
|
|
506
|
+
it('should account for args', () => {
|
|
507
|
+
const iniState: any = {
|
|
508
|
+
...initialState,
|
|
509
|
+
entities: {
|
|
510
|
+
[PaginatedArticle.key]: {
|
|
511
|
+
'10': PaginatedArticle.fromJS({ id: 10 }),
|
|
512
|
+
},
|
|
513
|
+
},
|
|
514
|
+
results: {
|
|
515
|
+
[PaginatedArticleResource.getList.key({ admin: true })]: {
|
|
516
|
+
results: ['10'],
|
|
517
|
+
},
|
|
518
|
+
},
|
|
519
|
+
};
|
|
520
|
+
const newState = reducer(
|
|
521
|
+
iniState,
|
|
522
|
+
createReceive(
|
|
523
|
+
{ results: [{ id: 11 }, { id: 12 }] },
|
|
524
|
+
{
|
|
525
|
+
...endpoint,
|
|
526
|
+
key: endpoint.key({ cursor: 2, admin: true }),
|
|
527
|
+
args: [{ cursor: 2, admin: true }],
|
|
528
|
+
update: (nextpage: { results: string[] }, { admin }) => ({
|
|
529
|
+
[PaginatedArticleResource.getList.key({ admin })]: (
|
|
530
|
+
existing: { results: string[] } = { results: [] },
|
|
531
|
+
) => ({
|
|
532
|
+
...existing,
|
|
533
|
+
results: [...nextpage.results, ...existing.results],
|
|
534
|
+
}),
|
|
535
|
+
}),
|
|
536
|
+
dataExpiryLength: 600000,
|
|
537
|
+
},
|
|
538
|
+
),
|
|
539
|
+
);
|
|
540
|
+
expect(
|
|
541
|
+
newState.results[
|
|
542
|
+
PaginatedArticleResource.getList.key({ admin: true })
|
|
543
|
+
],
|
|
544
|
+
).toStrictEqual({
|
|
545
|
+
results: ['11', '12', '10'],
|
|
546
|
+
});
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it('invalidates resources correctly', () => {
|
|
552
|
+
const id = 20;
|
|
553
|
+
const action: InvalidateAction = {
|
|
554
|
+
type: INVALIDATE_TYPE,
|
|
555
|
+
meta: {
|
|
556
|
+
key: id.toString(),
|
|
557
|
+
},
|
|
558
|
+
};
|
|
559
|
+
const iniState: any = {
|
|
560
|
+
...initialState,
|
|
561
|
+
entities: {
|
|
562
|
+
[Article.key]: {
|
|
563
|
+
'10': Article.fromJS({ id: 10 }),
|
|
564
|
+
'20': Article.fromJS({ id: 20 }),
|
|
565
|
+
'25': Article.fromJS({ id: 25 }),
|
|
566
|
+
},
|
|
567
|
+
[PaginatedArticle.key]: {
|
|
568
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
569
|
+
},
|
|
570
|
+
'5': undefined,
|
|
571
|
+
},
|
|
572
|
+
results: { abc: '20' },
|
|
573
|
+
meta: {
|
|
574
|
+
'20': {
|
|
575
|
+
expiresAt: 500,
|
|
576
|
+
},
|
|
577
|
+
'25': {
|
|
578
|
+
expiresAt: 1000,
|
|
579
|
+
},
|
|
580
|
+
},
|
|
581
|
+
};
|
|
582
|
+
const newState = reducer(iniState, action);
|
|
583
|
+
expect(newState.results).toEqual(iniState.results);
|
|
584
|
+
expect(newState.entities).toBe(iniState.entities);
|
|
585
|
+
const expectedMeta = { ...iniState.meta };
|
|
586
|
+
expectedMeta['20'] = { expiresAt: 0, invalidated: true };
|
|
587
|
+
expect(newState.meta).toEqual(expectedMeta);
|
|
588
|
+
});
|
|
589
|
+
it('should set error in meta for "receive"', () => {
|
|
590
|
+
const id = 20;
|
|
591
|
+
const error = new Error('hi');
|
|
592
|
+
const action: ReceiveAction = {
|
|
593
|
+
type: RECEIVE_TYPE,
|
|
594
|
+
payload: error,
|
|
595
|
+
meta: {
|
|
596
|
+
schema: Article,
|
|
597
|
+
key: ArticleResource.get.key({ id }),
|
|
598
|
+
date: 5000000000,
|
|
599
|
+
expiresAt: 5000500000,
|
|
600
|
+
},
|
|
601
|
+
error: true,
|
|
602
|
+
};
|
|
603
|
+
const iniState = initialState;
|
|
604
|
+
const newState = reducer(iniState, action);
|
|
605
|
+
expect(newState).toMatchSnapshot();
|
|
606
|
+
});
|
|
607
|
+
it('should not modify state on error for "rpc"', () => {
|
|
608
|
+
const id = 20;
|
|
609
|
+
const error = new Error('hi');
|
|
610
|
+
const action: ReceiveAction = {
|
|
611
|
+
type: RECEIVE_TYPE,
|
|
612
|
+
payload: error,
|
|
613
|
+
meta: {
|
|
614
|
+
schema: Article,
|
|
615
|
+
key: ArticleResource.get.key({ id }),
|
|
616
|
+
date: 0,
|
|
617
|
+
expiresAt: 10000000000000000000,
|
|
618
|
+
},
|
|
619
|
+
error: true,
|
|
620
|
+
};
|
|
621
|
+
const iniState = initialState;
|
|
622
|
+
const newState = reducer(iniState, action);
|
|
623
|
+
// ignore meta for this check
|
|
624
|
+
expect({ ...newState, meta: {} }).toEqual(iniState);
|
|
625
|
+
});
|
|
626
|
+
it('should not delete on error for "purge"', () => {
|
|
627
|
+
const id = 20;
|
|
628
|
+
const error = new Error('hi');
|
|
629
|
+
const action: ReceiveAction = {
|
|
630
|
+
type: RECEIVE_TYPE,
|
|
631
|
+
payload: error,
|
|
632
|
+
meta: {
|
|
633
|
+
schema: new schema.Delete(Article),
|
|
634
|
+
key: ArticleResource.delete.key({ id }),
|
|
635
|
+
date: 0,
|
|
636
|
+
expiresAt: 0,
|
|
637
|
+
},
|
|
638
|
+
error: true,
|
|
639
|
+
};
|
|
640
|
+
const iniState = {
|
|
641
|
+
...initialState,
|
|
642
|
+
entities: {
|
|
643
|
+
[Article.key]: {
|
|
644
|
+
[id]: Article.fromJS({}),
|
|
645
|
+
},
|
|
646
|
+
},
|
|
647
|
+
results: {
|
|
648
|
+
[ArticleResource.get.url({ id })]: id,
|
|
649
|
+
},
|
|
650
|
+
};
|
|
651
|
+
const newState = reducer(iniState, action);
|
|
652
|
+
expect(newState.entities).toBe(iniState.entities);
|
|
653
|
+
});
|
|
654
|
+
it('rest-hooks/fetch should console.warn()', () => {
|
|
655
|
+
const warnspy = jest.spyOn(global.console, 'warn');
|
|
656
|
+
try {
|
|
657
|
+
const action: FetchAction = {
|
|
658
|
+
type: FETCH_TYPE,
|
|
659
|
+
payload: () => new Promise<any>(() => null),
|
|
660
|
+
meta: {
|
|
661
|
+
schema: Article,
|
|
662
|
+
key: ArticleResource.get.url({ id: 5 }),
|
|
663
|
+
type: 'read' as const,
|
|
664
|
+
throttle: true,
|
|
665
|
+
reject: (v: any) => null,
|
|
666
|
+
resolve: (v: any) => null,
|
|
667
|
+
promise: new Promise((v: any) => null),
|
|
668
|
+
createdAt: 0,
|
|
669
|
+
},
|
|
670
|
+
};
|
|
671
|
+
const iniState = {
|
|
672
|
+
...initialState,
|
|
673
|
+
results: { abc: '5' },
|
|
674
|
+
};
|
|
675
|
+
const newState = reducer(iniState, action);
|
|
676
|
+
expect(newState).toBe(iniState);
|
|
677
|
+
expect(warnspy.mock.calls.length).toBe(2);
|
|
678
|
+
} finally {
|
|
679
|
+
warnspy.mockRestore();
|
|
680
|
+
}
|
|
681
|
+
});
|
|
682
|
+
it('other types should do nothing', () => {
|
|
683
|
+
const action: any = {
|
|
684
|
+
type: 'whatever',
|
|
685
|
+
};
|
|
686
|
+
const iniState = {
|
|
687
|
+
...initialState,
|
|
688
|
+
results: { abc: '5' },
|
|
689
|
+
};
|
|
690
|
+
const newState = reducer(iniState, action);
|
|
691
|
+
expect(newState).toBe(iniState);
|
|
692
|
+
});
|
|
693
|
+
describe('RESET', () => {
|
|
694
|
+
let warnspy: jest.SpyInstance;
|
|
695
|
+
beforeEach(() => {
|
|
696
|
+
warnspy = jest.spyOn(global.console, 'warn');
|
|
697
|
+
});
|
|
698
|
+
afterEach(() => {
|
|
699
|
+
warnspy.mockRestore();
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
it('reset should delete all entries', () => {
|
|
703
|
+
const action: ResetAction = {
|
|
704
|
+
type: RESET_TYPE,
|
|
705
|
+
date: Date.now(),
|
|
706
|
+
};
|
|
707
|
+
const iniState: any = {
|
|
708
|
+
...initialState,
|
|
709
|
+
entities: {
|
|
710
|
+
[Article.key]: {
|
|
711
|
+
'10': Article.fromJS({ id: 10 }),
|
|
712
|
+
'20': Article.fromJS({ id: 20 }),
|
|
713
|
+
'25': Article.fromJS({ id: 25 }),
|
|
714
|
+
},
|
|
715
|
+
[PaginatedArticle.key]: {
|
|
716
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
717
|
+
},
|
|
718
|
+
'5': undefined,
|
|
719
|
+
},
|
|
720
|
+
results: { abc: '20' },
|
|
721
|
+
};
|
|
722
|
+
const newState = reducer(iniState, action);
|
|
723
|
+
expect(newState.results).toEqual({});
|
|
724
|
+
expect(newState.meta).toEqual({});
|
|
725
|
+
expect(newState.entities).toEqual({});
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
// TODO(breaking): Remove once Date support is removed from action
|
|
729
|
+
it('reset should delete all entries (legacy format)', () => {
|
|
730
|
+
const action: ResetAction = {
|
|
731
|
+
type: RESET_TYPE,
|
|
732
|
+
date: new Date(),
|
|
733
|
+
};
|
|
734
|
+
const iniState: any = {
|
|
735
|
+
...initialState,
|
|
736
|
+
entities: {
|
|
737
|
+
[Article.key]: {
|
|
738
|
+
'10': Article.fromJS({ id: 10 }),
|
|
739
|
+
'20': Article.fromJS({ id: 20 }),
|
|
740
|
+
'25': Article.fromJS({ id: 25 }),
|
|
741
|
+
},
|
|
742
|
+
[PaginatedArticle.key]: {
|
|
743
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
744
|
+
},
|
|
745
|
+
'5': undefined,
|
|
746
|
+
},
|
|
747
|
+
results: { abc: '20' },
|
|
748
|
+
};
|
|
749
|
+
const newState = reducer(iniState, action);
|
|
750
|
+
expect(newState.results).toEqual({});
|
|
751
|
+
expect(newState.meta).toEqual({});
|
|
752
|
+
expect(newState.entities).toEqual({});
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
it('reset without date should warn about deprecation', () => {
|
|
756
|
+
const action: any = {
|
|
757
|
+
type: RESET_TYPE,
|
|
758
|
+
};
|
|
759
|
+
const iniState: any = {
|
|
760
|
+
...initialState,
|
|
761
|
+
entities: {
|
|
762
|
+
[Article.key]: {
|
|
763
|
+
'10': Article.fromJS({ id: 10 }),
|
|
764
|
+
'20': Article.fromJS({ id: 20 }),
|
|
765
|
+
'25': Article.fromJS({ id: 25 }),
|
|
766
|
+
},
|
|
767
|
+
[PaginatedArticle.key]: {
|
|
768
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
769
|
+
},
|
|
770
|
+
'5': undefined,
|
|
771
|
+
},
|
|
772
|
+
results: { abc: '20' },
|
|
773
|
+
};
|
|
774
|
+
const newState = reducer(iniState, action);
|
|
775
|
+
expect(newState.results).toEqual({});
|
|
776
|
+
expect(newState.meta).toEqual({});
|
|
777
|
+
expect(newState.entities).toEqual({});
|
|
778
|
+
expect(newState.lastReset).toBeDefined();
|
|
779
|
+
expect(newState.lastReset).toBeGreaterThan(0);
|
|
780
|
+
expect(warnspy.mock.calls).toMatchInlineSnapshot(`
|
|
781
|
+
[
|
|
782
|
+
[
|
|
783
|
+
"rest-hooks/reset sent without 'date' member. This is deprecated. Please use createReset() action creator to ensure correct action shape.",
|
|
784
|
+
],
|
|
785
|
+
]
|
|
786
|
+
`);
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
|
|
790
|
+
describe('GC action', () => {
|
|
791
|
+
let iniState: State<unknown>;
|
|
792
|
+
|
|
793
|
+
beforeEach(() => {
|
|
794
|
+
iniState = {
|
|
795
|
+
...initialState,
|
|
796
|
+
entities: {
|
|
797
|
+
[Article.key]: {
|
|
798
|
+
'10': Article.fromJS({ id: 10 }),
|
|
799
|
+
'20': Article.fromJS({ id: 20 }),
|
|
800
|
+
'25': Article.fromJS({ id: 25 }),
|
|
801
|
+
'250': Article.fromJS({ id: 250 }),
|
|
802
|
+
},
|
|
803
|
+
[PaginatedArticle.key]: {
|
|
804
|
+
hi: PaginatedArticle.fromJS({ id: 5 }),
|
|
805
|
+
},
|
|
806
|
+
'5': undefined,
|
|
807
|
+
},
|
|
808
|
+
entityMeta: {
|
|
809
|
+
[Article.key]: {
|
|
810
|
+
'10': { date: 0, expiresAt: 10000, fetchedAt: 0 },
|
|
811
|
+
'20': { date: 0, expiresAt: 10000, fetchedAt: 0 },
|
|
812
|
+
'25': { date: 0, expiresAt: 10000, fetchedAt: 0 },
|
|
813
|
+
'250': { date: 0, expiresAt: 10000, fetchedAt: 0 },
|
|
814
|
+
},
|
|
815
|
+
},
|
|
816
|
+
results: { abc: '20' },
|
|
817
|
+
};
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
it('empty targets should do nothing', () => {
|
|
821
|
+
const action: GCAction = {
|
|
822
|
+
type: GC_TYPE,
|
|
823
|
+
entities: [],
|
|
824
|
+
results: [],
|
|
825
|
+
};
|
|
826
|
+
|
|
827
|
+
const newState = reducer(iniState, action);
|
|
828
|
+
expect(newState).toBe(iniState);
|
|
829
|
+
expect(Object.keys(newState.entities[Article.key] ?? {}).length).toBe(4);
|
|
830
|
+
expect(Object.keys(newState.results).length).toBe(1);
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
it('empty deleting entities should work', () => {
|
|
834
|
+
const action: GCAction = {
|
|
835
|
+
type: GC_TYPE,
|
|
836
|
+
entities: [
|
|
837
|
+
[Article.key, '10'],
|
|
838
|
+
[Article.key, '250'],
|
|
839
|
+
],
|
|
840
|
+
results: ['abc'],
|
|
841
|
+
};
|
|
842
|
+
|
|
843
|
+
const newState = reducer(iniState, action);
|
|
844
|
+
expect(newState).toBe(iniState);
|
|
845
|
+
expect(Object.keys(newState.entities[Article.key] ?? {}).length).toBe(2);
|
|
846
|
+
expect(Object.keys(newState.entityMeta[Article.key] ?? {}).length).toBe(
|
|
847
|
+
2,
|
|
848
|
+
);
|
|
849
|
+
expect(Object.keys(newState.results).length).toBe(0);
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
it('empty deleting nonexistant things should passthrough', () => {
|
|
853
|
+
const action: GCAction = {
|
|
854
|
+
type: GC_TYPE,
|
|
855
|
+
entities: [
|
|
856
|
+
[Article.key, '100000000'],
|
|
857
|
+
['sillythings', '10'],
|
|
858
|
+
],
|
|
859
|
+
results: [],
|
|
860
|
+
};
|
|
861
|
+
|
|
862
|
+
const newState = reducer(iniState, action);
|
|
863
|
+
expect(newState).toBe(iniState);
|
|
864
|
+
expect(Object.keys(newState.entities[Article.key] ?? {}).length).toBe(4);
|
|
865
|
+
expect(Object.keys(newState.results).length).toBe(1);
|
|
866
|
+
});
|
|
867
|
+
});
|
|
868
|
+
});
|