I need to conduct a test on an array of objects. During the test coverage analysis of the displayed array, I found that the last object with the key link
has certain conditions that are not covered.
export const relatedServicesList: IRelatedServiceItem[] = [
{
label: 'inquiry.title',
link: '/adc/declaration/landing',
},
{
label: 'extendDeposit.title',
link: '/adc/extend-deposit/landing',
},
{
label: 'generalAdminCustomsServices.landing.title',
link:
window.location.host === 'stage'
? '/demo'
: '/test',
},
];
Approach Taken for Testing
import { relatedServicesList } from './routes';
describe('Routes', () => {
it('when host = stage', () => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
host: 'stage',
},
});
window.location.host = 'stage';
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
expect.objectContaining({
label: 'generalAdminCustomsServices.landing.title',
link:
'stage',
}),
);
});
it('when host != stage', () => {
global.window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
host: 'demo',
},
});
window.location.host = 'demo';
expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
expect.objectContaining({
label: 'generalAdminCustomsServices.landing.title',
link: '/test',
}),
);
});
});
The condition part remains uncovered. It should be noted that only the array is exported which is defined as type
IRelatedServiceItem
, there are no additional functions included.