@flusys/nestjs-task-manager 6.0.2 → 6.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/cjs/controllers/task-board.controller.spec.js +132 -0
- package/cjs/controllers/task-comment.controller.spec.js +86 -0
- package/cjs/controllers/task-label.controller.spec.js +88 -0
- package/cjs/controllers/task-list.controller.spec.js +112 -0
- package/cjs/controllers/task.controller.spec.js +131 -0
- package/cjs/docs/task-manager-swagger.config.spec.js +28 -0
- package/cjs/entities/index.spec.js +30 -0
- package/cjs/gateways/task-board.gateway.spec.js +243 -0
- package/cjs/modules/task-manager.module.spec.js +144 -0
- package/cjs/services/task-activity.service.spec.js +125 -0
- package/cjs/services/task-board.service.spec.js +429 -0
- package/cjs/services/task-comment.service.spec.js +160 -0
- package/cjs/services/task-label.service.spec.js +138 -0
- package/cjs/services/task-list.service.spec.js +214 -0
- package/cjs/services/task-manager-config.service.spec.js +98 -0
- package/cjs/services/task-manager-datasource.provider.spec.js +131 -0
- package/cjs/services/task.service.spec.js +610 -0
- package/fesm/controllers/task-board.controller.spec.js +128 -0
- package/fesm/controllers/task-comment.controller.spec.js +82 -0
- package/fesm/controllers/task-label.controller.spec.js +84 -0
- package/fesm/controllers/task-list.controller.spec.js +108 -0
- package/fesm/controllers/task.controller.spec.js +127 -0
- package/fesm/docs/task-manager-swagger.config.spec.js +24 -0
- package/fesm/entities/index.spec.js +26 -0
- package/fesm/gateways/task-board.gateway.spec.js +239 -0
- package/fesm/modules/task-manager.module.spec.js +140 -0
- package/fesm/services/task-activity.service.spec.js +121 -0
- package/fesm/services/task-board.service.spec.js +425 -0
- package/fesm/services/task-comment.service.spec.js +156 -0
- package/fesm/services/task-label.service.spec.js +134 -0
- package/fesm/services/task-list.service.spec.js +210 -0
- package/fesm/services/task-manager-config.service.spec.js +94 -0
- package/fesm/services/task-manager-datasource.provider.spec.js +127 -0
- package/fesm/services/task.service.spec.js +606 -0
- package/package.json +3 -3
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { TaskBoardGateway } from './task-board.gateway';
|
|
2
|
+
function buildSocket(overrides = {}) {
|
|
3
|
+
return {
|
|
4
|
+
id: overrides.id ?? 'socket-1',
|
|
5
|
+
handshake: {
|
|
6
|
+
auth: overrides.authToken !== undefined ? {
|
|
7
|
+
token: overrides.authToken
|
|
8
|
+
} : {},
|
|
9
|
+
query: overrides.queryToken !== undefined ? {
|
|
10
|
+
token: overrides.queryToken
|
|
11
|
+
} : {},
|
|
12
|
+
headers: overrides.authHeader !== undefined ? {
|
|
13
|
+
authorization: overrides.authHeader
|
|
14
|
+
} : {}
|
|
15
|
+
},
|
|
16
|
+
data: {},
|
|
17
|
+
join: jest.fn(),
|
|
18
|
+
leave: jest.fn(),
|
|
19
|
+
disconnect: jest.fn()
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
describe('TaskBoardGateway', ()=>{
|
|
23
|
+
let gateway;
|
|
24
|
+
let mockConfigService;
|
|
25
|
+
let mockJwtService;
|
|
26
|
+
let mockServer;
|
|
27
|
+
beforeEach(()=>{
|
|
28
|
+
mockConfigService = {
|
|
29
|
+
getJwtSecret: jest.fn().mockReturnValue('test-secret')
|
|
30
|
+
};
|
|
31
|
+
mockJwtService = {
|
|
32
|
+
verify: jest.fn()
|
|
33
|
+
};
|
|
34
|
+
gateway = new TaskBoardGateway(mockConfigService, mockJwtService);
|
|
35
|
+
mockServer = {
|
|
36
|
+
to: jest.fn().mockReturnThis(),
|
|
37
|
+
emit: jest.fn()
|
|
38
|
+
};
|
|
39
|
+
gateway.server = mockServer;
|
|
40
|
+
});
|
|
41
|
+
describe('handleConnection', ()=>{
|
|
42
|
+
it('authenticates via handshake.auth.token, joins the user room and tracks the socket', async ()=>{
|
|
43
|
+
const socket = buildSocket({
|
|
44
|
+
id: 'socket-1',
|
|
45
|
+
authToken: 'valid-token'
|
|
46
|
+
});
|
|
47
|
+
mockJwtService.verify.mockReturnValue({
|
|
48
|
+
id: 'user-1',
|
|
49
|
+
companyId: 'company-1'
|
|
50
|
+
});
|
|
51
|
+
await gateway.handleConnection(socket);
|
|
52
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('valid-token', {
|
|
53
|
+
secret: 'test-secret',
|
|
54
|
+
algorithms: [
|
|
55
|
+
'HS256'
|
|
56
|
+
]
|
|
57
|
+
});
|
|
58
|
+
expect(socket.data.userId).toBe('user-1');
|
|
59
|
+
expect(socket.data.companyId).toBe('company-1');
|
|
60
|
+
expect(socket.join).toHaveBeenCalledWith('user:user-1');
|
|
61
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
62
|
+
expect(socket.disconnect).not.toHaveBeenCalled();
|
|
63
|
+
});
|
|
64
|
+
it('falls back to handshake.query.token when auth.token is absent', async ()=>{
|
|
65
|
+
const socket = buildSocket({
|
|
66
|
+
queryToken: 'query-token'
|
|
67
|
+
});
|
|
68
|
+
mockJwtService.verify.mockReturnValue({
|
|
69
|
+
id: 'user-2'
|
|
70
|
+
});
|
|
71
|
+
await gateway.handleConnection(socket);
|
|
72
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('query-token', expect.anything());
|
|
73
|
+
expect(socket.data.userId).toBe('user-2');
|
|
74
|
+
});
|
|
75
|
+
it('falls back to the Authorization header (Bearer token) when other sources are absent', async ()=>{
|
|
76
|
+
const socket = buildSocket({
|
|
77
|
+
authHeader: 'Bearer header-token'
|
|
78
|
+
});
|
|
79
|
+
mockJwtService.verify.mockReturnValue({
|
|
80
|
+
id: 'user-3'
|
|
81
|
+
});
|
|
82
|
+
await gateway.handleConnection(socket);
|
|
83
|
+
expect(mockJwtService.verify).toHaveBeenCalledWith('header-token', expect.anything());
|
|
84
|
+
expect(socket.data.userId).toBe('user-3');
|
|
85
|
+
});
|
|
86
|
+
it('disconnects the socket when no token is present on the handshake', async ()=>{
|
|
87
|
+
const socket = buildSocket();
|
|
88
|
+
await gateway.handleConnection(socket);
|
|
89
|
+
expect(socket.disconnect).toHaveBeenCalled();
|
|
90
|
+
expect(mockJwtService.verify).not.toHaveBeenCalled();
|
|
91
|
+
});
|
|
92
|
+
it('disconnects the socket when the JWT secret is not configured', async ()=>{
|
|
93
|
+
mockConfigService.getJwtSecret.mockReturnValue(undefined);
|
|
94
|
+
const socket = buildSocket({
|
|
95
|
+
authToken: 'some-token'
|
|
96
|
+
});
|
|
97
|
+
await gateway.handleConnection(socket);
|
|
98
|
+
expect(socket.disconnect).toHaveBeenCalled();
|
|
99
|
+
});
|
|
100
|
+
it('disconnects the socket when token verification throws (invalid/expired token)', async ()=>{
|
|
101
|
+
const socket = buildSocket({
|
|
102
|
+
authToken: 'garbage-token'
|
|
103
|
+
});
|
|
104
|
+
mockJwtService.verify.mockImplementation(()=>{
|
|
105
|
+
throw new Error('invalid signature');
|
|
106
|
+
});
|
|
107
|
+
await gateway.handleConnection(socket);
|
|
108
|
+
expect(socket.disconnect).toHaveBeenCalled();
|
|
109
|
+
});
|
|
110
|
+
it('tracks multiple concurrent sockets for the same authenticated user', async ()=>{
|
|
111
|
+
const socketA = buildSocket({
|
|
112
|
+
id: 'socket-a',
|
|
113
|
+
authToken: 'token'
|
|
114
|
+
});
|
|
115
|
+
const socketB = buildSocket({
|
|
116
|
+
id: 'socket-b',
|
|
117
|
+
authToken: 'token'
|
|
118
|
+
});
|
|
119
|
+
mockJwtService.verify.mockReturnValue({
|
|
120
|
+
id: 'user-multi'
|
|
121
|
+
});
|
|
122
|
+
await gateway.handleConnection(socketA);
|
|
123
|
+
await gateway.handleConnection(socketB);
|
|
124
|
+
expect(gateway.isUserOnline('user-multi')).toBe(true);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('handleDisconnect', ()=>{
|
|
128
|
+
it('removes the socket from the connected-users map and marks the user offline once all sockets are gone', async ()=>{
|
|
129
|
+
const socket = buildSocket({
|
|
130
|
+
id: 'socket-1',
|
|
131
|
+
authToken: 'valid-token'
|
|
132
|
+
});
|
|
133
|
+
mockJwtService.verify.mockReturnValue({
|
|
134
|
+
id: 'user-1'
|
|
135
|
+
});
|
|
136
|
+
await gateway.handleConnection(socket);
|
|
137
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
138
|
+
gateway.handleDisconnect(socket);
|
|
139
|
+
expect(gateway.isUserOnline('user-1')).toBe(false);
|
|
140
|
+
});
|
|
141
|
+
it('keeps the user marked online while other sockets for that user remain connected', async ()=>{
|
|
142
|
+
const socketA = buildSocket({
|
|
143
|
+
id: 'socket-a',
|
|
144
|
+
authToken: 'token'
|
|
145
|
+
});
|
|
146
|
+
const socketB = buildSocket({
|
|
147
|
+
id: 'socket-b',
|
|
148
|
+
authToken: 'token'
|
|
149
|
+
});
|
|
150
|
+
mockJwtService.verify.mockReturnValue({
|
|
151
|
+
id: 'user-1'
|
|
152
|
+
});
|
|
153
|
+
await gateway.handleConnection(socketA);
|
|
154
|
+
await gateway.handleConnection(socketB);
|
|
155
|
+
gateway.handleDisconnect(socketA);
|
|
156
|
+
expect(gateway.isUserOnline('user-1')).toBe(true);
|
|
157
|
+
});
|
|
158
|
+
it('does nothing when the disconnecting socket has no tracked userId', ()=>{
|
|
159
|
+
const socket = buildSocket({
|
|
160
|
+
id: 'never-connected'
|
|
161
|
+
});
|
|
162
|
+
expect(()=>gateway.handleDisconnect(socket)).not.toThrow();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe('handleJoinBoard / handleLeaveBoard', ()=>{
|
|
166
|
+
it('joins the board room when a boardId is provided', ()=>{
|
|
167
|
+
const socket = buildSocket();
|
|
168
|
+
gateway.handleJoinBoard(socket, {
|
|
169
|
+
boardId: 'board-1'
|
|
170
|
+
});
|
|
171
|
+
expect(socket.join).toHaveBeenCalledWith('board:board-1');
|
|
172
|
+
});
|
|
173
|
+
it('does not attempt to join a room when boardId is missing', ()=>{
|
|
174
|
+
const socket = buildSocket();
|
|
175
|
+
gateway.handleJoinBoard(socket, {});
|
|
176
|
+
expect(socket.join).not.toHaveBeenCalled();
|
|
177
|
+
});
|
|
178
|
+
it('leaves the board room when a boardId is provided', ()=>{
|
|
179
|
+
const socket = buildSocket();
|
|
180
|
+
gateway.handleLeaveBoard(socket, {
|
|
181
|
+
boardId: 'board-1'
|
|
182
|
+
});
|
|
183
|
+
expect(socket.leave).toHaveBeenCalledWith('board:board-1');
|
|
184
|
+
});
|
|
185
|
+
it('does not attempt to leave a room when boardId is missing', ()=>{
|
|
186
|
+
const socket = buildSocket();
|
|
187
|
+
gateway.handleLeaveBoard(socket, {});
|
|
188
|
+
expect(socket.leave).not.toHaveBeenCalled();
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
describe('emitToBoardRoom', ()=>{
|
|
192
|
+
it('broadcasts the event to the board room', ()=>{
|
|
193
|
+
gateway.emitToBoardRoom('board-1', {
|
|
194
|
+
type: 'task:moved',
|
|
195
|
+
boardId: 'board-1',
|
|
196
|
+
payload: {
|
|
197
|
+
taskId: 'task-1'
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
expect(mockServer.to).toHaveBeenCalledWith('board:board-1');
|
|
201
|
+
expect(mockServer.emit).toHaveBeenCalledWith('board:event', {
|
|
202
|
+
type: 'task:moved',
|
|
203
|
+
boardId: 'board-1',
|
|
204
|
+
payload: {
|
|
205
|
+
taskId: 'task-1'
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
});
|
|
209
|
+
it('broadcasts a list:reordered event', ()=>{
|
|
210
|
+
gateway.emitToBoardRoom('board-2', {
|
|
211
|
+
type: 'list:reordered',
|
|
212
|
+
boardId: 'board-2',
|
|
213
|
+
payload: {
|
|
214
|
+
orderedIds: [
|
|
215
|
+
'a',
|
|
216
|
+
'b'
|
|
217
|
+
]
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
expect(mockServer.to).toHaveBeenCalledWith('board:board-2');
|
|
221
|
+
expect(mockServer.emit).toHaveBeenCalledWith('board:event', expect.objectContaining({
|
|
222
|
+
type: 'list:reordered'
|
|
223
|
+
}));
|
|
224
|
+
});
|
|
225
|
+
it('does not throw when the server instance is not yet attached', ()=>{
|
|
226
|
+
gateway.server = undefined;
|
|
227
|
+
expect(()=>gateway.emitToBoardRoom('board-1', {
|
|
228
|
+
type: 'task:updated',
|
|
229
|
+
boardId: 'board-1',
|
|
230
|
+
payload: {}
|
|
231
|
+
})).not.toThrow();
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
describe('isUserOnline', ()=>{
|
|
235
|
+
it('returns false for a user that never connected', ()=>{
|
|
236
|
+
expect(gateway.isUserOnline('never-seen')).toBe(false);
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
function _define_property(obj, key, value) {
|
|
2
|
+
if (key in obj) {
|
|
3
|
+
Object.defineProperty(obj, key, {
|
|
4
|
+
value: value,
|
|
5
|
+
enumerable: true,
|
|
6
|
+
configurable: true,
|
|
7
|
+
writable: true
|
|
8
|
+
});
|
|
9
|
+
} else {
|
|
10
|
+
obj[key] = value;
|
|
11
|
+
}
|
|
12
|
+
return obj;
|
|
13
|
+
}
|
|
14
|
+
import { TaskManagerModule } from './task-manager.module';
|
|
15
|
+
import { TASK_MANAGER_MODULE_OPTIONS } from '../config/task-manager.constants';
|
|
16
|
+
import { TaskBoardController, TaskCommentController, TaskLabelController, TaskListController, TaskController } from '../controllers';
|
|
17
|
+
import { TaskBoardGateway } from '../gateways/task-board.gateway';
|
|
18
|
+
function providerTokens(providers) {
|
|
19
|
+
return providers.map((p)=>typeof p === 'function' ? p : p.provide);
|
|
20
|
+
}
|
|
21
|
+
describe('TaskManagerModule', ()=>{
|
|
22
|
+
describe('forRoot', ()=>{
|
|
23
|
+
it('should default to a non-global module with all controllers registered', ()=>{
|
|
24
|
+
const dynamicModule = TaskManagerModule.forRoot({});
|
|
25
|
+
expect(dynamicModule.module).toBe(TaskManagerModule);
|
|
26
|
+
expect(dynamicModule.global).toBe(false);
|
|
27
|
+
expect(dynamicModule.controllers).toEqual([
|
|
28
|
+
TaskBoardController,
|
|
29
|
+
TaskListController,
|
|
30
|
+
TaskController,
|
|
31
|
+
TaskLabelController,
|
|
32
|
+
TaskCommentController
|
|
33
|
+
]);
|
|
34
|
+
});
|
|
35
|
+
it('should respect global: true', ()=>{
|
|
36
|
+
expect(TaskManagerModule.forRoot({
|
|
37
|
+
global: true
|
|
38
|
+
}).global).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
it('should register no controllers when includeController is false', ()=>{
|
|
41
|
+
expect(TaskManagerModule.forRoot({
|
|
42
|
+
includeController: false
|
|
43
|
+
}).controllers).toEqual([]);
|
|
44
|
+
});
|
|
45
|
+
it('should register TaskBoardGateway as both a provider and an export', ()=>{
|
|
46
|
+
const dynamicModule = TaskManagerModule.forRoot({});
|
|
47
|
+
expect(providerTokens(dynamicModule.providers)).toContain(TaskBoardGateway);
|
|
48
|
+
expect(dynamicModule.exports).toContain(TaskBoardGateway);
|
|
49
|
+
});
|
|
50
|
+
it('should register a TASK_MANAGER_MODULE_OPTIONS provider carrying the given options', ()=>{
|
|
51
|
+
const options = {
|
|
52
|
+
bootstrapAppConfig: {}
|
|
53
|
+
};
|
|
54
|
+
const dynamicModule = TaskManagerModule.forRoot(options);
|
|
55
|
+
const provider = dynamicModule.providers.find((p)=>p.provide === TASK_MANAGER_MODULE_OPTIONS);
|
|
56
|
+
expect(provider.useValue).toBe(options);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
describe('forRootAsync', ()=>{
|
|
60
|
+
it('should forward external imports alongside CacheModule/UtilsModule', ()=>{
|
|
61
|
+
let FakeImportedModule = class FakeImportedModule {
|
|
62
|
+
};
|
|
63
|
+
const dynamicModule = TaskManagerModule.forRootAsync({
|
|
64
|
+
imports: [
|
|
65
|
+
FakeImportedModule
|
|
66
|
+
]
|
|
67
|
+
});
|
|
68
|
+
expect(dynamicModule.imports).toEqual(expect.arrayContaining([
|
|
69
|
+
FakeImportedModule
|
|
70
|
+
]));
|
|
71
|
+
});
|
|
72
|
+
it('should still register TaskBoardGateway when using async registration', ()=>{
|
|
73
|
+
const dynamicModule = TaskManagerModule.forRootAsync({
|
|
74
|
+
useFactory: jest.fn()
|
|
75
|
+
});
|
|
76
|
+
expect(providerTokens(dynamicModule.providers)).toContain(TaskBoardGateway);
|
|
77
|
+
expect(dynamicModule.exports).toContain(TaskBoardGateway);
|
|
78
|
+
});
|
|
79
|
+
it('useFactory: builds a TASK_MANAGER_MODULE_OPTIONS provider merging resolved config', async ()=>{
|
|
80
|
+
const useFactory = jest.fn().mockResolvedValue({
|
|
81
|
+
foo: 'bar'
|
|
82
|
+
});
|
|
83
|
+
const dynamicModule = TaskManagerModule.forRootAsync({
|
|
84
|
+
useFactory,
|
|
85
|
+
inject: [
|
|
86
|
+
'SOME_TOKEN'
|
|
87
|
+
]
|
|
88
|
+
});
|
|
89
|
+
const provider = dynamicModule.providers.find((p)=>p.provide === TASK_MANAGER_MODULE_OPTIONS);
|
|
90
|
+
expect(provider.inject).toEqual([
|
|
91
|
+
'SOME_TOKEN'
|
|
92
|
+
]);
|
|
93
|
+
const resolved = await provider.useFactory('injected');
|
|
94
|
+
expect(useFactory).toHaveBeenCalledWith('injected');
|
|
95
|
+
expect(resolved.config).toEqual({
|
|
96
|
+
foo: 'bar'
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
it('useClass: registers both the class provider and an options provider calling createTaskManagerOptions()', async ()=>{
|
|
100
|
+
let MyOptionsFactory = class MyOptionsFactory {
|
|
101
|
+
constructor(){
|
|
102
|
+
_define_property(this, "createTaskManagerOptions", jest.fn().mockResolvedValue({
|
|
103
|
+
foo: 'from-class'
|
|
104
|
+
}));
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const dynamicModule = TaskManagerModule.forRootAsync({
|
|
108
|
+
useClass: MyOptionsFactory
|
|
109
|
+
});
|
|
110
|
+
const classProvider = dynamicModule.providers.find((p)=>p.provide === MyOptionsFactory);
|
|
111
|
+
expect(classProvider.useClass).toBe(MyOptionsFactory);
|
|
112
|
+
const optionsProvider = dynamicModule.providers.find((p)=>p.provide === TASK_MANAGER_MODULE_OPTIONS);
|
|
113
|
+
const instance = new MyOptionsFactory();
|
|
114
|
+
const resolved = await optionsProvider.useFactory(instance);
|
|
115
|
+
expect(instance.createTaskManagerOptions).toHaveBeenCalled();
|
|
116
|
+
expect(resolved.config).toEqual({
|
|
117
|
+
foo: 'from-class'
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
it('useExisting: registers a single options provider without a duplicate class registration', ()=>{
|
|
121
|
+
let MyOptionsFactory = class MyOptionsFactory {
|
|
122
|
+
constructor(){
|
|
123
|
+
_define_property(this, "createTaskManagerOptions", jest.fn());
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
const dynamicModule = TaskManagerModule.forRootAsync({
|
|
127
|
+
useExisting: MyOptionsFactory
|
|
128
|
+
});
|
|
129
|
+
expect(dynamicModule.providers.filter((p)=>p.provide === MyOptionsFactory)).toHaveLength(0);
|
|
130
|
+
const optionsProvider = dynamicModule.providers.find((p)=>p.provide === TASK_MANAGER_MODULE_OPTIONS);
|
|
131
|
+
expect(optionsProvider.inject).toEqual([
|
|
132
|
+
MyOptionsFactory
|
|
133
|
+
]);
|
|
134
|
+
});
|
|
135
|
+
it('should register no TASK_MANAGER_MODULE_OPTIONS provider when neither useFactory, useClass, nor useExisting is given', ()=>{
|
|
136
|
+
const dynamicModule = TaskManagerModule.forRootAsync({});
|
|
137
|
+
expect(providerTokens(dynamicModule.providers)).not.toContain(TASK_MANAGER_MODULE_OPTIONS);
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { Test } from '@nestjs/testing';
|
|
2
|
+
import { TaskActivity } from '../entities';
|
|
3
|
+
import { TaskActivityService } from './task-activity.service';
|
|
4
|
+
import { TaskManagerDataSourceProvider } from './task-manager-datasource.provider';
|
|
5
|
+
describe('TaskActivityService', ()=>{
|
|
6
|
+
let service;
|
|
7
|
+
let mockRepo;
|
|
8
|
+
let mockDataSourceProvider;
|
|
9
|
+
beforeEach(async ()=>{
|
|
10
|
+
mockRepo = {
|
|
11
|
+
create: jest.fn((data)=>({
|
|
12
|
+
id: 'activity-uuid-1',
|
|
13
|
+
...data
|
|
14
|
+
})),
|
|
15
|
+
save: jest.fn().mockResolvedValue(undefined)
|
|
16
|
+
};
|
|
17
|
+
mockDataSourceProvider = {
|
|
18
|
+
getRepository: jest.fn().mockResolvedValue(mockRepo)
|
|
19
|
+
};
|
|
20
|
+
const module = await Test.createTestingModule({
|
|
21
|
+
providers: [
|
|
22
|
+
TaskActivityService,
|
|
23
|
+
{
|
|
24
|
+
provide: TaskManagerDataSourceProvider,
|
|
25
|
+
useValue: mockDataSourceProvider
|
|
26
|
+
}
|
|
27
|
+
]
|
|
28
|
+
}).compile();
|
|
29
|
+
service = await module.resolve(TaskActivityService);
|
|
30
|
+
});
|
|
31
|
+
it('lazily resolves the TaskActivity repository from the DataSource provider', async ()=>{
|
|
32
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.created', null, {
|
|
33
|
+
title: 'New task'
|
|
34
|
+
});
|
|
35
|
+
expect(mockDataSourceProvider.getRepository).toHaveBeenCalledWith(TaskActivity);
|
|
36
|
+
expect(mockDataSourceProvider.getRepository).toHaveBeenCalledTimes(1);
|
|
37
|
+
});
|
|
38
|
+
it('caches the repository across multiple log calls (only resolved once)', async ()=>{
|
|
39
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.created', null, {
|
|
40
|
+
title: 'New task'
|
|
41
|
+
});
|
|
42
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.moved', {
|
|
43
|
+
listId: 'a'
|
|
44
|
+
}, {
|
|
45
|
+
listId: 'b'
|
|
46
|
+
});
|
|
47
|
+
expect(mockDataSourceProvider.getRepository).toHaveBeenCalledTimes(1);
|
|
48
|
+
expect(mockRepo.save).toHaveBeenCalledTimes(2);
|
|
49
|
+
});
|
|
50
|
+
it('records "task.created" activity with title in newValue and null oldValue', async ()=>{
|
|
51
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.created', null, {
|
|
52
|
+
title: 'New task'
|
|
53
|
+
});
|
|
54
|
+
expect(mockRepo.create).toHaveBeenCalledWith({
|
|
55
|
+
taskId: 'task-1',
|
|
56
|
+
boardId: 'board-1',
|
|
57
|
+
userId: 'user-1',
|
|
58
|
+
action: 'task.created',
|
|
59
|
+
oldValue: null,
|
|
60
|
+
newValue: {
|
|
61
|
+
title: 'New task'
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
expect(mockRepo.save).toHaveBeenCalledWith(expect.objectContaining({
|
|
65
|
+
action: 'task.created',
|
|
66
|
+
newValue: {
|
|
67
|
+
title: 'New task'
|
|
68
|
+
}
|
|
69
|
+
}));
|
|
70
|
+
});
|
|
71
|
+
it('records "task.moved" activity with old and new listId', async ()=>{
|
|
72
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.moved', {
|
|
73
|
+
listId: 'list-old'
|
|
74
|
+
}, {
|
|
75
|
+
listId: 'list-new'
|
|
76
|
+
});
|
|
77
|
+
expect(mockRepo.create).toHaveBeenCalledWith(expect.objectContaining({
|
|
78
|
+
action: 'task.moved',
|
|
79
|
+
oldValue: {
|
|
80
|
+
listId: 'list-old'
|
|
81
|
+
},
|
|
82
|
+
newValue: {
|
|
83
|
+
listId: 'list-new'
|
|
84
|
+
}
|
|
85
|
+
}));
|
|
86
|
+
});
|
|
87
|
+
it('records "task.assigned" activity with old and new assigneeIds', async ()=>{
|
|
88
|
+
await service.log('task-1', 'board-1', 'user-1', 'task.assigned', {
|
|
89
|
+
assigneeIds: [
|
|
90
|
+
'u1'
|
|
91
|
+
]
|
|
92
|
+
}, {
|
|
93
|
+
assigneeIds: [
|
|
94
|
+
'u1',
|
|
95
|
+
'u2'
|
|
96
|
+
]
|
|
97
|
+
});
|
|
98
|
+
expect(mockRepo.create).toHaveBeenCalledWith(expect.objectContaining({
|
|
99
|
+
action: 'task.assigned',
|
|
100
|
+
oldValue: {
|
|
101
|
+
assigneeIds: [
|
|
102
|
+
'u1'
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
newValue: {
|
|
106
|
+
assigneeIds: [
|
|
107
|
+
'u1',
|
|
108
|
+
'u2'
|
|
109
|
+
]
|
|
110
|
+
}
|
|
111
|
+
}));
|
|
112
|
+
});
|
|
113
|
+
it('records an activity with a null userId (system-initiated action)', async ()=>{
|
|
114
|
+
await service.log('task-1', 'board-1', null, 'task.created', null, {
|
|
115
|
+
title: 'System task'
|
|
116
|
+
});
|
|
117
|
+
expect(mockRepo.create).toHaveBeenCalledWith(expect.objectContaining({
|
|
118
|
+
userId: null
|
|
119
|
+
}));
|
|
120
|
+
});
|
|
121
|
+
});
|