Skip to content

Commit

Permalink
added tests for undefined config props
Browse files Browse the repository at this point in the history
  • Loading branch information
Olian04 committed Mar 23, 2021
1 parent 25420be commit f3f00dc
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 26 deletions.
136 changes: 110 additions & 26 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { DeepPartial, Record, useValueOrFallback } from '@olian/typescript-helpers';
import {
DeepPartial,
Record,
useValueOrFallback,
} from '@olian/typescript-helpers';
import { FormattingContext } from './interfaces/formatting.context';
import { MessageConstructionStrategy } from './enums/messageConstructionStrategy';
import { Theme } from './interfaces/theme';
Expand All @@ -23,7 +27,7 @@ export type PartialConfig = DeepPartial<Config>;

export const DefaultConfig = new Config({
messageConstructionStrategy: MessageConstructionStrategy.ALL,
format: ctx => `${ctx.time24} ${ctx.type} ${ctx.msg}`,
format: (ctx) => `${ctx.time24} ${ctx.type} ${ctx.msg}`,
saveToFile: null,
color: defaultTheme,
logLevels: {
Expand All @@ -32,30 +36,110 @@ export const DefaultConfig = new Config({
info: 2,
line: 1,
warn: 1,
error: 0
error: 0,
},
});

export const resolveConfig = (config: PartialConfig) => new Config({
messageConstructionStrategy: useValueOrFallback(config, 'messageConstructionStrategy', DefaultConfig.messageConstructionStrategy),
format: useValueOrFallback(config, 'format', DefaultConfig.format),
saveToFile: useValueOrFallback(config, 'saveToFile', null),
color: !config.color ? DefaultConfig.color : {
base: useValueOrFallback(config.color, 'base', DefaultConfig.color.base),
type: !config.color.type ? DefaultConfig.color.type : {
debug: useValueOrFallback(config.color.type, 'debug', DefaultConfig.color.type.debug),
error: useValueOrFallback(config.color.type, 'error', DefaultConfig.color.type.error),
info: useValueOrFallback(config.color.type, 'info', DefaultConfig.color.type.info),
log: useValueOrFallback(config.color.type, 'log', DefaultConfig.color.type.log),
warn: useValueOrFallback(config.color.type, 'warn', DefaultConfig.color.type.warn),
}
},
logLevels: !config.logLevels ? DefaultConfig.logLevels : {
debug: useValueOrFallback(config.logLevels, 'debug', DefaultConfig.logLevels.debug),
error: useValueOrFallback(config.logLevels, 'error', DefaultConfig.logLevels.error),
info: useValueOrFallback(config.logLevels, 'info', DefaultConfig.logLevels.info),
log: useValueOrFallback(config.logLevels, 'log', DefaultConfig.logLevels.log),
warn: useValueOrFallback(config.logLevels, 'warn', DefaultConfig.logLevels.warn),
line: useValueOrFallback(config.logLevels, 'line', DefaultConfig.logLevels.line),
}
});
// Treats undefined & null in the same way a missing key
const treatAsFalsy = [null, undefined];

export const resolveConfig = (config: PartialConfig) =>
new Config({
messageConstructionStrategy: useValueOrFallback(
config,
'messageConstructionStrategy',
DefaultConfig.messageConstructionStrategy,
treatAsFalsy
),
format: useValueOrFallback(
config,
'format',
DefaultConfig.format,
treatAsFalsy
),
saveToFile: useValueOrFallback(config, 'saveToFile', null, treatAsFalsy),
color: !config.color
? DefaultConfig.color
: {
base: useValueOrFallback(
config.color,
'base',
DefaultConfig.color.base,
treatAsFalsy
),
type: !config.color.type
? DefaultConfig.color.type
: {
debug: useValueOrFallback(
config.color.type,
'debug',
DefaultConfig.color.type.debug,
treatAsFalsy
),
error: useValueOrFallback(
config.color.type,
'error',
DefaultConfig.color.type.error,
treatAsFalsy
),
info: useValueOrFallback(
config.color.type,
'info',
DefaultConfig.color.type.info,
treatAsFalsy
),
log: useValueOrFallback(
config.color.type,
'log',
DefaultConfig.color.type.log,
treatAsFalsy
),
warn: useValueOrFallback(
config.color.type,
'warn',
DefaultConfig.color.type.warn,
treatAsFalsy
),
},
},
logLevels: !config.logLevels
? DefaultConfig.logLevels
: {
debug: useValueOrFallback(
config.logLevels,
'debug',
DefaultConfig.logLevels.debug,
treatAsFalsy
),
error: useValueOrFallback(
config.logLevels,
'error',
DefaultConfig.logLevels.error,
treatAsFalsy
),
info: useValueOrFallback(
config.logLevels,
'info',
DefaultConfig.logLevels.info,
treatAsFalsy
),
log: useValueOrFallback(
config.logLevels,
'log',
DefaultConfig.logLevels.log,
treatAsFalsy
),
warn: useValueOrFallback(
config.logLevels,
'warn',
DefaultConfig.logLevels.warn,
treatAsFalsy
),
line: useValueOrFallback(
config.logLevels,
'line',
DefaultConfig.logLevels.line,
treatAsFalsy
),
},
});
13 changes: 13 additions & 0 deletions test/unit-tests/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ts-check
const { expect } = require('chai');
const { DefaultConfig, resolveConfig } = require('../../dist/lib/config');

describe('Config', () => {
it('Undefined Format Function', () => {
const config = resolveConfig({
format: undefined,
});

expect(config.format).to.equal(DefaultConfig.format);
});
});

0 comments on commit f3f00dc

Please sign in to comment.