Added unit test specifically for afterEach method due to its large size.
This commit is contained in:
parent
73a6ac1dcc
commit
b9649d9153
1 changed files with 113 additions and 0 deletions
113
src/router/router.afterEach.spec.js
Normal file
113
src/router/router.afterEach.spec.js
Normal file
|
|
@ -0,0 +1,113 @@
|
||||||
|
import issPageValues from '@/router/router-constants/issPage-values';
|
||||||
|
import routerParams from '@/router/router-constants/router-params';
|
||||||
|
|
||||||
|
let router;
|
||||||
|
let saveSession;
|
||||||
|
let mockAfterEachHandler;
|
||||||
|
let mockStore;
|
||||||
|
|
||||||
|
jest.mock('vue-router', () => ({
|
||||||
|
createWebHistory: jest.fn(() => ({ state: {} })),
|
||||||
|
createRouter: jest.fn((options) => ({
|
||||||
|
options,
|
||||||
|
beforeEach: jest.fn(),
|
||||||
|
afterEach: jest.fn((handler) => {
|
||||||
|
mockAfterEachHandler = handler;
|
||||||
|
}),
|
||||||
|
addRoute: jest.fn(),
|
||||||
|
getRoutes: jest.fn(() => []),
|
||||||
|
hasRoute: jest.fn(() => true),
|
||||||
|
push: jest.fn(),
|
||||||
|
go: jest.fn(),
|
||||||
|
currentRoute: { value: { query: { issPage: 'welcomePage' } } }
|
||||||
|
}))
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/helpers/order-helper.js', () => ({
|
||||||
|
saveSession: jest.fn(() => Promise.resolve())
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/mixins/analytics-mixin', () => ({
|
||||||
|
__esModule: true,
|
||||||
|
default: {
|
||||||
|
methods: {
|
||||||
|
validateSession: jest.fn(() => Promise.resolve()),
|
||||||
|
logDigitalConsumer: jest.fn(),
|
||||||
|
pushIssSessionData: jest.fn(),
|
||||||
|
pushPageViewToGA: jest.fn(),
|
||||||
|
pushExperimentsToDataLayer: jest.fn(),
|
||||||
|
pushOrderToDataLayer: jest.fn()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
jest.mock('@/store', () => ({
|
||||||
|
useMainStore: jest.fn(() => mockStore)
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('Router afterEach skipSaveSession', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
router = require('@/router/').default;
|
||||||
|
({ saveSession } = require('@/helpers/order-helper.js'));
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockStore = {
|
||||||
|
updateLastPageVisited: jest.fn(),
|
||||||
|
clearSaveSessionPromise: jest.fn(),
|
||||||
|
hasSubmittedOrder: jest.fn(() => false)
|
||||||
|
};
|
||||||
|
|
||||||
|
router.options.history.state = {};
|
||||||
|
jest.clearAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should skip saveSession when skipSaveSession is true on to.state', async () => {
|
||||||
|
await mockAfterEachHandler(
|
||||||
|
{
|
||||||
|
name: issPageValues.WELCOME_PAGE,
|
||||||
|
query: { issPage: issPageValues.WELCOME_PAGE },
|
||||||
|
state: { [routerParams.SKIP_SAVE_SESSION]: true }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: issPageValues.ENTRY_PAGE,
|
||||||
|
redirectedFrom: undefined
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(saveSession).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should skip saveSession when skipSaveSession is true on history state', async () => {
|
||||||
|
router.options.history.state = { [routerParams.SKIP_SAVE_SESSION]: true };
|
||||||
|
|
||||||
|
await mockAfterEachHandler(
|
||||||
|
{
|
||||||
|
name: issPageValues.WELCOME_PAGE,
|
||||||
|
query: { issPage: issPageValues.WELCOME_PAGE }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: issPageValues.ENTRY_PAGE,
|
||||||
|
redirectedFrom: undefined
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(saveSession).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call saveSession when skipSaveSession is not present', async () => {
|
||||||
|
await mockAfterEachHandler(
|
||||||
|
{
|
||||||
|
name: issPageValues.WELCOME_PAGE,
|
||||||
|
query: { issPage: issPageValues.WELCOME_PAGE }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: issPageValues.ENTRY_PAGE,
|
||||||
|
redirectedFrom: undefined
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(saveSession).toHaveBeenCalledTimes(1);
|
||||||
|
expect(saveSession).toHaveBeenCalledWith({ bailoutOnError: true });
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue