-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: virkt25 <[email protected]>
- Loading branch information
Showing
3 changed files
with
18 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: '[email protected]', | ||
|
@@ -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(); | ||
}); | ||
|
@@ -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(); | ||
} | ||
|