-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
todo-list.controller.unit.ts
145 lines (130 loc) · 4.62 KB
/
todo-list.controller.unit.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
144
145
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-todo-list
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {
createStubInstance,
expect,
sinon,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {TodoListController} from '../../../controllers';
import {TodoList} from '../../../models';
import {TodoListRepository} from '../../../repositories';
import {givenTodoList} from '../../helpers';
describe('TodoController', () => {
let todoListRepo: StubbedInstanceWithSinonAccessor<TodoListRepository>;
/*
=============================================================================
TEST VARIABLES
Combining top-level objects with our resetRepositories method means we don't
need to duplicate several variable assignments (and generation statements)
in all of our test logic.
NOTE: If you wanted to parallelize your test runs, you should avoid this
pattern since each of these tests is sharing references.
=============================================================================
*/
let controller: TodoListController;
let aTodoList: TodoList;
let aTodoListWithId: TodoList;
let aTodoListToPatchTo: TodoList;
let aChangedTodoList: TodoList;
let aListOfTodoLists: TodoList[];
beforeEach(resetRepositories);
describe('create()', () => {
it('creates a TodoList', async () => {
const create = todoListRepo.stubs.create;
create.resolves(aTodoListWithId);
expect(await controller.create(aTodoList)).to.eql(aTodoListWithId);
sinon.assert.calledWith(create, aTodoList);
});
});
describe('count()', () => {
it('returns the number of existing todoLists', async () => {
const count = todoListRepo.stubs.count;
count.resolves({count: aListOfTodoLists.length});
expect(await controller.count()).to.eql({count: aListOfTodoLists.length});
sinon.assert.called(count);
});
});
describe('find()', () => {
it('returns multiple todos if they exist', async () => {
const find = todoListRepo.stubs.find;
find.resolves(aListOfTodoLists);
expect(await controller.find()).to.eql(aListOfTodoLists);
sinon.assert.called(find);
});
it('returns empty list if no todos exist', async () => {
const find = todoListRepo.stubs.find;
const expected: TodoList[] = [];
find.resolves(expected);
expect(await controller.find()).to.eql(expected);
sinon.assert.called(find);
});
});
describe('updateAll()', () => {
it('returns a number of todos updated', async () => {
const updateAll = todoListRepo.stubs.updateAll;
updateAll.resolves({count: [aChangedTodoList].length});
const where = {title: aTodoListWithId.title};
const result = await controller.updateAll(aTodoListToPatchTo, where);
expect(result).to.eql({count: 1});
sinon.assert.calledWith(updateAll, aTodoListToPatchTo, where);
});
});
describe('findById()', () => {
it('returns a todo if it exists', async () => {
const findById = todoListRepo.stubs.findById;
findById.resolves(aTodoListWithId);
expect(await controller.findById(aTodoListWithId.id as number)).to.eql(
aTodoListWithId,
);
sinon.assert.calledWith(findById, aTodoListWithId.id);
});
});
describe('updateById', () => {
it('successfully updates existing items', async () => {
const updateById = todoListRepo.stubs.updateById;
updateById.resolves();
await controller.updateById(
aTodoListWithId.id as number,
aTodoListToPatchTo,
);
sinon.assert.calledWith(
updateById,
aTodoListWithId.id,
aTodoListToPatchTo,
);
});
});
describe('deleteById', () => {
it('successfully deletes existing items', async () => {
const deleteById = todoListRepo.stubs.deleteById;
deleteById.resolves();
await controller.deleteById(aTodoListWithId.id as number);
sinon.assert.calledWith(deleteById, aTodoListWithId.id);
});
});
function resetRepositories() {
todoListRepo = createStubInstance(TodoListRepository);
aTodoList = givenTodoList();
aTodoListWithId = givenTodoList({
id: 1,
});
aListOfTodoLists = [
aTodoListWithId,
givenTodoList({
id: 2,
title: 'a lot of todos',
}),
] as TodoList[];
aTodoListToPatchTo = givenTodoList({
title: 'changed list of todos',
});
aChangedTodoList = givenTodoList({
id: aTodoListWithId.id,
title: aTodoListToPatchTo.title,
});
controller = new TodoListController(todoListRepo);
}
});