Created
November 18, 2018 21:05
-
-
Save mksglu/837202c1048687ad33b4d1dee01bd29c to your computer and use it in GitHub Desktop.
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
process.env.NODE_ENV = "test"; | |
import * as mongoose from "mongoose"; | |
import config from "../../config"; | |
import { ALREADY_MEMBER_EMAIL, INVITE_EMAIL, mockUser } from "../../utils/test.utils"; | |
import userService from "./users.service"; | |
describe("Users Service", () => { | |
let _mailConfirm: string; | |
let userToken: any = {}; | |
beforeAll(done => { | |
mongoose.connect( | |
config.connectionStr.dev, | |
done | |
); | |
}); | |
const user = { ...mockUser, email: "[email protected]" }; | |
describe("/POST users", () => { | |
it("it should add a new user", done => { | |
userService.createUser(user).then(res => { | |
const { _id, accounts, firstName, lastName, defaultAccount, email, mailConfirm, state, password } = res.data; | |
userToken.id = res.data._id; | |
_mailConfirm = res.data.mailConfirm; | |
expect(res.status).toBe(true); | |
expect(_id).toEqual("b5d86dc400f0caec2362354bfb29775b"); | |
expect(firstName).toEqual("Mert"); | |
expect(lastName).toEqual("Koseoglu"); | |
expect(defaultAccount).toEqual("2dea0389acd42e4fd5e0e1393fb99607"); | |
expect(email).toEqual("[email protected]"); | |
expect(mailConfirm).toBeDefined(); | |
expect(state).toEqual(0); | |
expect(password).toBeUndefined(); | |
expect(accounts).toEqual(expect.arrayContaining([])); | |
done(); | |
}); | |
}); | |
it("it should be login", done => { | |
userService.loginUser(user).then(res => { | |
expect(res.status).toBe(true); | |
expect(res.data.token).toBeDefined(); | |
done(); | |
}); | |
}); | |
}); | |
describe("/GET users", () => { | |
it("it should be return a user by the given id", done => { | |
userService.getUser(userToken.id, userToken.id).then(res => { | |
const { _id, email, password } = res.data; | |
expect(res.status).toBe(true); | |
expect(_id).toEqual("b5d86dc400f0caec2362354bfb29775b"); | |
expect(email).toEqual("[email protected]"); | |
expect(password).toBeUndefined(); | |
done(); | |
}); | |
}); | |
}); | |
describe("/PUT users", () => { | |
it("it should send email to invite user", done => { | |
userService.mailConfirm(_mailConfirm).then(res => { | |
expect(res.status).toBe(true); | |
expect(res.data.token).toBeDefined(); | |
userService.getUser(userToken.id, userToken.id).then(res => { | |
expect(res.data.state).toEqual(1); | |
}); | |
done(); | |
}); | |
}); | |
}); | |
afterAll(done => { | |
mongoose.connection.dropDatabase(() => { | |
mongoose.connection.close(() => { | |
done(); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment