Added unit tests for beforeEach router method.
This commit is contained in:
parent
085abd1f3a
commit
abdda18b4a
1 changed files with 72 additions and 0 deletions
72
src/router/router.beforeEach.spec.js
Normal file
72
src/router/router.beforeEach.spec.js
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
import issPageValues from '@/router/router-constants/issPage-values';
|
||||||
|
|
||||||
|
let router;
|
||||||
|
let saveSession;
|
||||||
|
let showIssLoadingModal;
|
||||||
|
let mockBeforeEachHandler;
|
||||||
|
let mockStore;
|
||||||
|
|
||||||
|
jest.mock('vue-router', () => ({
|
||||||
|
createWebHistory: jest.fn(() => ({ state: {} })),
|
||||||
|
createRouter: jest.fn((options) => ({
|
||||||
|
options,
|
||||||
|
beforeEach: jest.fn((handler) => {
|
||||||
|
mockBeforeEachHandler = handler;
|
||||||
|
}),
|
||||||
|
afterEach: jest.fn(),
|
||||||
|
addRoute: jest.fn(),
|
||||||
|
getRoutes: jest.fn(() => []),
|
||||||
|
hasRoute: jest.fn(() => true),
|
||||||
|
push: jest.fn(),
|
||||||
|
go: jest.fn(),
|
||||||
|
currentRoute: { value: { query: { issPage: 'welcome-page' } } }
|
||||||
|
}))
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/helpers/order-helper.js', () => ({
|
||||||
|
saveSession: jest.fn(() => Promise.resolve())
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/helpers/loading-modal-helper', () => jest.fn());
|
||||||
|
|
||||||
|
jest.mock('@/store', () => ({
|
||||||
|
useMainStore: jest.fn(() => mockStore)
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('Router beforeEach callSaveSession', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
router = require('@/router/').default;
|
||||||
|
({ saveSession } = require('@/helpers/order-helper.js'));
|
||||||
|
const loadingModalModule = require('@/helpers/loading-modal-helper');
|
||||||
|
showIssLoadingModal = loadingModalModule.default || loadingModalModule;
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockStore = {
|
||||||
|
order: {},
|
||||||
|
isBailout: false
|
||||||
|
};
|
||||||
|
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('calls saveSession synchronously when entering vehicle-selection without a referral number', async () => {
|
||||||
|
const result = await mockBeforeEachHandler(
|
||||||
|
{
|
||||||
|
name: issPageValues.VEHICLE_SELECTION,
|
||||||
|
query: { issPage: issPageValues.VEHICLE_SELECTION },
|
||||||
|
href: '/?issPage=vehicle-selection'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: issPageValues.WELCOME_PAGE,
|
||||||
|
query: { issPage: issPageValues.WELCOME_PAGE }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(saveSession).toHaveBeenCalledTimes(1);
|
||||||
|
expect(saveSession).toHaveBeenCalledWith({ shouldAwaitSaveSessionQueue: true, bailoutOnError: true });
|
||||||
|
expect(showIssLoadingModal).toHaveBeenNthCalledWith(1, true);
|
||||||
|
expect(showIssLoadingModal).toHaveBeenNthCalledWith(2, false);
|
||||||
|
expect(result).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue