Skip to content

Commit 33c6e09

Browse files
author
Manuela Paula Ritter
committed
more tests
1 parent fb12e95 commit 33c6e09

File tree

2 files changed

+168
-47
lines changed

2 files changed

+168
-47
lines changed

src/app/matrix/matrix.reducers.spec.ts

+133-7
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ import {
44
AddTopic,
55
AddTopicFailed,
66
AddTopicSuccess,
7+
DeleteTaskFailed,
8+
DeleteTaskSuccess,
79
GetMatrixData,
810
GetMatrixDataFailed,
911
GetMatrixDataSuccess,
1012
MatrixAction,
13+
SelectTask,
1114
ToggleTopicVisibility,
1215
UpdateTask,
1316
UpdateTaskFailed,
@@ -126,7 +129,7 @@ describe('MatrixReducers', () => {
126129
beforeEach(() => {
127130
defaultState = {
128131
topics: [new Topic(1, 'test', Color.orange, true, false)],
129-
tasks: [],
132+
tasks: [new Task(1, 'Test', 1, 1, 1, 1, 21)],
130133
taskHistory: [],
131134
isLoading: true,
132135
errorMessage: 'test message',
@@ -169,6 +172,29 @@ describe('MatrixReducers', () => {
169172
expect(newState.isLoading).toEqual(false);
170173
expect(newState.errorMessage).toBeUndefined();
171174
});
175+
176+
it('should set unloading on DELETE_TASK_SUCCESS', () => {
177+
const action: MatrixAction = new DeleteTaskSuccess(
178+
new Task(
179+
1,
180+
'Test',
181+
1,
182+
1,
183+
1,
184+
1,
185+
21,
186+
'',
187+
false,
188+
true,
189+
new Date(),
190+
new Date(),
191+
new Date(),
192+
),
193+
);
194+
const newState = matrixReducer(defaultState, action);
195+
expect(newState.isLoading).toEqual(false);
196+
expect(newState.errorMessage).toBeUndefined();
197+
});
172198
});
173199

174200
describe('success states', () => {
@@ -203,12 +229,37 @@ describe('MatrixReducers', () => {
203229
it('should update task on UPDATE_TASK_SUCCESS', () => {
204230
defaultState = {
205231
...defaultState,
206-
tasks: [new Task(1, 'test', 1, 1, 1, 12, 2020)],
232+
tasks: [new Task(1, 'test', 1, 1, 1, 12, 20)],
207233
};
208-
const updatedTask = new Task(1, 'test', 7, 1, 1, 12, 2020);
234+
const updatedTask = new Task(1, 'test', 7, 1, 1, 12, 20);
209235
const action: MatrixAction = new UpdateTaskSuccess(updatedTask);
210236
const newState = matrixReducer(defaultState, action);
211-
expect(newState.tasks[1]).toContain(updatedTask);
237+
expect(newState.tasks).toContain(updatedTask);
238+
});
239+
240+
it('should delete task on DELETE_TASK_SUCCESS', () => {
241+
defaultState = {
242+
...defaultState,
243+
tasks: [new Task(1, 'test', 1, 1, 1, 1, 21)],
244+
};
245+
const updatedTask = new Task(
246+
1,
247+
'test',
248+
1,
249+
1,
250+
1,
251+
1,
252+
21,
253+
'',
254+
false,
255+
true,
256+
undefined,
257+
undefined,
258+
new Date(),
259+
);
260+
const action: MatrixAction = new DeleteTaskSuccess(updatedTask);
261+
const newState = matrixReducer(defaultState, action);
262+
expect(newState.tasks[0]).toEqual(updatedTask);
212263
});
213264

214265
it('should add topic on ADD_TOPIC_SUCCESS', () => {
@@ -243,9 +294,14 @@ describe('MatrixReducers', () => {
243294
});
244295

245296
it('should toggle visibility on TOGGLE_TOPIC_VISIBILITY', () => {
246-
const action: MatrixAction = new ToggleTopicVisibility(1);
247-
const newState = matrixReducer(defaultState, action);
248-
expect(newState.topics[0].visible).toEqual(false);
297+
const action1: MatrixAction = new ToggleTopicVisibility(1);
298+
const action2: MatrixAction = new ToggleTopicVisibility(2);
299+
const newState1 = matrixReducer(defaultState, action1);
300+
expect(newState1.topics[0].visible).toEqual(false);
301+
expect(newState1.topics[1].visible).toEqual(false);
302+
const newState2 = matrixReducer(newState1, action2);
303+
expect(newState2.topics[0].visible).toEqual(false);
304+
expect(newState2.topics[1].visible).toEqual(true);
249305
});
250306

251307
it('should not change if topic not found on TOGGLE_TOPIC_VISIBILITY', () => {
@@ -254,4 +310,74 @@ describe('MatrixReducers', () => {
254310
expect(newState).toEqual(defaultState);
255311
});
256312
});
313+
314+
describe('select task', () => {
315+
let defaultState: MatrixState;
316+
317+
beforeEach(() => {
318+
defaultState = {
319+
topics: [],
320+
tasks: [
321+
new Task(1, 'test', 1, 1, 1, 1, 21),
322+
new Task(2, 'test', 1, 1, 1, 1, 21),
323+
new Task(3, 'test', 1, 1, 1, 1, 21),
324+
new Task(4, 'test', 1, 1, 1, 1, 21),
325+
new Task(5, 'test', 1, 1, 1, 1, 21),
326+
new Task(6, 'test', 1, 1, 1, 1, 21),
327+
new Task(7, 'test', 1, 1, 1, 1, 21),
328+
],
329+
taskHistory: [],
330+
isLoading: false,
331+
errorMessage: undefined,
332+
};
333+
});
334+
335+
it('should select tasks in correct order on SELECT_TASK', () => {
336+
const action1: MatrixAction = new SelectTask(1);
337+
const action2: MatrixAction = new SelectTask(2);
338+
const action3: MatrixAction = new SelectTask(3);
339+
const action4: MatrixAction = new SelectTask(4);
340+
let newState = matrixReducer(defaultState, action1);
341+
newState = matrixReducer(newState, action2);
342+
newState = matrixReducer(newState, action3);
343+
newState = matrixReducer(newState, action4);
344+
newState = matrixReducer(newState, action4);
345+
expect(newState.taskHistory).toEqual([4, 3, 2, 1]);
346+
});
347+
348+
it('should only select task once on SELECT_TASK', () => {
349+
const action: MatrixAction = new SelectTask(1);
350+
let newState = matrixReducer(defaultState, action);
351+
newState = matrixReducer(newState, action);
352+
expect(newState.taskHistory).toEqual([1]);
353+
});
354+
355+
it('should only change taskHistory on SELECT_TASK', () => {
356+
const action: MatrixAction = new SelectTask(1);
357+
const newState = matrixReducer(defaultState, action);
358+
expect(newState).toEqual({ ...defaultState, taskHistory: [1] });
359+
});
360+
361+
it('should correctly order tasks in history on SELECT_TASK', () => {
362+
const action1: MatrixAction = new SelectTask(1);
363+
const action7: MatrixAction = new SelectTask(7);
364+
const action5: MatrixAction = new SelectTask(5);
365+
const action3: MatrixAction = new SelectTask(3);
366+
let newState = matrixReducer(defaultState, action1);
367+
newState = matrixReducer(newState, action7);
368+
newState = matrixReducer(newState, action5);
369+
newState = matrixReducer(newState, action3);
370+
newState = matrixReducer(newState, action3);
371+
newState = matrixReducer(newState, action1);
372+
expect(newState.taskHistory).toEqual([1, 3, 5, 7]);
373+
});
374+
375+
it('should only take 6 elements in history on SELECT_TASK', () => {
376+
let newState = matrixReducer(defaultState, new SelectTask(1));
377+
for (let i = 2; i < 8; i++) {
378+
newState = matrixReducer(newState, new SelectTask(i));
379+
}
380+
expect(newState.taskHistory.length).toEqual(6);
381+
});
382+
});
257383
});

src/app/matrix/matrix.service.spec.ts

+35-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { async, TestBed } from '@angular/core/testing';
22
import { Action, Store } from '@ngrx/store';
33
import { MockStore, provideMockStore } from '@ngrx/store/testing';
4+
import { of } from 'rxjs';
45
import { Color } from '../shared/color.interfaces';
56
import {
67
DeleteTask,
@@ -13,7 +14,7 @@ import { MatrixState, Task, Topic } from './matrix.interfaces';
1314
import { initialState } from './matrix.reducers';
1415
import { MatrixService } from './matrix.service';
1516

16-
describe('MatrixService', () => {
17+
fdescribe('MatrixService', () => {
1718
let store: MockStore<MatrixState>;
1819
let service: MatrixService;
1920

@@ -37,58 +38,52 @@ describe('MatrixService', () => {
3738
dispatchSpy = spyOn(store, 'dispatch');
3839
});
3940

40-
describe('getData', () => {
41-
let action: Action;
42-
43-
beforeEach(() => {
44-
action = new GetMatrixData();
45-
});
41+
it('getData - should dispatch GetMatrixData action to store', () => {
42+
const action = new GetMatrixData();
43+
service.getData();
44+
expect(dispatchSpy).toHaveBeenCalledWith(action);
45+
});
4646

47-
it('should dispatch GetMatrixData action to store', () => {
48-
service.getData();
49-
expect(dispatchSpy).toHaveBeenCalledWith(action);
50-
});
47+
it('updateTask - should dispatch UpdateTask action with task to store', () => {
48+
const task = new Task(1, 'test', 1, 1, 1, 12, 20);
49+
const action = new UpdateTask(task);
50+
service.updateTask(task);
51+
expect(dispatchSpy).toHaveBeenCalledWith(action);
5152
});
5253

53-
describe('updateTask', () => {
54-
it('should dispatch UpdateTask action with task to store', () => {
55-
const task = new Task(1, 'test', 1, 1);
56-
const action = new UpdateTask(task);
57-
service.updateTask(task);
58-
expect(dispatchSpy).toHaveBeenCalledWith(action);
59-
});
54+
it('deleteTask - should dispatch DeleteTask action with id to store', () => {
55+
const id = 1;
56+
const action = new DeleteTask(id);
57+
service.deleteTask(id);
58+
expect(dispatchSpy).toHaveBeenCalledWith(action);
6059
});
6160

62-
describe('deleteTask', () => {
63-
it('should dispatch DeleteTask action with id to store', () => {
64-
const id = 1;
65-
const action = new DeleteTask(id);
66-
service.deleteTask(id);
67-
expect(dispatchSpy).toHaveBeenCalledWith(action);
68-
});
61+
it('toggleTopicVisibility - should dispatch ToggleTopicVisibility with id to store', () => {
62+
const id = 34;
63+
const action = new ToggleTopicVisibility(id);
64+
service.toggleTopicVisibility(id);
65+
expect(dispatchSpy).toHaveBeenCalledWith(action);
6966
});
7067

71-
describe('toggleTopicVisibility', () => {
72-
it('should dispatch ToggleTopicVisibility with id to store', () => {
73-
const id = 34;
74-
const action = new ToggleTopicVisibility(id);
75-
service.toggleTopicVisibility(id);
76-
expect(dispatchSpy).toHaveBeenCalledWith(action);
77-
});
68+
it('updateTopic - should dispatch UpdateTopic with topic to store', () => {
69+
const topic = new Topic(1, 'Test', Color.orange, true, false);
70+
const action = new UpdateTopic(topic);
71+
service.updateTopic(topic);
72+
expect(dispatchSpy).toHaveBeenCalledWith(action);
7873
});
74+
});
7975

80-
describe('updateTopic', () => {
81-
it('should dispatch UpdateTopic with topic to store', () => {
82-
const topic = new Topic(1, 'Test', Color.orange, true, false);
83-
const action = new UpdateTopic(topic);
84-
service.updateTopic(topic);
85-
expect(dispatchSpy).toHaveBeenCalledWith(action);
76+
describe('select', () => {
77+
it('selectTopics - should select topics from store', (done) => {
78+
const testTopic = new Topic(1, 'Test', Color.green, true, false);
79+
store.overrideSelector('selectMatrixTopics', [testTopic]);
80+
service.selectTopics().subscribe((result) => {
81+
expect(result).toEqual([testTopic]);
82+
done();
8683
});
8784
});
8885
});
8986

90-
describe('selectTopics', () => {});
91-
9287
describe('selectIsLoading', () => {});
9388

9489
describe('selectErrorMessage', () => {});

0 commit comments

Comments
 (0)