Skip to content

Commit

Permalink
Refactor user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kevynsax committed Jun 29, 2020
1 parent 253d001 commit e1bd15c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 59 deletions.
14 changes: 5 additions & 9 deletions src/controllers/UserController.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import {IUserApp} from "src/model/user/UserApp";
import {Request, Response} from "express";
import {httpStatusCode} from "src/constants";
import {CreateUserViewModel, UserViewModel} from "src/viewModel/userViewModels";
import {Endpoint} from "src/middleware/injectHelpers";

export class UserController {

constructor(private app: IUserApp){}

public insertUser = async (req: Request, res: Response): Promise<void> =>{
public insertUser: Endpoint = async (req, res): Promise<void> =>{
const {ipAddress} = req.body as CreateUserViewModel;

await this.app.createUser(ipAddress)
.then((id) => {
const result: UserViewModel = {id};
res.status(httpStatusCode.success).send(result);
})
.catch((err) => res.status(httpStatusCode.internalServerError).send(err.message));
const result = this.app.createUser(ipAddress)
.then(id => ({id} as UserViewModel));
return res.unwrap(result);
}
}
83 changes: 33 additions & 50 deletions src/tests/controllers/UserController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,62 +5,45 @@ import {UserController} from "src/controllers/UserController";
import {CreateUserViewModel, UserViewModel} from "src/viewModel/userViewModels";
import {ResumeResponse} from "src/middleware/injectHelpers";

const fakeRequestUser: CreateUserViewModel = {
ipAddress: "192.404.303.022",
};

const fakeReturnUser: UserViewModel = {
id: "abacate",
};

describe('Insert User', () => {
test('Happy path', async () => {
const fakeRequestUser: CreateUserViewModel = {
ipAddress: "192.404.303.022",
};

const fakeReturnUser: UserViewModel = {
id: "abacate",
};
let target: UserController;
let fakeApp: IUserApp;
let fakeResponse: ResumeResponse;

beforeEach(() => {
const fakeUnwrap = jest.fn()
.mockImplementationOnce(x => x);

// given
fakeResponse = {
unwrap: fakeUnwrap
} as unknown as ResumeResponse;


const createUser = jest.fn()
.mockImplementationOnce(x => new Promise<string>(resolve => resolve(fakeReturnUser.id)));
.mockImplementationOnce(x =>
new Promise<string>(resolve => resolve(fakeReturnUser.id)));
fakeApp = {createUser} as IUserApp;
target = new UserController(fakeApp);
});

const target = new UserController({createUser} as IUserApp);

const send = jest.fn();

test('Happy path', async () => {
// given
const req = {body: fakeRequestUser} as Request;
const res = {send} as unknown as ResumeResponse;

const status = jest.fn()
.mockImplementationOnce(() => res);

res.status = status;



// when
await target.insertUser(req, res);
const result = await target.insertUser(req, fakeResponse);

// then
expect(status).toBeCalledWith(httpStatusCode.success);
expect(send).toBeCalledWith(fakeReturnUser);
expect(createUser).toBeCalledWith(fakeRequestUser.ipAddress);
expect(result).toEqual(fakeReturnUser);
expect(fakeApp.createUser).toBeCalledWith(fakeRequestUser.ipAddress);
});


test('Error on App', async () => {
// given
const errorMessage = "Error on app user";
const createUser = jest.fn()
.mockImplementationOnce(() => new Promise<string>((_, reject) => reject(new Error(errorMessage))));
const target = new UserController({createUser} as IUserApp);

const send = jest.fn();
const res = {send} as unknown as ResumeResponse;

const status = jest.fn().mockImplementationOnce(() => res);
res.status = status;

// when
await target.insertUser({body: {}} as Request, res);

// then
expect(send).toBeCalledWith(errorMessage);
expect(status).toBeCalledWith(httpStatusCode.internalServerError);

})

});

0 comments on commit e1bd15c

Please sign in to comment.