Skip to content

Commit

Permalink
fixup! add acceptance test for todoListImage
Browse files Browse the repository at this point in the history
  • Loading branch information
biniam committed Dec 12, 2018
1 parent bb946f3 commit a3e6d88
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 3 deletions.
4 changes: 2 additions & 2 deletions examples/todo-list/src/models/todo-list-image.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {TodoList} from './todo-list.model';
@model()
export class TodoListImage extends Entity {
@property({
type: 'string',
type: 'number',
id: true,
})
id: string;
id: number;

@belongsTo(() => TodoList)
todoListId?: number;
Expand Down
1 change: 1 addition & 0 deletions examples/todo-list/src/repositories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

export * from './todo.repository';
export * from './todo-list.repository';
export * from './todo-list-image.repository';
136 changes: 136 additions & 0 deletions examples/todo-list/test/acceptance/todo-list-image.acceptance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// Copyright IBM Corp. 2018. 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 {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
toJSON,
} from '@loopback/testlab';
import {TodoListApplication} from '../../src/application';
import {Todo, TodoList, TodoListImage} from '../../src/models/';
import {
TodoListRepository,
TodoListImageRepository,
} from '../../src/repositories/';
import {givenTodoListImage, givenTodoList} from '../helpers';

describe.only('TodoListApplication', () => {
let app: TodoListApplication;
let client: Client;
let todoListImageRepo: TodoListImageRepository;
let todoListRepo: TodoListRepository;

let persistedTodoList: TodoList;

before(givenRunningApplicationWithCustomConfiguration);
after(() => app.stop());

before(givenTodoListImageRepository);
before(givenTodoListRepository);
before(() => {
client = createRestAppClient(app);
});

beforeEach(async () => {
await todoListImageRepo.deleteAll();
await todoListRepo.deleteAll();
});

beforeEach(async () => {
persistedTodoList = await givenTodoListInstance();
});

it('creates image for a todoList', async () => {
const todoListImage = givenTodoListImage();
delete todoListImage.todoListId;
const response = await client
.post(`/todo-lists/${persistedTodoList.id}/image`)
.send(todoListImage)
.expect(200);

const expected = {...todoListImage, todoListId: persistedTodoList.id};
expect(response.body).to.containEql(expected);

const created = await todoListImageRepo.findById(response.body.id);
expect(toJSON(created)).to.deepEqual({id: response.body.id, ...expected});
});

it('finds images for a todoList', async () => {
const todoListImage = await givenTodoListImageInstanceOfTodoList(
persistedTodoList.id,
{
value: 'A picture of a green checkmark',
},
);

const response = await client
.get(`/todo-lists/${persistedTodoList.id}/image`)
.send()
.expect(200);

expect(response.body).to.containDeep(todoListImage);
});

/*
============================================================================
TEST HELPERS
These functions help simplify setup of your test fixtures so that your tests
can:
- operate on a "clean" environment each time (a fresh in-memory database)
- avoid polluting the test with large quantities of setup logic to keep
them clear and easy to read
- keep them DRY (who wants to write the same stuff over and over?)
============================================================================
*/

async function givenRunningApplicationWithCustomConfiguration() {
app = new TodoListApplication({
rest: givenHttpServerConfig(),
});

await app.boot();

/**
* Override default config for DataSource for testing so we don't write
* test data to file when using the memory connector.
*/
app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});

// Start Application
await app.start();
}

async function givenTodoListImageRepository() {
todoListImageRepo = await app.getRepository(TodoListImageRepository);
}

async function givenTodoListRepository() {
todoListRepo = await app.getRepository(TodoListRepository);
}

async function givenTodoListImageInstance(
todoListImage?: Partial<TodoListImage>,
) {
return await todoListImageRepo.create(givenTodoListImage(todoListImage));
}

async function givenTodoListImageInstanceOfTodoList(
id: typeof TodoList.prototype.id,
todoListImage?: Partial<TodoListImage>,
) {
const data = givenTodoListImage(todoListImage);
delete data.todoListId;
return await todoListRepo.image(id).create(data);
}

async function givenTodoListInstance(todoList?: Partial<TodoList>) {
return await todoListRepo.create(givenTodoList(todoList));
}
});
16 changes: 15 additions & 1 deletion examples/todo-list/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {Todo, TodoList} from '../src/models';
import {Todo, TodoList, TodoListImage} from '../src/models';

/*
==============================================================================
Expand Down Expand Up @@ -55,3 +55,17 @@ export function givenTodoList(todoList?: Partial<TodoList>) {
);
return new TodoList(data);
}

/**
* Generate a complete TodoListImage object for use with tests.
* @param todoListImage A partial (or complete) TodoListImage object.
*/
export function givenTodoListImage(todoListImage?: Partial<TodoListImage>) {
const data = Object.assign(
{
value: 'A picture of a basket of fruits',
},
todoListImage,
);
return new TodoListImage(data);
}

0 comments on commit a3e6d88

Please sign in to comment.