Add tests and functions
Some checks reported errors
continuous-integration/drone/push Build encountered an error

Allow to run jests and add the excepted
Add not resolve yet for the async in tests
This commit is contained in:
Sambo Chea 2021-03-13 11:27:14 +07:00
parent fc4d8c9cdb
commit 0f3746f189
9 changed files with 137 additions and 30 deletions

6
jest.config.js Normal file
View File

@ -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']
};

View File

@ -6,6 +6,7 @@
"@types/jest": "^26.0.20", "@types/jest": "^26.0.20",
"jest": "^26.6.3", "jest": "^26.6.3",
"prisma": "2.18.0", "prisma": "2.18.0",
"ts-jest": "^26.5.3",
"ts-node": "9.1.1", "ts-node": "9.1.1",
"typescript": "4.2.3" "typescript": "4.2.3"
}, },

View File

@ -0,0 +1,7 @@
export default class ProfileRequest {
bio: string;
constructor(bio: string) {
this.bio = bio;
}
}

9
src/Model/UserRequest.ts Normal file
View File

@ -0,0 +1,9 @@
export default class UserRequest {
name: string;
email: string;
constructor(name: string, email: string) {
this.name = name;
this.email = email;
}
}

View File

@ -1,9 +1,11 @@
import { PrismaClient, User } from "@prisma/client"; import { PrismaClient, User } from "@prisma/client";
import ProfileRequest from "../Model/ProfileRequest";
import UserRequest from "../Model/UserRequest";
const prisma = new PrismaClient(); const prisma = new PrismaClient();
async function isProfileExistByEmail(email: string) { function isProfileExistByEmail(email: string) {
const exist = await prisma.profile.count({ const exist = prisma.profile.count({
where: { where: {
user: { user: {
email: email, 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) { async function findOneProfileByEmail(email: string) {
@ -21,19 +23,22 @@ async function findOneProfileByEmail(email: string) {
email: email, email: email,
}, },
}, },
include: {
user: true,
},
}); });
return profile; return profile;
} }
async function isExistByEmail(email: string) { function isExistByEmail(email: string) {
const exist = await prisma.user.count({ const exist = prisma.user.count({
where: { where: {
email: email, email: email,
}, },
}); });
return exist > 0; return exist.then((c) => c > 0).catch((e) => 0);
} }
async function findOneByEmail( async function findOneByEmail(
@ -53,18 +58,15 @@ async function findOneByEmail(
return user; return user;
} }
async function createUser() { async function createUser(request: UserRequest) {
const email = "ops@cubetiqs.com"; if (isExistByEmail(request.email)) {
return findOneByEmail(request.email);
if (isExistByEmail(email)) {
console.log("User existed with email => ", email);
return findOneByEmail(email);
} }
const user = await prisma.user.create({ const user = await prisma.user.create({
data: { data: {
name: "CUBETIQ Solution", name: request.name,
email: email, email: request.email,
}, },
}); });
@ -73,16 +75,15 @@ async function createUser() {
return user; return user;
} }
async function createProfile(user: User) { async function createProfile(user: User, request: ProfileRequest) {
if (isProfileExistByEmail(user.email)) { if (isProfileExistByEmail(user.email)) {
console.log("Profile existed by email => ", user.email);
return findOneProfileByEmail(user.email); return findOneProfileByEmail(user.email);
} }
const profile = await prisma.profile.create({ const profile = await prisma.profile.create({
data: { data: {
userId: user.id, userId: user.id,
bio: "Software Company", bio: request.bio,
}, },
}); });
@ -91,4 +92,4 @@ async function createProfile(user: User) {
return profile; return profile;
} }
export { createUser, createProfile }; export { createUser, createProfile, findOneByEmail, findOneProfileByEmail };

View File

@ -5,12 +5,22 @@ const prisma = new PrismaClient();
// main function // main function
async function main() { async function main() {
const email = "ops@cubetiqs.com";
const name = "CUBETIQ Solution";
// create user // create user
await createUser() await createUser(
{
name: name,
email: email,
}
)
.then((user) => { .then((user) => {
if (user != null) { if (user != null) {
// create profile for user // create profile for user
createProfile(user); createProfile(user, {
bio: "Software Developer",
});
} }
}) })
.catch((e) => { .catch((e) => {

View File

@ -1,9 +0,0 @@
describe('user', function() {
it('createUser', function() {
})
it('checkUser', function() {
})
})

81
tests/user.test.ts Normal file
View File

@ -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();
});
});

View File

@ -6,5 +6,6 @@
"lib": ["esnext", "dom"], "lib": ["esnext", "dom"],
"esModuleInterop": true, "esModuleInterop": true,
}, },
"include": ["src"] "include": ["src"],
"exclude": ["node_modules"]
} }