Add tests and functions
Some checks reported errors
continuous-integration/drone/push Build encountered an error
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:
parent
fc4d8c9cdb
commit
0f3746f189
6
jest.config.js
Normal file
6
jest.config.js
Normal 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']
|
||||
};
|
@ -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"
|
||||
},
|
||||
|
7
src/Model/ProfileRequest.ts
Normal file
7
src/Model/ProfileRequest.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export default class ProfileRequest {
|
||||
bio: string;
|
||||
|
||||
constructor(bio: string) {
|
||||
this.bio = bio;
|
||||
}
|
||||
}
|
9
src/Model/UserRequest.ts
Normal file
9
src/Model/UserRequest.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export default class UserRequest {
|
||||
name: string;
|
||||
email: string;
|
||||
|
||||
constructor(name: string, email: string) {
|
||||
this.name = name;
|
||||
this.email = email;
|
||||
}
|
||||
}
|
@ -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 };
|
||||
|
14
src/main.ts
14
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) => {
|
||||
|
@ -1,9 +0,0 @@
|
||||
describe('user', function() {
|
||||
it('createUser', function() {
|
||||
|
||||
})
|
||||
|
||||
it('checkUser', function() {
|
||||
|
||||
})
|
||||
})
|
81
tests/user.test.ts
Normal file
81
tests/user.test.ts
Normal 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();
|
||||
});
|
||||
});
|
@ -6,5 +6,6 @@
|
||||
"lib": ["esnext", "dom"],
|
||||
"esModuleInterop": true,
|
||||
},
|
||||
"include": ["src"]
|
||||
"include": ["src"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user