Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
115 lines
No EOL
3.5 KiB
JavaScript
115 lines
No EOL
3.5 KiB
JavaScript
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 = {
|
|
applicationUser: { triggeredSiteEntry: true },
|
|
runExperimentsForTrigger: jest.fn(() => Promise.resolve()),
|
|
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 });
|
|
});
|
|
}); |