Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type safety of function calls #2733

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,6 @@ import {givenTodoList} from '../../helpers';
describe('TodoController', () => {
let todoListRepo: StubbedInstanceWithSinonAccessor<TodoListRepository>;

/*
=============================================================================
METHOD STUBS
These handles give us a quick way to fake the response of our repository
without needing to wrangle fake repository objects or manage real ones
in our tests themselves.
=============================================================================
*/
let create: sinon.SinonStub;
let count: sinon.SinonStub;
let find: sinon.SinonStub;
let updateAll: sinon.SinonStub;
let findById: sinon.SinonStub;
let updateById: sinon.SinonStub;
let deleteById: sinon.SinonStub;

/*
=============================================================================
TEST VARIABLES
Expand All @@ -55,6 +39,7 @@ describe('TodoController', () => {

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);
Expand All @@ -63,20 +48,23 @@ describe('TodoController', () => {

describe('count()', () => {
it('returns the number of existing todoLists', async () => {
count.resolves(aListOfTodoLists.length);
expect(await controller.count()).to.eql(aListOfTodoLists.length);
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);
Expand All @@ -86,15 +74,18 @@ describe('TodoController', () => {

describe('updateAll()', () => {
it('returns a number of todos updated', async () => {
updateAll.resolves([aChangedTodoList].length);
const updateAll = todoListRepo.stubs.updateAll;
updateAll.resolves({count: [aChangedTodoList].length});
const where = {title: aTodoListWithId.title};
expect(await controller.updateAll(aTodoListToPatchTo, where)).to.eql(1);
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,
Expand All @@ -105,6 +96,7 @@ describe('TodoController', () => {

describe('updateById', () => {
it('successfully updates existing items', async () => {
const updateById = todoListRepo.stubs.updateById;
updateById.resolves();
await controller.updateById(
aTodoListWithId.id as number,
Expand All @@ -120,6 +112,7 @@ describe('TodoController', () => {

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);
Expand Down Expand Up @@ -147,17 +140,6 @@ describe('TodoController', () => {
title: aTodoListToPatchTo.title,
});

// Setup CRUD fakes
({
create,
count,
find,
updateAll,
findById,
updateById,
deleteById,
} = todoListRepo.stubs);

controller = new TodoListController(todoListRepo);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,6 @@ import {givenTodo} from '../../helpers';
describe('TodoController', () => {
let todoRepo: StubbedInstanceWithSinonAccessor<TodoRepository>;

/*
=============================================================================
METHOD STUBS
These handles give us a quick way to fake the response of our repository
without needing to wrangle fake repository objects or manage real ones
in our tests themselves.
=============================================================================
*/
let create: sinon.SinonStub;
let findById: sinon.SinonStub;
let find: sinon.SinonStub;
let replaceById: sinon.SinonStub;
let updateById: sinon.SinonStub;
let deleteById: sinon.SinonStub;

/*
=============================================================================
TEST VARIABLES
Expand All @@ -53,6 +38,7 @@ describe('TodoController', () => {

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);
Expand All @@ -62,6 +48,7 @@ describe('TodoController', () => {

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,
Expand All @@ -72,12 +59,14 @@ describe('TodoController', () => {

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);
Expand All @@ -87,6 +76,7 @@ describe('TodoController', () => {

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);
Expand All @@ -95,6 +85,7 @@ describe('TodoController', () => {

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);
Expand All @@ -103,6 +94,7 @@ describe('TodoController', () => {

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);
Expand All @@ -127,16 +119,6 @@ describe('TodoController', () => {
title: 'Do some important things',
});

// Setup CRUD fakes
({
create,
findById,
find,
updateById,
replaceById,
deleteById,
} = todoRepo.stubs);

controller = new TodoController(todoRepo);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,6 @@ describe('TodoController', () => {
let todoRepo: StubbedInstanceWithSinonAccessor<TodoRepository>;
let geoService: GeocoderService;

/*
=============================================================================
METHOD STUBS
These handles give us a quick way to fake the response of our repository
without needing to wrangle fake repository objects or manage real ones
in our tests themselves.
=============================================================================
*/
let create: sinon.SinonStub;
let findById: sinon.SinonStub;
let find: sinon.SinonStub;
let replaceById: sinon.SinonStub;
let updateById: sinon.SinonStub;
let deleteById: sinon.SinonStub;
let geocode: sinon.SinonStub;

/*
Expand All @@ -57,13 +43,15 @@ describe('TodoController', () => {

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});
Expand All @@ -84,6 +72,7 @@ describe('TodoController', () => {

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,
Expand All @@ -94,19 +83,22 @@ describe('TodoController', () => {

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 = {where: {isCompleted: false}};

find.resolves(aListOfTodos);
Expand All @@ -117,6 +109,7 @@ describe('TodoController', () => {

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);
Expand All @@ -125,6 +118,7 @@ describe('TodoController', () => {

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);
Expand All @@ -133,6 +127,7 @@ describe('TodoController', () => {

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);
Expand All @@ -157,16 +152,6 @@ describe('TodoController', () => {
title: 'Do some important things',
});

// Setup CRUD fakes
({
create,
findById,
find,
updateById,
replaceById,
deleteById,
} = todoRepo.stubs);

geoService = {geocode: sinon.stub()};
geocode = geoService.geocode as sinon.SinonStub;

Expand Down
Loading