From c8680703375b2d1a86d5fdae4aa454b8b0b621a5 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Thu, 10 Oct 2024 17:06:49 +0000 Subject: [PATCH] test(config): write model tests --- .../models/__snapshots__/config.test.js.snap | 52 +++++++ packages/backend/src/models/config.test.js | 137 ++++++++++++++++++ packages/backend/test/factories/config.js | 4 + 3 files changed, 193 insertions(+) create mode 100644 packages/backend/src/models/__snapshots__/config.test.js.snap create mode 100644 packages/backend/src/models/config.test.js diff --git a/packages/backend/src/models/__snapshots__/config.test.js.snap b/packages/backend/src/models/__snapshots__/config.test.js.snap new file mode 100644 index 00000000..cf1c5274 --- /dev/null +++ b/packages/backend/src/models/__snapshots__/config.test.js.snap @@ -0,0 +1,52 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`Config model > jsonSchema should have correct validations 1`] = ` +{ + "properties": { + "createdAt": { + "type": "string", + }, + "id": { + "format": "uuid", + "type": "string", + }, + "installationCompleted": { + "type": "boolean", + }, + "logoSvgData": { + "type": [ + "string", + "null", + ], + }, + "palettePrimaryDark": { + "type": [ + "string", + "null", + ], + }, + "palettePrimaryLight": { + "type": [ + "string", + "null", + ], + }, + "palettePrimaryMain": { + "type": [ + "string", + "null", + ], + }, + "title": { + "type": [ + "string", + "null", + ], + }, + "updatedAt": { + "type": "string", + }, + }, + "type": "object", +} +`; diff --git a/packages/backend/src/models/config.test.js b/packages/backend/src/models/config.test.js new file mode 100644 index 00000000..596bf16f --- /dev/null +++ b/packages/backend/src/models/config.test.js @@ -0,0 +1,137 @@ +import { describe, it, expect, vi } from 'vitest'; +import appConfig from '../config/app.js'; +import Config from './config'; +import { createConfig } from '../../test/factories/config.js'; + +describe('Config model', () => { + it('tableName should return correct name', () => { + expect(Config.tableName).toBe('config'); + }); + + it('jsonSchema should have correct validations', () => { + expect(Config.jsonSchema).toMatchSnapshot(); + }); + + it('virtualAttributes should return correct attributes', () => { + const virtualAttributes = Config.virtualAttributes; + + const expectedAttributes = [ + 'disableNotificationsPage', + 'disableFavicon', + 'additionalDrawerLink', + 'additionalDrawerLinkIcon', + 'additionalDrawerLinkText', + ]; + + expect(virtualAttributes).toStrictEqual(expectedAttributes); + }); + + it('disableNotificationsPage should return its value in appConfig', async () => { + const disableNotificationsPageSpy = vi.spyOn( + appConfig, + 'disableNotificationsPage', + 'get' + ); + + new Config().disableNotificationsPage; + + expect(disableNotificationsPageSpy).toHaveBeenCalledOnce(); + }); + + it('disableFavicon should return its value in appConfig', async () => { + const disableFaviconSpy = vi + .spyOn(appConfig, 'disableFavicon', 'get') + .mockReturnValue(true); + + new Config().disableFavicon; + + expect(disableFaviconSpy).toHaveBeenCalledOnce(); + }); + + it('additionalDrawerLink should return its value in appConfig', async () => { + const additionalDrawerLinkSpy = vi + .spyOn(appConfig, 'additionalDrawerLink', 'get') + .mockReturnValue('https://automatisch.io'); + + new Config().additionalDrawerLink; + + expect(additionalDrawerLinkSpy).toHaveBeenCalledOnce(); + }); + + it('additionalDrawerLinkIcon should return its value in appConfig', async () => { + const additionalDrawerLinkIconSpy = vi + .spyOn(appConfig, 'additionalDrawerLinkIcon', 'get') + .mockReturnValue('SampleIcon'); + + new Config().additionalDrawerLinkIcon; + + expect(additionalDrawerLinkIconSpy).toHaveBeenCalledOnce(); + }); + + it('additionalDrawerLinkText should return its value in appConfig', async () => { + const additionalDrawerLinkTextSpy = vi + .spyOn(appConfig, 'additionalDrawerLinkText', 'get') + .mockReturnValue('Go back to Automatisch'); + + new Config().additionalDrawerLinkText; + + expect(additionalDrawerLinkTextSpy).toHaveBeenCalledOnce(); + }); + + describe('get', () => { + it('should return single config record when it exists', async () => { + const createdConfig = await createConfig({ + title: 'Automatisch', + }); + + const config = await Config.get(); + + expect(config).toStrictEqual(createdConfig); + }); + + it('should create config record and return when it does not exist', async () => { + const configBefore = await Config.query().first(); + + expect(configBefore).toBeUndefined(); + + const config = await Config.get(); + + expect(config).toBeTruthy(); + }); + }); + + it('update should update existing single record', async () => { + const patchAndFetchSpy = vi + .fn() + .mockImplementation((newConfig) => newConfig); + + vi.spyOn(Config, 'get').mockImplementation(() => ({ + $query: () => ({ + patchAndFetch: patchAndFetchSpy, + }), + })); + + const config = await Config.update({ title: 'Automatisch' }); + + expect(patchAndFetchSpy).toHaveBeenCalledWith({ title: 'Automatisch' }); + expect(config).toStrictEqual({ title: 'Automatisch' }); + }); + + it('isInstallationCompleted should return installationCompleted value', async () => { + const configGetSpy = vi.spyOn(Config, 'get').mockImplementation(() => ({ + installationCompleted: true, + })); + + await Config.isInstallationCompleted(); + + expect(configGetSpy).toHaveBeenCalledOnce(); + }); + + it('markInstallationCompleted should update installationCompleted as true', async () => { + await Config.update({ installationCompleted: false }); + + const config = await Config.markInstallationCompleted(); + + expect(config.installationCompleted).toBe(true); + }); +}); diff --git a/packages/backend/test/factories/config.js b/packages/backend/test/factories/config.js index 96ccc523..e786a80a 100644 --- a/packages/backend/test/factories/config.js +++ b/packages/backend/test/factories/config.js @@ -4,6 +4,10 @@ export const getConfig = async () => { return await Config.get(); }; +export const createConfig = async (params = {}) => { + return await Config.query().insertAndFetch(params); +}; + export const updateConfig = async (params = {}) => { return await Config.update(params); };