Skip to content

Commit

Permalink
refactor: simplify test setup
Browse files Browse the repository at this point in the history
Signed-off-by: virkt25 <taranveer@virk.cc>
  • Loading branch information
virkt25 committed Sep 25, 2018
1 parent 010a3ac commit a79d375
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 89 deletions.
30 changes: 4 additions & 26 deletions test/acceptance/ping.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,16 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, supertest} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {supertest} from '@loopback/testlab';
import {ShoppingApplication} from '../..';
import {setupApplication} from './helper';

describe('PingController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;

before(givenAnApplication);

before(givenARestServer);

before(async () => {
await app.boot();
await app.start();
});

before(() => {
client = createClientForHandler(server.requestHandler);
before('setupApplication', async () => {
({app, client} = await setupApplication());
});

after(async () => {
Expand All @@ -32,16 +22,4 @@ describe('PingController', () => {
it('invokes GET /ping', async () => {
await client.get('/ping?msg=world').expect(200);
});

function givenAnApplication() {
app = new ShoppingApplication({
rest: {
port: 0,
},
});
}

async function givenARestServer() {
server = await app.getServer(RestServer);
}
});
43 changes: 10 additions & 33 deletions test/acceptance/shopping-cart.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,23 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {supertest, expect} from '@loopback/testlab';
import {ShoppingApplication} from '../..';
import {ShoppingCartRepository} from '../../src/repositories';
import {RedisDataSource} from '../../src/datasources';
import {ShoppingCart, ShoppingCartItem} from '../../src/models';
import {setupApplication} from './helper';

describe('ShoppingCartController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;
const cartRepo = new ShoppingCartRepository(new RedisDataSource());

before(givenAnApplication);

before(givenARestServer);

before(async () => {
await app.boot();
await app.start();
});

before(() => {
client = createClientForHandler(server.requestHandler);
before('setupApplication', async () => {
({app, client} = await setupApplication());
});

beforeEach(clearDatabase);

after(async () => {
await app.stop();
});
Expand All @@ -41,7 +30,7 @@ describe('ShoppingCartController', () => {
.put(`/shoppingCarts/${cart.userId}`)
.set('Content-Type', 'application/json')
.send(cart)
.expect(200);
.expect(204);
});

it('throws error if userId does not match the cart', async () => {
Expand All @@ -59,7 +48,7 @@ describe('ShoppingCartController', () => {
await client
.put(`/shoppingCarts/${cart.userId}`)
.send(cart)
.expect(200);
.expect(204);
await client
.get(`/shoppingCarts/${cart.userId}`)
.expect(200, cart.toJSON());
Expand All @@ -71,13 +60,13 @@ describe('ShoppingCartController', () => {
await client
.put(`/shoppingCarts/${cart.userId}`)
.send(cart)
.expect(200);
.expect(204);
// Now we can see it
await client
.get(`/shoppingCarts/${cart.userId}`)
.expect(200, cart.toJSON());
// Delete the shopping cart
await client.del(`/shoppingCarts/${cart.userId}`).expect(200);
await client.del(`/shoppingCarts/${cart.userId}`).expect(204);
// Now it's gone
await client.get(`/shoppingCarts/${cart.userId}`).expect(404);
});
Expand All @@ -89,30 +78,18 @@ describe('ShoppingCartController', () => {
await client
.put(`/shoppingCarts/${cart.userId}`)
.send(cart)
.expect(200);
.expect(204);
// Now we can see it
await client
.post(`/shoppingCarts/${cart.userId}/items`)
.send(newItem)
.expect(200);
.expect(204);
const newCart = (await client
.get(`/shoppingCarts/${cart.userId}`)
.expect(200)).body;
expect(newCart.items).to.containEql(newItem.toJSON());
});

function givenAnApplication() {
app = new ShoppingApplication({
rest: {
port: 0,
},
});
}

async function givenARestServer() {
server = await app.getServer(RestServer);
}

async function clearDatabase() {
await cartRepo.deleteAll();
}
Expand Down
39 changes: 9 additions & 30 deletions test/acceptance/user.controller.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {createClientForHandler, supertest, expect} from '@loopback/testlab';
import {RestServer} from '@loopback/rest';
import {supertest, expect} from '@loopback/testlab';
import {ShoppingApplication} from '../..';
import {UserRepository} from '../../src/repositories';
import {UserDataSource} from '../../src/datasources';
import {UserRepository, OrderRepository} from '../../src/repositories';
import {MongoDataSource} from '../../src/datasources';
import {setupApplication} from './helper';

describe('UserController', () => {
let app: ShoppingApplication;
let server: RestServer;
let client: supertest.SuperTest<supertest.Test>;
const userRepo = new UserRepository(new UserDataSource());
const orderRepo = new OrderRepository(new MongoDataSource());
const userRepo = new UserRepository(new MongoDataSource(), orderRepo);

const user = {
email: 'test@loopback.io',
Expand All @@ -22,21 +22,11 @@ describe('UserController', () => {
surname: 'User',
};

before(givenAnApplication);

before(givenARestServer);

before(async () => {
await app.boot();
await app.start();
});

before(() => {
client = createClientForHandler(server.requestHandler);
before('setupApplication', async () => {
({app, client} = await setupApplication());
});

beforeEach(clearDatabase);

after(async () => {
await app.stop();
});
Expand Down Expand Up @@ -109,25 +99,14 @@ describe('UserController', () => {
it('returns a user with given id when GET /user/{id} is invoked', async () => {
const newUser = await userRepo.create(user);
delete newUser.password;
delete newUser.orders;
// MongoDB returns an id object we need to convert to string
// since the REST API returns a string for the id property.
newUser.id = newUser.id.toString();

await client.get(`/users/${newUser.id}`).expect(200, newUser.toJSON());
});

function givenAnApplication() {
app = new ShoppingApplication({
rest: {
port: 0,
},
});
}

async function givenARestServer() {
server = await app.getServer(RestServer);
}

async function clearDatabase() {
await userRepo.deleteAll();
}
Expand Down

0 comments on commit a79d375

Please sign in to comment.