test(app-config): write model tests

This commit is contained in:
Ali BARIN
2024-10-01 15:52:28 +00:00
committed by Faruk AYDIN
parent dabb01e237
commit ecb04b4ba9
2 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,115 @@
import { describe, it, expect } from 'vitest';
import Base from './base.js';
import AppConfig from './app-config.js';
import AppAuthClient from './app-auth-client.js';
import { createAppConfig } from '../../test/factories/app-config.js';
import { createAppAuthClient } from '../../test/factories/app-auth-client.js';
describe('AppConfig model', () => {
it('tableName should return correct name', () => {
expect(AppConfig.tableName).toBe('app_configs');
});
it('jsonSchema should have correct validations', () => {
expect(AppConfig.jsonSchema).toMatchSnapshot();
});
it('relationMappings should return correct associations', () => {
const relationMappings = AppConfig.relationMappings();
const expectedRelations = {
appAuthClients: {
relation: Base.HasManyRelation,
modelClass: AppAuthClient,
join: {
from: 'app_configs.key',
to: 'app_auth_clients.app_key',
},
},
};
expect(relationMappings).toStrictEqual(expectedRelations);
});
it('virtualAttributes should return correct properties', () => {
expect(AppConfig.virtualAttributes).toMatchSnapshot();
});
it('getApp should return associated application', async () => {
const appConfig = await createAppConfig({ key: 'deepl' });
const app = await appConfig.getApp();
expect(app.key).toBe('deepl');
});
describe('canCustomConnect', () => {
it('should return true when app is enabled and allows custom connection', async () => {
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: true,
});
expect(appConfig.canCustomConnect).toBe(true);
});
it('should return false when app is disabled', async () => {
const appConfig = await createAppConfig({
disabled: true,
allowCustomConnection: true,
});
expect(appConfig.canCustomConnect).toBe(false);
});
it(`should return false when app doesn't allow custom connection`, async () => {
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: false,
});
expect(appConfig.canCustomConnect).toBe(false);
});
});
describe('canConnect', () => {
it('should return true when app is enabled, shared and allows custom connection', async () => {
await createAppAuthClient({
appKey: 'deepl',
active: true,
});
let appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: true,
shared: true,
key: 'deepl',
});
appConfig = await appConfig.$query().withGraphFetched({
appAuthClients: true,
});
expect(appConfig.canConnect).toBe(true);
});
it('should return false when app is disabled', async () => {
const appConfig = await createAppConfig({
disabled: true,
allowCustomConnection: true,
});
expect(appConfig.canConnect).toBe(false);
});
it(`should return false when app doesn't allow custom connection`, async () => {
const appConfig = await createAppConfig({
disabled: false,
allowCustomConnection: false,
});
expect(appConfig.canConnect).toBe(false);
});
});
});