Skip to content

Commit

Permalink
fixup! add todolistimage findbyid and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jun 20, 2019
1 parent 09289d8 commit fba488c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@
// 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 {Client, createRestAppClient, expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {TodoList, TodoListImage} from '../../models/';
import {TodoListRepository, TodoListImageRepository} from '../../repositories/';
import {givenTodoListImage, givenTodoList} from '../helpers';
import {TodoListImageRepository, TodoListRepository} from '../../repositories/';
import {
givenRunningApplicationWithCustomConfiguration,
givenTodoListImage,
givenTodoListInstance,
givenTodoListRepositories,
} from '../helpers';

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

let persistedTodoList: TodoList;

before(givenRunningApplicationWithCustomConfiguration);
before(async () => {
app = await givenRunningApplicationWithCustomConfiguration();
});
after(() => app.stop());

before(givenTodoListImageRepository);
before(givenTodoListRepository);
before(async () => {
({todoListRepo, todoListImageRepo} = await givenTodoListRepositories(app));
});
before(() => {
client = createRestAppClient(app);
});
Expand All @@ -38,7 +40,7 @@ describe('TodoListApplication', () => {
});

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

it('creates image for a todoList', async () => {
Expand Down Expand Up @@ -84,34 +86,6 @@ describe('TodoListApplication', () => {
============================================================================
*/

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 givenTodoListImageInstanceOfTodoList(
id: typeof TodoList.prototype.id,
todoListImage?: Partial<TodoListImage>,
Expand All @@ -120,8 +94,4 @@ describe('TodoListApplication', () => {
delete data.todoListId;
return await todoListRepo.image(id).create(data);
}

async function givenTodoListInstance(todoList?: Partial<TodoList>) {
return await todoListRepo.create(givenTodoList(todoList));
}
});
19 changes: 18 additions & 1 deletion examples/todo-list/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
import {givenHttpServerConfig} from '@loopback/testlab';
import {TodoListApplication} from '../application';
import {Todo, TodoList, TodoListImage} from '../models';
import {TodoListRepository, TodoRepository} from '../repositories';
import {
TodoListImageRepository,
TodoListRepository,
TodoRepository,
} from '../repositories';

/*
==============================================================================
Expand Down Expand Up @@ -100,6 +104,12 @@ export async function givenTodoRepositories(app: TodoListApplication) {
return {todoRepo, todoListRepo};
}

export async function givenTodoListRepositories(app: TodoListApplication) {
const todoListRepo = await app.getRepository(TodoListRepository);
const todoListImageRepo = await app.getRepository(TodoListImageRepository);
return {todoListRepo, todoListImageRepo};
}

export async function givenTodoInstance(
todoRepo: TodoRepository,
todo?: Partial<Todo>,
Expand All @@ -113,3 +123,10 @@ export async function givenTodoListInstance(
) {
return await todoListRepo.create(givenTodoList(data));
}

export async function givenTodoListImageInstance(
todoListImageRepo: TodoListImageRepository,
data?: Partial<TodoListImage>,
) {
return await todoListImageRepo.create(givenTodoListImage(data));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {expect} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {TodoListImageRepository, TodoListRepository} from '../../repositories';
import {
givenRunningApplicationWithCustomConfiguration,
givenTodoListImageInstance,
givenTodoListInstance,
givenTodoListRepositories,
} from '../helpers';

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

before(async () => {
app = await givenRunningApplicationWithCustomConfiguration();
});
after(() => app.stop());

before(async () => {
({todoListRepo, todoListImageRepo} = await givenTodoListRepositories(app));
});

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

it('includes TodoList in find method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
const image = await givenTodoListImageInstance(todoListImageRepo, {
todoListId: list.id,
});

const response = await todoListImageRepo.find({
include: [{relation: 'todoList'}],
});

expect(response[0]).to.containEql(image);
expect(response[0]).to.containEql({
todoList: list,
});
});

it('includes TodoList in findById result', async () => {
const list = await givenTodoListInstance(todoListRepo, {});
const image = await givenTodoListImageInstance(todoListImageRepo, {
todoListId: list.id,
});

const response = await todoListImageRepo.findById(image.id, {
include: [{relation: 'todoList'}],
});

expect(response).to.containEql(image);
expect(response).to.containEql({
todoList: list,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
givenTodoRepositories,
} from '../helpers';

describe('TodoApplication', () => {
describe('TodoListApplication', () => {
let app: TodoListApplication;
let todoRepo: TodoRepository;
let todoListRepo: TodoListRepository;
Expand Down Expand Up @@ -41,7 +41,6 @@ describe('TodoApplication', () => {
});

it('includes TodoList in findById result', async () => {
// remove grey?
const list = await givenTodoListInstance(todoListRepo, {});
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});

Expand Down
19 changes: 19 additions & 0 deletions examples/todo-list/src/repositories/todo-list-image.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ export class TodoListImageRepository extends DefaultCrudRepository<

return result;
}

async findById(
id: typeof TodoListImage.prototype.id,
filter?: Filter<TodoListImage>,
options?: Options,
): Promise<TodoListImageWithRelations> {
// Prevent juggler for applying "include" filter
// Juggler is not aware of LB4 relations
const include = filter && filter.include;
filter = filter && Object.assign(filter, {include: undefined});

const result = await super.findById(id, filter, options);

if (include && include.length && include[0].relation === 'todoList') {
result.todoList = await this.todoList(result.id);
}

return result;
}
}
2 changes: 1 addition & 1 deletion examples/todo-list/src/repositories/todo.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class TodoRepository extends DefaultCrudRepository<
if (include && include.length && include[0].relation === 'todoList') {
await Promise.all(
result.map(async r => {
if (r.todoListId) r.todoList = await this.todoList(r.id);
r.todoList = await this.todoList(r.id);
}),
);
}
Expand Down

0 comments on commit fba488c

Please sign in to comment.