test(config): add unit testing

test: add unhappy path
This commit is contained in:
Nicolas Meienberger 2022-09-22 22:08:43 +02:00
parent ddfc6eff34
commit dd62fcdde0
2 changed files with 84 additions and 1 deletions

View file

@ -7,7 +7,7 @@ module.exports = {
setupFiles: ['<rootDir>/src/test/dotenv-config.ts'],
setupFilesAfterEnv: ['<rootDir>/src/test/jest-setup.ts'],
collectCoverage: true,
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/migrations/**/*.{ts,tsx}', '!**/config/**/*.{ts,tsx}', '!**/__tests__/**'],
collectCoverageFrom: ['src/**/*.{ts,tsx}', '!src/**/migrations/**/*.{ts,tsx}', '!**/src/config/**/*.{ts,tsx}', '!**/__tests__/**'],
passWithNoTests: true,
transform: {
'^.+\\.graphql$': 'graphql-import-node/jest',

View file

@ -0,0 +1,83 @@
import { faker } from '@faker-js/faker';
import fs from 'fs-extra';
import { applyJsonConfig, getConfig, setConfig } from '../TipiConfig';
jest.mock('fs-extra');
beforeEach(async () => {
jest.resetModules();
jest.resetAllMocks();
});
describe('Test: getConfig', () => {
it('It should return config from .env', () => {
const config = getConfig();
expect(config).toBeDefined();
expect(config.NODE_ENV).toBe('test');
expect(config.logs.LOGS_FOLDER).toBe('logs');
expect(config.logs.LOGS_APP).toBe('app.log');
expect(config.logs.LOGS_ERROR).toBe('error.log');
expect(config.dnsIp).toBe('9.9.9.9');
expect(config.rootFolder).toBe('/runtipi');
expect(config.internalIp).toBe('192.168.1.10');
});
});
describe('Test: setConfig', () => {
it('It should be able set config', () => {
const randomWord = faker.random.word();
setConfig('appsRepoUrl', randomWord);
const config = getConfig();
expect(config).toBeDefined();
expect(config.appsRepoUrl).toBe(randomWord);
});
it('Should not be able to set invalid NODE_ENV', () => {
expect(() => setConfig('NODE_ENV', 'invalid')).toThrow();
});
});
describe('Test: applyJsonConfig', () => {
it('It should be able to apply json config', () => {
const settingsJson = {
appsRepoUrl: faker.random.word(),
appsRepoId: faker.random.word(),
domain: faker.random.word(),
};
const MockFiles = {
'/runtipi/state/settings.json': JSON.stringify(settingsJson),
};
// @ts-ignore
fs.__createMockFiles(MockFiles);
applyJsonConfig();
const config = getConfig();
expect(config).toBeDefined();
expect(config.appsRepoUrl).toBe(settingsJson.appsRepoUrl);
expect(config.appsRepoId).toBe(settingsJson.appsRepoId);
expect(config.domain).toBe(settingsJson.domain);
});
it('Should not be able to apply an invalid value from json config', () => {
const settingsJson = {
appsRepoUrl: faker.random.word(),
appsRepoId: faker.random.word(),
domain: 10,
};
const MockFiles = {
'/runtipi/state/settings.json': JSON.stringify(settingsJson),
};
// @ts-ignore
fs.__createMockFiles(MockFiles);
expect(() => applyJsonConfig()).toThrow();
});
});