-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
create_remote_operation.test.ts
143 lines (111 loc) · 4.6 KB
/
create_remote_operation.test.ts
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
import { watchRemoteOperation } from '@farfetched/test-utils';
import { allSettled, createStore, fork } from 'effector';
import { describe, test, expect, vi } from 'vitest';
import { unknownContract } from '../../contract/unknown_contract';
import { createDefer } from '../../libs/lohyphen';
import { createRemoteOperation } from '../create_remote_operation';
describe('createRemoteOperation, disable in-flight', () => {
const defaultConfig = {
name: 'test',
mapData: (v: any) => v,
meta: null,
kind: 'test',
contract: unknownContract,
};
test('emit skip with success in handler', async () => {
const defer = createDefer();
const $enabled = createStore(true);
const operation = createRemoteOperation({
...defaultConfig,
enabled: $enabled,
});
operation.__.executeFx.use(() => defer.promise);
const scope = fork();
const { listeners } = watchRemoteOperation(operation, scope);
allSettled(operation.start, { scope, params: 1 });
allSettled($enabled, { scope, params: false });
defer.resolve({});
await allSettled(scope);
expect(listeners.onSkip).toBeCalledTimes(1);
expect(listeners.onSkip).toBeCalledWith(
expect.objectContaining({ params: 1 })
);
expect(listeners.onSuccess).not.toBeCalled();
});
test('emit skip with error in handler', async () => {
const defer = createDefer();
const $enabled = createStore(true);
const operation = createRemoteOperation({
...defaultConfig,
enabled: $enabled,
});
operation.__.executeFx.use(() => defer.promise);
const scope = fork();
const { listeners } = watchRemoteOperation(operation, scope);
allSettled(operation.start, { scope, params: 1 });
allSettled($enabled, { scope, params: false });
defer.reject({});
await allSettled(scope);
expect(listeners.onSkip).toBeCalledTimes(1);
expect(listeners.onSkip).toBeCalledWith(
expect.objectContaining({ params: 1 })
);
expect(listeners.onFailure).not.toBeCalled();
});
test('$status changes on stages', async () => {
const executorFirstDefer = createDefer();
const executorSecondDefer = createDefer();
const operation = createRemoteOperation({
...defaultConfig,
});
const scope = fork({
handlers: [
[
operation.__.executeFx,
vi
.fn()
.mockImplementationOnce(() => executorFirstDefer.promise)
.mockImplementationOnce(() => executorSecondDefer.promise),
],
],
});
expect(scope.getState(operation.$status)).toBe('initial');
expect(scope.getState(operation.$idle)).toBeTruthy();
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();
// do not await
allSettled(operation.start, { scope, params: 42 });
expect(scope.getState(operation.$status)).toBe('pending');
expect(scope.getState(operation.$idle)).toBeFalsy();
expect(scope.getState(operation.$pending)).toBeTruthy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();
executorFirstDefer.resolve('result');
await allSettled(scope);
expect(scope.getState(operation.$status)).toBe('done');
expect(scope.getState(operation.$idle)).toBeFalsy();
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeTruthy();
expect(scope.getState(operation.$finished)).toBeTruthy();
// do not await
allSettled(operation.start, { scope, params: 42 });
expect(scope.getState(operation.$status)).toBe('pending');
expect(scope.getState(operation.$idle)).toBeFalsy();
expect(scope.getState(operation.$pending)).toBeTruthy();
expect(scope.getState(operation.$failed)).toBeFalsy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeFalsy();
executorSecondDefer.reject(new Error('error'));
await allSettled(scope);
expect(scope.getState(operation.$status)).toBe('fail');
expect(scope.getState(operation.$idle)).toBeFalsy();
expect(scope.getState(operation.$pending)).toBeFalsy();
expect(scope.getState(operation.$failed)).toBeTruthy();
expect(scope.getState(operation.$succeeded)).toBeFalsy();
expect(scope.getState(operation.$finished)).toBeTruthy();
});
});