From 6fcd126ff86ae24202449dd69b3d05953218f4d0 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Mon, 21 Oct 2024 14:32:03 +0000 Subject: [PATCH] test(app-auth-client): cover lifecycle hooks and triggerAppConfigUpdate method --- .../src/models/app-auth-client.test.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/packages/backend/src/models/app-auth-client.test.js b/packages/backend/src/models/app-auth-client.test.js index d95ade3d..d9ef9d99 100644 --- a/packages/backend/src/models/app-auth-client.test.js +++ b/packages/backend/src/models/app-auth-client.test.js @@ -2,9 +2,11 @@ import { describe, it, expect, vi } from 'vitest'; import AES from 'crypto-js/aes.js'; import enc from 'crypto-js/enc-utf8.js'; +import AppConfig from './app-config.js'; import AppAuthClient from './app-auth-client.js'; import appConfig from '../config/app.js'; import { createAppAuthClient } from '../../test/factories/app-auth-client.js'; +import { createAppConfig } from '../../test/factories/app-config.js'; describe('AppAuthClient model', () => { it('tableName should return correct name', () => { @@ -140,6 +142,23 @@ describe('AppAuthClient model', () => { }); }); + it('triggerAppConfigUpdate should trigger an update in related app config', async () => { + await createAppConfig({ key: 'gitlab' }); + + const appAuthClient = await createAppAuthClient({ + appKey: 'gitlab', + }); + + const appConfigBeforeUpdateSpy = vi.spyOn( + AppConfig.prototype, + '$beforeUpdate' + ); + + await appAuthClient.triggerAppConfigUpdate(); + + expect(appConfigBeforeUpdateSpy).toHaveBeenCalledOnce(); + }); + it('$beforeInsert should call AppAuthClient.encryptData', async () => { const appAuthClientBeforeInsertSpy = vi.spyOn( AppAuthClient.prototype, @@ -151,6 +170,17 @@ describe('AppAuthClient model', () => { expect(appAuthClientBeforeInsertSpy).toHaveBeenCalledOnce(); }); + it('$afterInsert should call AppAuthClient.triggerAppConfigUpdate', async () => { + const appAuthClientAfterInsertSpy = vi.spyOn( + AppAuthClient.prototype, + '$afterInsert' + ); + + await createAppAuthClient(); + + expect(appAuthClientAfterInsertSpy).toHaveBeenCalledOnce(); + }); + it('$beforeUpdate should call AppAuthClient.encryptData', async () => { const appAuthClient = await createAppAuthClient(); @@ -164,6 +194,19 @@ describe('AppAuthClient model', () => { expect(appAuthClientBeforeUpdateSpy).toHaveBeenCalledOnce(); }); + it('$afterUpdate should call AppAuthClient.triggerAppConfigUpdate', async () => { + const appAuthClient = await createAppAuthClient(); + + const appAuthClientAfterUpdateSpy = vi.spyOn( + AppAuthClient.prototype, + 'triggerAppConfigUpdate' + ); + + await appAuthClient.$query().patchAndFetch({ name: 'sample' }); + + expect(appAuthClientAfterUpdateSpy).toHaveBeenCalledOnce(); + }); + it('$afterFind should call AppAuthClient.decryptData', async () => { const appAuthClient = await createAppAuthClient();