Вот решение для модульного тестирования:
sop3login.ts
:
import ADCServices from './adc.service';
export interface IVariables {
user: any;
history: IHistory;
i18n(name: string): any;
SOP3toggleModal(props: IVariables, flag: boolean): void;
}
interface IHistory {
push(router: string): any;
}
export const SOP3loginConfig = (props: IVariables) => {
const { i18n } = props;
return {
buttonLabel: props.user != null ? i18n('StartJourney') : i18n('logIn'),
loginLink: '/login?redirectUrl=' + window.location.href,
isSOP3: async () => {
const userData = await ADCServices.getUserInfo();
if (!userData.session.tammUserInfo || userData.session.tammUserInfo.Type !== 'SOP3') {
props.SOP3toggleModal(props, true);
setTimeout(() => props.SOP3toggleModal(props, false), 5000);
return false;
} else {
return true;
}
},
};
};
adc.service.ts
:
export default class ADCServices {
public static async getUserInfo() {
return {
session: {
tammUserInfo: {
Type: 'real type',
},
},
};
}
}
work.ts
:
import { SOP3loginConfig, IVariables } from './sop3login';
const start = async (props: IVariables) => {
if (props.user) {
if (await SOP3loginConfig(props).isSOP3()) {
props.history.push('/adc/card-renewal/customs');
}
} else {
props.history.push(SOP3loginConfig(props).loginLink);
}
};
export { start };
work.test.ts
:
import { start } from './work';
import { SOP3loginConfig, IVariables } from './sop3login';
jest.mock('./sop3login.ts', () => {
const mObj = {
isSOP3: jest.fn(),
};
return { SOP3loginConfig: jest.fn(() => mObj) };
});
describe('Start SOP3loginConfig should be called', () => {
it('Should call the SOP3', async () => {
const mProps: IVariables = {
user: true,
history: { push: jest.fn() },
i18n: jest.fn(),
SOP3toggleModal: jest.fn(),
};
await start(mProps);
expect(SOP3loginConfig).toBeCalledWith(mProps);
expect(SOP3loginConfig(mProps).isSOP3).toBeCalledTimes(1);
});
});
Результаты модульных испытаний с отчетом о покрытии:
PASS src/stackoverflow/59627009/work.test.ts
Start SOP3loginConfig should be called
✓ Should call the SOP3 (7ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files | 77.78 | 50 | 100 | 71.43 | |
work.ts | 77.78 | 50 | 100 | 71.43 | 6,9 |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.078s, estimated 10s
Исходный код: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59627009