-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathactionUtils.test.ts
More file actions
173 lines (150 loc) · 4.26 KB
/
Copy pathactionUtils.test.ts
File metadata and controls
173 lines (150 loc) · 4.26 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { describe, test, expect, vi, afterEach, afterAll } from 'vitest';
import { defineAction } from '../defineAction';
import { checkActionAllowed, runAction } from './actionUtils';
describe('Test Action Utils', () => {
describe('Test checkActionAllowed util', () => {
const baseAction = {
id: 'test-action',
title: 'Test Action',
run: () => {
console.log('test ran');
},
};
test('should allow action if no condition defined', () => {
const action = defineAction(baseAction);
const actionsContext = {
root: {},
dynamic: {},
};
const isAllowed = checkActionAllowed(action, actionsContext);
expect(isAllowed).toBe(true);
});
test('should allow action based on root context', () => {
const action = defineAction({
...baseAction,
cond: ({ rootContext }) => {
return rootContext.profile === 'work';
},
});
const failingActionsContext = {
root: {
profile: 'personal',
},
dynamic: {},
};
expect(checkActionAllowed(action, failingActionsContext), 'action is not allowed').toBe(
false
);
const passingActionsContext = {
root: {
profile: 'work',
},
dynamic: {},
};
expect(checkActionAllowed(action, passingActionsContext), 'action is allowed').toBe(true);
});
test('should allow action based on dynamic context', () => {
const action = defineAction({
...baseAction,
cond: ({ dynamicContext }) => {
return dynamicContext.isActive === true;
},
});
const failingActionsContext = {
root: {},
dynamic: {
'test-action': {
isActive: false,
},
'other-action': {
isActive: true,
},
},
};
expect(checkActionAllowed(action, failingActionsContext), 'action is not allowed').toBe(
false
);
const passingActionsContext = {
root: {},
dynamic: {
'test-action': {
isActive: true,
},
'other-action': {
isActive: false,
},
},
};
expect(checkActionAllowed(action, passingActionsContext), 'action is allowed').toBe(true);
});
});
describe('Test runAction util', () => {
const runMock = vi.fn();
const selectParentActionMock = vi.fn();
const closePaletteMock = vi.fn();
const openPaletteMock = vi.fn();
const baseAction = {
id: 'test-action',
title: 'Test Action',
run: runMock,
};
const baseStoreMethods = {
selectParentAction: selectParentActionMock,
closePalette: closePaletteMock,
openPalette: openPaletteMock,
};
afterEach(() => {
runMock.mockClear();
selectParentActionMock.mockClear();
closePaletteMock.mockClear();
});
afterAll(() => {
runMock.mockReset();
selectParentActionMock.mockReset();
closePaletteMock.mockReset();
});
test('should trigger run callback of the action correctly', () => {
const action = defineAction(baseAction);
const actionsContext = {
root: {
profile: 'work',
},
dynamic: {
'test-action': {
isActive: true,
},
'other-action': {
isActive: false,
},
},
};
runAction(action, actionsContext, baseStoreMethods);
expect(runMock).toBeCalledWith({
actionId: 'test-action',
rootContext: {
profile: 'work',
},
dynamicContext: {
isActive: true,
},
});
expect(selectParentActionMock).not.toBeCalled();
expect(closePaletteMock).toBeCalled();
});
test('should setup nested actions correctly', () => {
const action = defineAction({
...baseAction,
id: 'parent-test-action',
run: undefined,
});
const actionsContext = {
root: {},
dynamic: {},
};
runAction(action, actionsContext, baseStoreMethods);
expect(selectParentActionMock).toBeCalledWith('parent-test-action');
expect(runMock).not.toBeCalled();
expect(closePaletteMock).not.toBeCalled();
});
});
});