-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
todo.controller.unit.ts
161 lines (138 loc) · 5.04 KB
/
todo.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright IBM Corp. 2019. All Rights Reserved.
// Node module: @loopback/example-todo
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {Filter} from '@loopback/repository';
import {
createStubInstance,
expect,
sinon,
StubbedInstanceWithSinonAccessor,
} from '@loopback/testlab';
import {TodoController} from '../../../controllers';
import {Todo} from '../../../models/index';
import {TodoRepository} from '../../../repositories';
import {GeocoderService} from '../../../services';
import {aLocation, givenTodo} from '../../helpers';
describe('TodoController', () => {
let todoRepo: StubbedInstanceWithSinonAccessor<TodoRepository>;
let geoService: GeocoderService;
let geocode: sinon.SinonStub;
/*
=============================================================================
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: TodoController;
let aTodo: Todo;
let aTodoWithId: Todo;
let aChangedTodo: Todo;
let aListOfTodos: Todo[];
beforeEach(resetRepositories);
describe('createTodo', () => {
it('creates a Todo', async () => {
const create = todoRepo.stubs.create;
create.resolves(aTodoWithId);
const result = await controller.createTodo(aTodo);
expect(result).to.eql(aTodoWithId);
sinon.assert.calledWith(create, aTodo);
});
it('resolves remindAtAddress to a geocode', async () => {
const create = todoRepo.stubs.create;
geocode.resolves([aLocation.geopoint]);
const input = givenTodo({remindAtAddress: aLocation.address});
const expected = new Todo(input);
Object.assign(expected, {
remindAtAddress: aLocation.address,
remindAtGeo: aLocation.geostring,
});
create.resolves(expected);
const result = await controller.createTodo(input);
expect(result).to.eql(expected);
sinon.assert.calledWith(create, input);
sinon.assert.calledWith(geocode, input.remindAtAddress);
});
});
describe('findTodoById', () => {
it('returns a todo if it exists', async () => {
const findById = todoRepo.stubs.findById;
findById.resolves(aTodoWithId);
expect(await controller.findTodoById(aTodoWithId.id as number)).to.eql(
aTodoWithId,
);
sinon.assert.calledWith(findById, aTodoWithId.id);
});
});
describe('findTodos', () => {
it('returns multiple todos if they exist', async () => {
const find = todoRepo.stubs.find;
find.resolves(aListOfTodos);
expect(await controller.findTodos()).to.eql(aListOfTodos);
sinon.assert.called(find);
});
it('returns empty list if no todos exist', async () => {
const find = todoRepo.stubs.find;
const expected: Todo[] = [];
find.resolves(expected);
expect(await controller.findTodos()).to.eql(expected);
sinon.assert.called(find);
});
it('uses the provided filter', async () => {
const find = todoRepo.stubs.find;
const filter: Filter<Todo> = {where: {isComplete: false}};
find.resolves(aListOfTodos);
await controller.findTodos(filter);
sinon.assert.calledWith(find, filter);
});
});
describe('replaceTodo', () => {
it('successfully replaces existing items', async () => {
const replaceById = todoRepo.stubs.replaceById;
replaceById.resolves();
await controller.replaceTodo(aTodoWithId.id as number, aChangedTodo);
sinon.assert.calledWith(replaceById, aTodoWithId.id, aChangedTodo);
});
});
describe('updateTodo', () => {
it('successfully updates existing items', async () => {
const updateById = todoRepo.stubs.updateById;
updateById.resolves();
await controller.updateTodo(aTodoWithId.id as number, aChangedTodo);
sinon.assert.calledWith(updateById, aTodoWithId.id, aChangedTodo);
});
});
describe('deleteTodo', () => {
it('successfully deletes existing items', async () => {
const deleteById = todoRepo.stubs.deleteById;
deleteById.resolves();
await controller.deleteTodo(aTodoWithId.id as number);
sinon.assert.calledWith(deleteById, aTodoWithId.id);
});
});
function resetRepositories() {
todoRepo = createStubInstance(TodoRepository);
aTodo = givenTodo();
aTodoWithId = givenTodo({
id: 1,
});
aListOfTodos = [
aTodoWithId,
givenTodo({
id: 2,
title: 'so many things to do',
}),
] as Todo[];
aChangedTodo = givenTodo({
id: aTodoWithId.id,
title: 'Do some important things',
});
geoService = {geocode: sinon.stub()};
geocode = geoService.geocode as sinon.SinonStub;
controller = new TodoController(todoRepo, geoService);
}
});