Skip to content

Commit

Permalink
refactor(example-todo-list): move reused functions into helper
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jun 19, 2019
1 parent f9a7a01 commit 62637cc
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 204 deletions.
84 changes: 31 additions & 53 deletions examples/todo-list/src/__tests__/acceptance/todo-list.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,33 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityNotFoundError} from '@loopback/repository';
import {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
toJSON,
} from '@loopback/testlab';
import {Client, createRestAppClient, expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {Todo, TodoList} from '../../models/';
import {TodoList} from '../../models/';
import {TodoListRepository, TodoRepository} from '../../repositories/';
import {givenTodo, givenTodoList} from '../helpers';
import {
givenRunningApplicationWithCustomConfiguration,
givenTodoInstance,
givenTodoList,
givenTodoListInstance,
givenTodoRepositories,
} from '../helpers';

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

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

before(givenTodoRepositories);
before(async () => {
({todoRepo, todoListRepo} = await givenTodoRepositories(app));
});

before(() => {
client = createRestAppClient(app);
});
Expand Down Expand Up @@ -91,8 +96,14 @@ describe('TodoListApplication', () => {

it('updates selected todoLists', async () => {
await todoListRepo.deleteAll();
await givenTodoListInstance({title: 'red-list', color: 'red'});
await givenTodoListInstance({title: 'green-list', color: 'green'});
await givenTodoListInstance(todoListRepo, {
title: 'red-list',
color: 'red',
});
await givenTodoListInstance(todoListRepo, {
title: 'green-list',
color: 'green',
});

const response = await client
.patch('/todo-lists')
Expand All @@ -119,7 +130,7 @@ describe('TodoListApplication', () => {
let persistedTodoList: TodoList;

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

it('gets a todoList by ID', async () => {
Expand Down Expand Up @@ -166,9 +177,9 @@ describe('TodoListApplication', () => {
});

it('queries todo-lists with a filter', async () => {
await givenTodoListInstance({title: 'day', color: 'white'});
await givenTodoListInstance(todoListRepo, {title: 'day', color: 'white'});

const listInBlack = await givenTodoListInstance({
const listInBlack = await givenTodoListInstance(todoListRepo, {
title: 'night',
color: 'black',
});
Expand All @@ -180,8 +191,8 @@ describe('TodoListApplication', () => {
});

it('includes Todos in query result', async () => {
const list = await givenTodoListInstance();
const todo = await givenTodoInstance({todoListId: list.id});
const list = await givenTodoListInstance(todoListRepo);
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});
const filter = JSON.stringify({include: [{relation: 'todos'}]});

const response = await client.get('/todo-lists').query({filter: filter});
Expand All @@ -205,43 +216,10 @@ 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 givenTodoRepositories() {
todoRepo = await app.getRepository(TodoRepository);
todoListRepo = await app.getRepository(TodoListRepository);
}

async function givenTodoListInstance(todoList?: Partial<TodoList>) {
return await todoListRepo.create(givenTodoList(todoList));
}

async function givenTodoInstance(todo?: Partial<Todo>) {
return await todoRepo.create(givenTodo(todo));
}

function givenMutlipleTodoListInstances() {
return Promise.all([
givenTodoListInstance(),
givenTodoListInstance({title: 'so many things to do wow'}),
givenTodoListInstance(todoListRepo),
givenTodoListInstance(todoListRepo, {title: 'so many things to do wow'}),
]);
}
});
85 changes: 22 additions & 63 deletions examples/todo-list/src/__tests__/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,32 @@
// License text available at https://opensource.org/licenses/MIT

import {EntityNotFoundError} from '@loopback/repository';
import {
Client,
createRestAppClient,
expect,
givenHttpServerConfig,
toJSON,
} from '@loopback/testlab';
import {Client, createRestAppClient, expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {Todo, TodoList} from '../../models/';
import {Todo} from '../../models/';
import {TodoListRepository, TodoRepository} from '../../repositories/';
import {givenTodo, givenTodoList} from '../helpers';
import {
givenRunningApplicationWithCustomConfiguration,
givenTodo,
givenTodoInstance,
givenTodoListInstance,
givenTodoRepositories,
} from '../helpers';

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

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

before(givenTodoRepositories);
before(async () => {
({todoRepo, todoListRepo} = await givenTodoRepositories(app));
});
before(() => {
client = createRestAppClient(app);
});
Expand Down Expand Up @@ -58,7 +62,7 @@ describe('TodoListApplication', () => {
let persistedTodo: Todo;

beforeEach(async () => {
persistedTodo = await givenTodoInstance();
persistedTodo = await givenTodoInstance(todoRepo);
});

it('gets a todo by ID', () => {
Expand Down Expand Up @@ -128,17 +132,17 @@ describe('TodoListApplication', () => {
});

it('returns the owning todo-list', async () => {
const list = await givenTodoListInstance();
const todo = await givenTodoInstance({todoListId: list.id});
const list = await givenTodoListInstance(todoListRepo);
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});

await client.get(`/todos/${todo.id}/todo-list`).expect(200, toJSON(list));
});
});

it('queries todos with a filter', async () => {
await givenTodoInstance({title: 'wake up', isComplete: true});
await givenTodoInstance(todoRepo, {title: 'wake up', isComplete: true});

const todoInProgress = await givenTodoInstance({
const todoInProgress = await givenTodoInstance(todoRepo, {
title: 'go to sleep',
isComplete: false,
});
Expand All @@ -150,8 +154,8 @@ describe('TodoListApplication', () => {
});

it('includes TodoList in query result', async () => {
const list = await givenTodoListInstance();
const todo = await givenTodoInstance({todoListId: list.id});
const list = await givenTodoListInstance(todoListRepo);
const todo = await givenTodoInstance(todoRepo, {todoListId: list.id});
const filter = JSON.stringify({include: [{relation: 'todoList'}]});

const response = await client.get('/todos').query({filter: filter});
Expand All @@ -162,49 +166,4 @@ describe('TodoListApplication', () => {
todoList: toJSON(list),
});
});

/*
============================================================================
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 givenTodoRepositories() {
todoRepo = await app.getRepository(TodoRepository);
todoListRepo = await app.getRepository(TodoListRepository);
}

async function givenTodoInstance(todo?: Partial<Todo>) {
return await todoRepo.create(givenTodo(todo));
}

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

import {givenHttpServerConfig} from '@loopback/testlab';
import {TodoListApplication} from '../application';
import {Todo, TodoList, TodoListImage} from '../models';
import {TodoListRepository, TodoRepository} from '../repositories';

/*
==============================================================================
Expand Down Expand Up @@ -69,3 +72,44 @@ export function givenTodoListImage(todoListImage?: Partial<TodoListImage>) {
);
return new TodoListImage(data);
}

export async function givenRunningApplicationWithCustomConfiguration() {
const 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();
return app;
}

export async function givenTodoRepositories(app: TodoListApplication) {
const todoRepo = await app.getRepository(TodoRepository);
const todoListRepo = await app.getRepository(TodoListRepository);
return {todoRepo, todoListRepo};
}

export async function givenTodoInstance(
todoRepo: TodoRepository,
todo?: Partial<Todo>,
) {
return await todoRepo.create(givenTodo(todo));
}

export async function givenTodoListInstance(
todoListRepo: TodoListRepository,
data?: Partial<TodoList>,
) {
return await todoListRepo.create(givenTodoList(data));
}
Loading

0 comments on commit 62637cc

Please sign in to comment.