From 14886d42e8a67d5dd379402d5a6f9885fc741f81 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 18 Oct 2023 00:43:16 +0200 Subject: [PATCH 1/3] test: Clear all jest mocks with after each global hook --- packages/backend/test/setup/global-hooks.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index bec2d0d9..e83f6204 100644 --- a/packages/backend/test/setup/global-hooks.ts +++ b/packages/backend/test/setup/global-hooks.ts @@ -21,6 +21,8 @@ global.beforeEach(async () => { global.afterEach(async () => { await global.knex.rollback(); Model.knex(knex); + + jest.clearAllMocks(); }); global.afterAll(async () => { From b2205097dabff23904e235e3ece4f5fb43efa4d7 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 18 Oct 2023 15:45:54 +0200 Subject: [PATCH 2/3] test: Implement the structure of mocking appConfig options --- packages/backend/test/setup/global-hooks.ts | 11 +++++++++++ packages/backend/test/setup/set-mock-config.ts | 13 +++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 packages/backend/test/setup/set-mock-config.ts diff --git a/packages/backend/test/setup/global-hooks.ts b/packages/backend/test/setup/global-hooks.ts index e83f6204..80d72630 100644 --- a/packages/backend/test/setup/global-hooks.ts +++ b/packages/backend/test/setup/global-hooks.ts @@ -1,6 +1,16 @@ import { Model } from 'objection'; import { client as knex } from '../../src/config/database'; import logger from '../../src/helpers/logger'; +import { mockConfigState, resetMockConfig } from './set-mock-config'; + +jest.mock('../../src/config/app', () => ({ + ...jest.requireActual('../../src/config/app').default, + get isCloud() { + return mockConfigState.isCloud !== undefined + ? mockConfigState.isCloud + : jest.requireActual('../../src/config/app').default.isCloud; + }, +})); global.beforeAll(async () => { global.knex = null; @@ -23,6 +33,7 @@ global.afterEach(async () => { Model.knex(knex); jest.clearAllMocks(); + resetMockConfig(); }); global.afterAll(async () => { diff --git a/packages/backend/test/setup/set-mock-config.ts b/packages/backend/test/setup/set-mock-config.ts new file mode 100644 index 00000000..954a93e3 --- /dev/null +++ b/packages/backend/test/setup/set-mock-config.ts @@ -0,0 +1,13 @@ +export const mockConfigState = { + isCloud: false, +}; + +export const resetMockConfig = () => { + for (const key in mockConfigState) { + delete (mockConfigState as any)[key]; + } +}; + +export const setMockConfig = (config: Partial) => { + Object.assign(mockConfigState, config); +}; From 76e442940bac75707b1bb72d3ccf193017aa7492 Mon Sep 17 00:00:00 2001 From: Faruk AYDIN Date: Wed, 18 Oct 2023 15:46:08 +0200 Subject: [PATCH 3/3] test: Add getAutomatischInfo graphQL query tests --- .../queries/get-automatisch-info.test.ts | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 packages/backend/src/graphql/queries/get-automatisch-info.test.ts diff --git a/packages/backend/src/graphql/queries/get-automatisch-info.test.ts b/packages/backend/src/graphql/queries/get-automatisch-info.test.ts new file mode 100644 index 00000000..0e72f2e8 --- /dev/null +++ b/packages/backend/src/graphql/queries/get-automatisch-info.test.ts @@ -0,0 +1,120 @@ +import { setMockConfig } from '../../../test/setup/set-mock-config'; +import request from 'supertest'; +import app from '../../app'; +import * as license from '../../helpers/license.ee'; + +describe('graphQL getAutomatischInfo query', () => { + const query = ` + query { + getAutomatischInfo { + isCloud + license { + id + name + expireAt + verified + } + } + } + `; + + describe('and without valid license', () => { + beforeEach(async () => { + jest.spyOn(license, 'getLicense').mockResolvedValue(false); + }); + + it('should return empty license data', async () => { + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getAutomatischInfo: { + isCloud: false, + license: { + id: null as string, + name: null as string, + expireAt: null as string, + verified: false, + }, + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + + describe('and with valid license', () => { + beforeEach(async () => { + const mockedLicense = { + id: '123123', + name: 'Test License', + expireAt: '2025-08-09T10:56:54.144Z', + verified: true, + }; + + jest.spyOn(license, 'getLicense').mockResolvedValue(mockedLicense); + }); + + describe('and with cloud flag enabled', () => { + beforeEach(async () => { + setMockConfig({ isCloud: true }); + }); + + it('should return all license data', async () => { + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getAutomatischInfo: { + isCloud: true, + license: { + expireAt: '2025-08-09T10:56:54.144Z', + id: '123123', + name: 'Test License', + verified: true, + }, + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + + describe('and with cloud flag disabled', () => { + beforeEach(async () => { + setMockConfig({ isCloud: false }); + }); + + it('should return all license data', async () => { + const response = await request(app) + .post('/graphql') + .send({ query }) + .expect(200); + + const expectedResponsePayload = { + data: { + getAutomatischInfo: { + isCloud: false, + license: { + expireAt: '2025-08-09T10:56:54.144Z', + id: '123123', + name: 'Test License', + verified: true, + }, + }, + }, + }; + + expect(response.body).toEqual(expectedResponsePayload); + }); + }); + }); +});