Skip to content

Commit

Permalink
fixup! improve integration tests setup
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Jun 21, 2019
1 parent 04e7d44 commit 0ad1a23
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 44 deletions.
1 change: 1 addition & 0 deletions examples/todo-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@loopback/build": "^2.0.1",
"@loopback/eslint-config": "^1.1.2",
"@loopback/http-caching-proxy": "^1.1.3",
"@loopback/repository": "^1.8.0",
"@loopback/testlab": "^1.6.1",
"@types/lodash": "^4.14.134",
"@types/node": "^10.14.9",
Expand Down
28 changes: 28 additions & 0 deletions examples/todo-list/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {juggler} from '@loopback/repository';
import {givenHttpServerConfig} from '@loopback/testlab';
import {TodoListApplication} from '../application';
import {Todo, TodoList, TodoListImage} from '../models';
Expand Down Expand Up @@ -130,3 +131,30 @@ export async function givenTodoListImageInstance(
) {
return await todoListImageRepo.create(givenTodoListImage(data));
}

export async function givenEmptyDatabase() {
const todoRepo: TodoRepository = new TodoRepository(
testdb,
async () => todoListRepo,
);

const todoListRepo: TodoListRepository = new TodoListRepository(
testdb,
async () => todoRepo,
async () => todoListImageRepo,
);

const todoListImageRepo: TodoListImageRepository = new TodoListImageRepository(
testdb,
async () => todoListRepo,
);

await todoRepo.deleteAll();
await todoListRepo.deleteAll();
await todoListImageRepo.deleteAll();
}

export const testdb: juggler.DataSource = new juggler.DataSource({
name: 'db',
connector: 'memory',
});
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
import {expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {TodoListImageRepository, TodoListRepository} from '../../repositories';
import {
givenRunningApplicationWithCustomConfiguration,
TodoListImageRepository,
TodoListRepository,
TodoRepository,
} from '../../repositories';
import {
givenEmptyDatabase,
givenTodoListImageInstance,
givenTodoListInstance,
givenTodoListRepositories,
testdb,
} from '../helpers';

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

before(async () => {
app = await givenRunningApplicationWithCustomConfiguration();
todoListRepo = new TodoListRepository(
testdb,
async () => todoRepo,
async () => todoListImageRepo,
);
todoListImageRepo = new TodoListImageRepository(
testdb,
async () => todoListRepo,
);
});
after(() => app.stop());

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

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

it('includes TodoList in find method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import {expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {TodoListRepository, TodoRepository} from '../../repositories';
import {
givenRunningApplicationWithCustomConfiguration,
TodoListImageRepository,
TodoListRepository,
TodoRepository,
} from '../../repositories';
import {
givenEmptyDatabase,
givenTodoInstance,
givenTodoListInstance,
givenTodoRepositories,
testdb,
} from '../helpers';

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

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

before(async () => {
({todoRepo, todoListRepo} = await givenTodoRepositories(app));
todoListRepo = new TodoListRepository(
testdb,
async () => todoRepo,
async () => todoListImageRepo,
);
todoRepo = new TodoRepository(testdb, async () => todoListRepo);
});

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

it('includes Todos in find method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
import {expect, toJSON} from '@loopback/testlab';
import {TodoListApplication} from '../../application';
import {TodoListRepository, TodoRepository} from '../../repositories';
import {
givenRunningApplicationWithCustomConfiguration,
TodoListImageRepository,
TodoListRepository,
TodoRepository,
} from '../../repositories';
import {
givenEmptyDatabase,
givenTodoInstance,
givenTodoListInstance,
givenTodoRepositories,
testdb,
} from '../helpers';

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

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

before(async () => {
({todoRepo, todoListRepo} = await givenTodoRepositories(app));
todoListRepo = new TodoListRepository(
testdb,
async () => todoRepo,
async () => todoListImageRepo,
);
todoRepo = new TodoRepository(testdb, async () => todoListRepo);
});

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

it('includes TodoList in find method result', async () => {
const list = await givenTodoListInstance(todoListRepo);
Expand Down

0 comments on commit 0ad1a23

Please sign in to comment.