From 70638bdb716577e5ffaf98cd2fc54f96bdc8a1c0 Mon Sep 17 00:00:00 2001 From: Rosanny <rosanny.sihombing@hft-stuttgart.de> Date: Mon, 8 Mar 2021 21:40:30 +0100 Subject: [PATCH] update unit test --- __tests__/gitlab.unit.test.js | 92 +++++++++++++++++++++------ __tests__/method.unit.test.js | 79 ++++++++++++----------- __tests__/routes-account.unit.test.js | 5 -- 3 files changed, 110 insertions(+), 66 deletions(-) delete mode 100644 __tests__/routes-account.unit.test.js diff --git a/__tests__/gitlab.unit.test.js b/__tests__/gitlab.unit.test.js index c4cca338..65d02b22 100644 --- a/__tests__/gitlab.unit.test.js +++ b/__tests__/gitlab.unit.test.js @@ -1,29 +1,79 @@ const gitlab = require('../routes/gitlab') -const axios = require('axios') +//const axios = require('axios') +//jest.mock('axios') -beforeAll(() => { - // test gitlab API connection - var config = { - method: 'get', - url: 'https://transfer.hft-stuttgart.de/gitlab/api/v4/projects' - } - - axios(config) - .then(function (response) { - expect(response.statusCode).toBe(200) +describe('GitLab API', () => { + + /* + test('jest.fn recalls what it has been called with', () => { + const mock = jest.fn() + mock('a', 'b', 'c') + expect(mock).toHaveBeenCalledTimes(1) + expect(mock).toHaveBeenCalledWith('a', 'b', 'c') + }); */ + + // mock +/* it('returns an existing gitlab-userID by an email address', done => { + let resp = { + error: false, + data: 1} + axios.get.mockResolvedValue(resp) + + gitlab.getUserIdByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp){ + try { + expect(resp.error).toBeFalsy() + expect(resp.data).not.toBeNull() + done() + } catch (error) { + done(error) + } + }) + }) +*/ + + it('returns an existing gitlab-userID by an email address', done => { + gitlab.getUserIdByEmail('rosanny.sihombing@hft-stuttgart.de', function(data){ + try { + expect(data.error).toBeFalsy() + expect(data.data).not.toBeNull() + done() + } catch (error) { + done(error) + } + }) + }) + + it('returns an error due to the non-exist user', done => { + gitlab.getUserIdByEmail('test@hft-stuttgart.com', function (data) { + try { + expect(data.error).toBeTruthy() + done() + } catch (error) { + done(error) + } }) -}) + }) -describe('GitLab API test', () => { - test("Get a gitlab userID by email", () => { // email = any email address - gitlab.getUserIdByEmail("test@email.de", function(resp){ - expect(resp).not.toBeNull() - }) + it('returns the projects of a particular userId', done => { + gitlab.getUserProjects(3, function (data) { + try { + expect(data.error).toBeFalsy() + expect(data.data).not.toBeNull() + done() + } catch (error) { + done(error) + } }) + }) - test("Get the projects of a particular user", () => { // userID = any integer - gitlab.getUserProjects(3, function(resp){ - expect(resp).not.toBeNull() - }) + it('returns an error due to the wrong userID', done => { + gitlab.getUserProjects('abc', function (data) { + try { + expect(data.error).toBeTruthy() + done() + } catch (error) { + done(error) + } }) + }) }) \ No newline at end of file diff --git a/__tests__/method.unit.test.js b/__tests__/method.unit.test.js index 9c856e1d..9ad00d8e 100644 --- a/__tests__/method.unit.test.js +++ b/__tests__/method.unit.test.js @@ -1,53 +1,52 @@ -const dbconn = require('../routes/dbconn') const methods = require('../routes/methods') -beforeAll(() => { - // test DB connection - dbconn.user.query('SELECT 1 + 5 AS solution', function (err, rows) { - expect(err).toBeNull() - expect(rows[0].solution).toBe(6) - }) - dbconn.project.query('SELECT 10 + 5 AS solution', function (err, rows) { - expect(err).toBeNull() - expect(rows[0].solution).toBe(15) - }) -}) - describe("DB methohds test", () => { -/* - test("Get a user from DB by email", () => { - let gitlabUserData = "to-be-defined" - methods.addGitlabUser(gitlabUserData, function(data, err){ - expect(data).not.toBeNull() - expect(err).toBeNull() - }) - }) */ - - test("Get a user from DB by email", () => { // email = any email address - methods.getUserByEmail("test@email.de", function(data, err){ - expect(data).not.toBeNull() - expect(err).toBeNull() - }) + + it('returns a user from DB by email', done => { + methods.getUserByEmail('rosanny.sihombing@hft-stuttgart.de', function(resp, err){ + try { + expect(resp).not.toBeNull() + expect(err).toBeNull() + done() + } catch (error) { + done(error) + } + }) }) - test("Get a user from DB by ID", () => { // ID = any integer - methods.getUserById(100, function(data, err){ - expect(data).not.toBeNull() - expect(err).toBeNull() - }) + it("returns a user from DB by ID", done => { + methods.getUserById(10, function(resp, err){ + try { + expect(resp).not.toBeNull() + expect(err).toBeNull() + done() + } catch (error) { + done(error) + } + }) }) - test("Check user email", () => { // email = any email address - methods.checkUserEmail("test@email.de", function(err, data){ - expect(data).not.toBeNull() - expect(err).toBeNull() + it("checks user email", done => { + methods.checkUserEmail("test@email.de", function(err, resp){ + try { + expect(resp).not.toBeNull() + expect(err).toBeNull() + done() + } catch (error) { + done(error) + } }) }) - test("Get a user by token", () => { // token = any alphanumeric - methods.checkUserEmail("1abc0qwerty", function(err, data){ - expect(data).not.toBeNull() - expect(err).toBeNull() + it("returns a user by token", done => { + methods.checkUserEmail("1abc0qwerty", function(err, resp){ // token = any alphanumeric + try { + expect(resp).not.toBeNull() + expect(err).toBeNull() + done() + } catch (error) { + done(error) + } }) }) }) diff --git a/__tests__/routes-account.unit.test.js b/__tests__/routes-account.unit.test.js deleted file mode 100644 index 81f63035..00000000 --- a/__tests__/routes-account.unit.test.js +++ /dev/null @@ -1,5 +0,0 @@ -describe('Sample Test', () => { - it('should test that true === true', () => { - expect(true).toBe(true) - }) -}) \ No newline at end of file -- GitLab