test: utilize test.each for better readability

pull/5126/head
bnymncoskuner 5 years ago
parent f8bbc46e36
commit 7225cad2c0

@ -61,18 +61,20 @@ describe('EnvironmentUtils', () => {
},
};
it('should call the remoteEnv URL and dispatch the SetEnvironment action for overwrite', () => {
setupTestAndRun({ mergeStrategy: 'deepmerge' }, deepMerge(environment, customEnv));
});
const someEnv = { apiUrl: 'https://some-api-url' } as any;
const customFn = (_, __) => someEnv;
it('should call the remoteEnv URL and dispatch the SetEnvironment action for deepmerge', () => {
setupTestAndRun({ mergeStrategy: 'overwrite' }, customEnv);
});
it('should call the remoteEnv URL and dispatch the SetEnvironment action for customFn', () => {
const someEnv = { apiUrl: 'https://some-api-url' } as any;
setupTestAndRun({ mergeStrategy: (_, __) => someEnv }, someEnv);
});
test.each`
case | strategy | expected
${'null'} | ${null} | ${customEnv}
${'undefined'} | ${undefined} | ${customEnv}
${'overwrite'} | ${'overwrite'} | ${customEnv}
${'deepmerge'} | ${'deepmerge'} | ${deepMerge(environment, customEnv)}
${'customFn'} | ${customFn} | ${someEnv}
`(
'should call the remoteEnv URL and dispatch the SetEnvironment action for case $case ',
({ strategy, expected }) => setupTestAndRun({ mergeStrategy: strategy }, expected),
);
function setupTestAndRun(strategy: Pick<Config.RemoteEnv, 'mergeStrategy'>, expectedValue) {
const injector = spectator.inject(Injector);

@ -8,48 +8,36 @@ describe('DeepMerge', () => {
expect(deepMerge(null, null)).toEqual({});
});
it('should correctly return when any of the inputs is null or undefined', () => {
const differentTestValues = [10, false, '', 'test-string', { a: 1 }, [1, 2, 3], {}];
differentTestValues.forEach(val => {
expect(deepMerge(undefined, val)).toEqual(val);
expect(deepMerge(null, val)).toEqual(val);
expect(deepMerge(val, undefined)).toEqual(val);
expect(deepMerge(val, null)).toEqual(val);
});
test.each`
value
${10}
${false}
${''}
${'test-string'}
${{ a: 1 }}
${[1, 2, 3]}
${{}}
`('should correctly return when any of the inputs is null or undefined', val => {
expect(deepMerge(undefined, val)).toEqual(val);
expect(deepMerge(null, val)).toEqual(val);
expect(deepMerge(val, undefined)).toEqual(val);
expect(deepMerge(val, null)).toEqual(val);
});
it('should correctly return source if one of them is primitive or an array', () => {
const differentTestValues = [
{
target: 10,
source: false,
},
{
target: false,
source: 20,
},
{
target: 'string',
source: { a: 5 },
},
{
target: { b: 10 },
source: 50,
},
{
target: [1, 2, 3],
source: 40,
},
{
target: { k: 60 },
source: [4, 5, 6],
},
];
differentTestValues.forEach(val =>
expect(deepMerge(val.target, val.source)).toEqual(val.source),
);
});
test.each`
target | source
${10} | ${false}
${false} | ${20}
${'some-string'} | ${{ a: 5 }}
${{ b: 10 }} | ${50}
${[1, 2, 3]} | ${40}
${{ k: 60 }} | ${[4, 5, 6]}
`(
'should correctly return source if one of them is primitive or an array',
({ target, source }) => {
expect(deepMerge(target, source)).toEqual(source);
},
);
it('should correctly return when both inputs are objects with different fields', () => {
const target = { a: 1 };

Loading…
Cancel
Save