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

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

Loading…
Cancel
Save