I want to test an array of object. Basically, when I run the test coverage for the shown array of objects, the last object is link have conditions and that's the part which is uncovered.
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',
},
];
What I tried
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 is uncovered. Please note that only the array is exported which is of type
IRelatedServiceItemthere's no function etc.