From 0f3746f18933e98e0e08f6b60c96d4ba61373baf Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 13 Mar 2021 11:27:14 +0700 Subject: [PATCH] Add tests and functions Allow to run jests and add the excepted Add not resolve yet for the async in tests --- jest.config.js | 6 +++ package.json | 1 + src/Model/ProfileRequest.ts | 7 ++++ src/Model/UserRequest.ts | 9 +++++ src/User/UserService.ts | 37 ++++++++--------- src/main.ts | 14 ++++++- test/user.test.ts | 9 ----- tests/user.test.ts | 81 +++++++++++++++++++++++++++++++++++++ tsconfig.json | 3 +- 9 files changed, 137 insertions(+), 30 deletions(-) create mode 100644 jest.config.js create mode 100644 src/Model/ProfileRequest.ts create mode 100644 src/Model/UserRequest.ts delete mode 100644 test/user.test.ts create mode 100644 tests/user.test.ts diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..6661c3e --- /dev/null +++ b/jest.config.js @@ -0,0 +1,6 @@ +module.exports = { + transform: { '^.+\\.ts?$': 'ts-jest' }, + testEnvironment: 'node', + testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$', + moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'] +}; \ No newline at end of file diff --git a/package.json b/package.json index 6d8a321..79aeefe 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,7 @@ "@types/jest": "^26.0.20", "jest": "^26.6.3", "prisma": "2.18.0", + "ts-jest": "^26.5.3", "ts-node": "9.1.1", "typescript": "4.2.3" }, diff --git a/src/Model/ProfileRequest.ts b/src/Model/ProfileRequest.ts new file mode 100644 index 0000000..9638512 --- /dev/null +++ b/src/Model/ProfileRequest.ts @@ -0,0 +1,7 @@ +export default class ProfileRequest { + bio: string; + + constructor(bio: string) { + this.bio = bio; + } +} \ No newline at end of file diff --git a/src/Model/UserRequest.ts b/src/Model/UserRequest.ts new file mode 100644 index 0000000..b8a22d6 --- /dev/null +++ b/src/Model/UserRequest.ts @@ -0,0 +1,9 @@ +export default class UserRequest { + name: string; + email: string; + + constructor(name: string, email: string) { + this.name = name; + this.email = email; + } +} \ No newline at end of file diff --git a/src/User/UserService.ts b/src/User/UserService.ts index 7490698..f34844c 100644 --- a/src/User/UserService.ts +++ b/src/User/UserService.ts @@ -1,9 +1,11 @@ import { PrismaClient, User } from "@prisma/client"; +import ProfileRequest from "../Model/ProfileRequest"; +import UserRequest from "../Model/UserRequest"; const prisma = new PrismaClient(); -async function isProfileExistByEmail(email: string) { - const exist = await prisma.profile.count({ +function isProfileExistByEmail(email: string) { + const exist = prisma.profile.count({ where: { user: { email: email, @@ -11,7 +13,7 @@ async function isProfileExistByEmail(email: string) { }, }); - return exist > 0; + return exist.then((c) => c > 0).catch((e) => 0); } async function findOneProfileByEmail(email: string) { @@ -21,19 +23,22 @@ async function findOneProfileByEmail(email: string) { email: email, }, }, + include: { + user: true, + }, }); return profile; } -async function isExistByEmail(email: string) { - const exist = await prisma.user.count({ +function isExistByEmail(email: string) { + const exist = prisma.user.count({ where: { email: email, }, }); - return exist > 0; + return exist.then((c) => c > 0).catch((e) => 0); } async function findOneByEmail( @@ -53,18 +58,15 @@ async function findOneByEmail( return user; } -async function createUser() { - const email = "ops@cubetiqs.com"; - - if (isExistByEmail(email)) { - console.log("User existed with email => ", email); - return findOneByEmail(email); +async function createUser(request: UserRequest) { + if (isExistByEmail(request.email)) { + return findOneByEmail(request.email); } const user = await prisma.user.create({ data: { - name: "CUBETIQ Solution", - email: email, + name: request.name, + email: request.email, }, }); @@ -73,16 +75,15 @@ async function createUser() { return user; } -async function createProfile(user: User) { +async function createProfile(user: User, request: ProfileRequest) { if (isProfileExistByEmail(user.email)) { - console.log("Profile existed by email => ", user.email); return findOneProfileByEmail(user.email); } const profile = await prisma.profile.create({ data: { userId: user.id, - bio: "Software Company", + bio: request.bio, }, }); @@ -91,4 +92,4 @@ async function createProfile(user: User) { return profile; } -export { createUser, createProfile }; +export { createUser, createProfile, findOneByEmail, findOneProfileByEmail }; diff --git a/src/main.ts b/src/main.ts index 1457a02..0636e12 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,12 +5,22 @@ const prisma = new PrismaClient(); // main function async function main() { + const email = "ops@cubetiqs.com"; + const name = "CUBETIQ Solution"; + // create user - await createUser() + await createUser( + { + name: name, + email: email, + } + ) .then((user) => { if (user != null) { // create profile for user - createProfile(user); + createProfile(user, { + bio: "Software Developer", + }); } }) .catch((e) => { diff --git a/test/user.test.ts b/test/user.test.ts deleted file mode 100644 index 683f991..0000000 --- a/test/user.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -describe('user', function() { - it('createUser', function() { - - }) - - it('checkUser', function() { - - }) -}) \ No newline at end of file diff --git a/tests/user.test.ts b/tests/user.test.ts new file mode 100644 index 0000000..ef97403 --- /dev/null +++ b/tests/user.test.ts @@ -0,0 +1,81 @@ +import { + createUser, + findOneByEmail, + createProfile, + findOneProfileByEmail, +} from "../src/User/UserService"; + +// all tests about user +describe("user", function () { + const email = "sombochea@cubetiqs.com"; + const name = "Sambo Chea"; + const bio = "Software Developer"; + + // create user test + it("createUser", async (done) => { + // create user + await createUser({ + email: email, + name: name, + }) + .then((user) => { + if (user == null) { + throw Error("user is null"); + } + + expect(user).not.toBeNull(); + expect(email).toBe(user.email); + expect(name).toBe(user.name); + + // create profile + createProfile(user, { + bio: "Software Developer", + }) + .then((profile) => { + if (profile == null) { + throw Error("profile is null"); + } + + expect(profile).not.toBeNull(); + expect(bio).toBe(profile.bio); + }) + .catch((e) => console.error(e)); + }) + .catch((e) => console.error(e)); + }); + + // check user test + it("checkUser", async (done) => { + await findOneByEmail(email) + .then((user) => { + if (user == null) { + throw Error("user is null"); + } + + expect(user).not.toBeNull(); + expect(email).toBe(user.email); + expect(name).toBe(user.name); + }) + .catch((e) => console.error(e)); + + done(); + }); + + // check profile included user test + it("checkProfileUser", async (done) => { + await findOneProfileByEmail(email) + .then((profile) => { + if (profile == null) { + throw Error("profile is null"); + } + + expect(profile).not.toBeNull(); + expect(bio).toBe(profile.bio); + expect(email).toBe(profile.user.email); + expect(name).toBe(profile.user.name); + }) + .catch((e) => console.error(e)); + + done(); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 6474783..05597c9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,5 +6,6 @@ "lib": ["esnext", "dom"], "esModuleInterop": true, }, - "include": ["src"] + "include": ["src"], + "exclude": ["node_modules"] }