From 689b3cfa80af91c703d3c28999b6eba9eafb8252 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Wed, 6 Jul 2022 12:35:22 +0200 Subject: [PATCH] update test --- .../db.unit.test.js | 22 +++++++++---------- .../gitlab.unit.test.js | 22 +++++++++++-------- __tests/public.endpoint.test.js | 16 ++++++++++++++ jest.config.js | 4 ++++ 4 files changed, 44 insertions(+), 20 deletions(-) rename __tests__/method.unit.test.ts => __tests/db.unit.test.js (54%) rename __tests__/gitlab.unit.test.ts => __tests/gitlab.unit.test.js (52%) create mode 100644 __tests/public.endpoint.test.js create mode 100644 jest.config.js diff --git a/__tests__/method.unit.test.ts b/__tests/db.unit.test.js similarity index 54% rename from __tests__/method.unit.test.ts rename to __tests/db.unit.test.js index deb347e6..f1d8d697 100644 --- a/__tests__/method.unit.test.ts +++ b/__tests/db.unit.test.js @@ -1,50 +1,50 @@ -import methods from '../functions/methods' +const dbController = require('../../src/controller/dbController') describe('DB methohds test', () => { it('returns a user from DB by email', async () => { - const user = await methods.getUserByEmail('litehon958@whipjoy.com') + const user = await dbController.getUserByEmail('litehon958@whipjoy.com') expect(user).not.toBeNull() }) it('returns a null user', async () => { - const user = await methods.getUserByEmail('jondoe@nowhere.com') // a non-exist user + const user = await dbController.getUserByEmail('jondoe@nowhere.com') // a non-exist user expect(user).toBeNull() }) it("returns a user's email", async () => { - const email = await methods.getUserEmailById(1) + const email = await dbController.getUserEmailById(1) expect(email).not.toBeNull() }) it("returns null instead of a user's email", async () => { - const email = await methods.getUserEmailById(1005) // no user has this ID + const email = await dbController.getUserEmailById(1005) // no user has this ID expect(email).toBeNull() }) it('returns null from DB by token', async () => { - const user = await methods.getUserByToken('12345678') // unvalid token + const user = await dbController.getUserByToken('12345678') // unvalid token expect(user).toBeNull() // for valid token = expect(user).not.toBeNull() }) it("returns a user's verification token, if any", async () => { - const token = await methods.getVerificationTokenByUserId(1) + const token = await dbController.getVerificationTokenByUserId(1) expect(token).toBeNull() }) it("returns a user's ID, if any", async () => { - const token = await methods.getUserIdByVerificationToken('12345678') // unvalid token + const token = await dbController.getUserIdByVerificationToken('12345678') // unvalid token expect(token).toBeNull() // for valid token = expect(user).not.toBeNull() }) it("returns a user's GitLab_ID, if any", async () => { - const id = await methods.getGitlabId(1) + const id = await dbController.getGitlabId(1) expect(id).not.toBeNull() }) it('checks user email', async () => { - const user = await methods.checkUserEmail('litehon958@whipjoy.com') + const user = await dbController.checkUserEmail('litehon958@whipjoy.com') expect(user).not.toBeNull() }) it('checks user email and return null', async () => { - const user = await methods.checkUserEmail('jondoe@nowhere.com') // a non-exist user + const user = await dbController.checkUserEmail('jondoe@nowhere.com') // a non-exist user expect(user).toBeNull() }) }) diff --git a/__tests__/gitlab.unit.test.ts b/__tests/gitlab.unit.test.js similarity index 52% rename from __tests__/gitlab.unit.test.ts rename to __tests/gitlab.unit.test.js index df8aa3a3..babb5689 100644 --- a/__tests__/gitlab.unit.test.ts +++ b/__tests/gitlab.unit.test.js @@ -1,32 +1,36 @@ -import gitlab from '../functions/gitlab' -// const axios = require('axios') -// jest.mock('axios') +const gitlabController = require('../src/controller/gitlabController') +const axios = require('axios') + +jest.mock('axios') describe('GitLab API', () => { test('returns an existing gitlab user by an email address', async () => { - const user = await gitlab.getUserByEmail('litehon958@whipjoy.com') + axios.get.mockResolvedValue({ + + }) + const user = await gitlabController.getUserByEmail('litehon958@whipjoy.com') expect(user).not.toBeNull() }) test('returns an undefined user', async () => { - const user = await gitlab.getUserByEmail('johndoe@nowhere.com') + const user = await gitlabController.getUserByEmail('johndoe@nowhere.com') expect(user).toBeUndefined() }) test('returns users project', async () => { - const userProjects = await gitlab.getUserProjects(136) + const userProjects = await gitlabController.getUserProjects(136) expect(userProjects).toBeDefined() }) test('returns undefined projects, due to non-existing gitlab user ID', async () => { - const userProjects = await gitlab.getUserProjects(0) + const userProjects = await gitlabController.getUserProjects(0) expect(userProjects).toBeUndefined() }) test('returns a project by ID', async () => { - const project = await gitlab.getProjectById(13) // m4lab_landing_page + const project = await gitlabController.getProjectById(13) // m4lab_landing_page expect(project).toBeDefined() }) test('returns undefined, due to invalid project ID', async () => { - const project = await gitlab.getProjectById(0) + const project = await gitlabController.getProjectById(0) expect(project).toBeUndefined() }) }) diff --git a/__tests/public.endpoint.test.js b/__tests/public.endpoint.test.js new file mode 100644 index 00000000..e9f52bab --- /dev/null +++ b/__tests/public.endpoint.test.js @@ -0,0 +1,16 @@ +const request = require('supertest') +const express = require('express') + +const app = express() +app.set('port', 9989) + +describe('Test endpoint(s)', () => { + it('should return a 200 status code', () => { + request(app) + .get('/contact') + .expect(200) + .end(function (err, res) { + if (err) throw err + }) + }) +}) diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 00000000..758fa133 --- /dev/null +++ b/jest.config.js @@ -0,0 +1,4 @@ +module.exports = { + preset: 'ts-jest', + testEnvironment: 'node' +} -- GitLab