DigitalConsumer.ISS/src/helpers/unit-test-helper.js
2023-07-19 15:25:46 -04:00

183 lines
No EOL
5.4 KiB
JavaScript

import { navigationScenarios } from '@/router/router-constants/navigation-scenarios.js';
import { RouterLinkStub } from '@vue/test-utils';
import { vehicleCategories } from '@/constants/vehicle-categories.js';
import { issPageValues } from '@/router/router-constants/issPage-values';
import { cookieNames } from '@/constants/cookie-names';
import { Form } from 'vee-validate';
import baseMixin from '@/mixins/base-mixin';
import {
getCookieDomainValue,
setCookieProperties
} from '@/helpers/cookie-helper';
import {
analyticsPageEvents,
GaCategories,
GaActions,
GaLabels,
GaEvents,
ValueToLogTypes
} from '@/constants/analytics';
import { routerParams } from '@/router/router-constants/router-params';
import { queryStrings } from '@/constants/query-strings';
import { useMainStore } from '@/store';
import { mapStores } from 'pinia';
import { createTestingPinia } from '@pinia/testing';
const pinia = createTestingPinia();
useMainStore(pinia);
const mockMixin = {
methods: {
getCmsContent: jest.fn()
},
computed: {
...mapStores(useMainStore)
}
};
// Common methods
export function getMountOptions(mockData) {
// Define our mocks to attached to the 'global' object for Vue/Jest.
const mocks = {};
// Mock const files
mocks.navigationScenarios = navigationScenarios;
mocks.vehicleCategories = vehicleCategories;
mocks.issPageValues = issPageValues;
mocks.queryStrings = queryStrings;
mocks.$router = mockData?.router;
mocks.$route = mockData?.route;
mocks.GaActions = GaActions;
mocks.pushEventToGA = jest.fn();
mocks.$loadScript = mockData?.loadScript;
mocks.prependActionToMethod = jest.fn();
const global = {
mocks: mocks,
mixins: [mockMixin],
plugins: [pinia],
stubs: {
Form,
RouterLink: RouterLinkStub
}
};
return { global };
}
// Heritage integration common methods
export const cookies = {
[cookieNames.ISS_SESSION_INFO]: '{"ReferralNumber":"1566818","ReferralDate":"2022-03-15T10:56:24.597","ReferralCorrelationId":"404d2b04-f86e-45c3-b373-127b6217b060","ShouldResetState":false}',
UNIQUE_SESSION_ID: '33756020-b58e-4ec7-b8b8-3f1576719c40',
anotherCookie: '{}',
someOtherCookie: '{}',
dxdev: 'did=21b9b94a-ec23-42c1-aaac-e2ae4e4dbffe',
sid: 'cba0c3d1-3c1b-4305-bb56-31aa50f58e27',
skey: '12345'
};
export function setupCookies({ ISSCookieValue = '', includeHeritageCookie = true }) {
Object.keys(cookies).forEach((key) => {
const cookieValue =
key == cookieNames.ISS_SESSION_INFO ? ISSCookieValue : cookies[key];
if (includeHeritageCookie || key != cookieNames.ISS_SESSION_INFO)
setCookieProperties({ [key]: cookieValue }, { isSecure: false });
});
}
// Removes test cookies for testing cookie-helper and order-helper
export function removeAllTestCookies() {
Object.keys(cookies).forEach((key) => {
document.cookie = `${key}=;Max-Age=0;`;
document.cookie = `${key}=;Max-Age=0;path=/;${getCookieDomainValue()}`;
});
}
export function setupMocksForJsFiles() {
return { baseMixin };
}
// Private methods
/*
// Common methods
export function getMountOptions(mockData) {
// Define our mocks to attached to the 'global' object for Vue/Jest.
const mocks = {};
//this is mocking if you use the mixin directly(baseMixin.methods.dispatchStoreAction) vs this.dispatchStoreAction
setupBaseMixinDispatchStoreAction(mockData);
mocks.pushEventToGA = jest.fn();
mocks.pushPageViewToGA = jest.fn();
mocks.logEvent = jest.fn();
mocks.pushExperimentsToDataLayer = jest.fn();
mocks.prependActionToMethod = jest.fn();
mocks.dispatchStoreAction = jest.fn();
mocks.dispatchStoreAction.mockImplementation((actionName) => {
let actionFilterResult = mockData.actionList?.filter((x) => x.actionName == actionName);
if (actionFilterResult?.length === 1) {
return Promise.resolve({
data: actionFilterResult[0].data,
});
}
});
// Mock const files
mocks.storeActions = storeActions;
mocks.storeMutations = storeMutations;
mocks.navigationScenarios = navigationScenarios;
mocks.vehicleCategories = vehicleCategories;
mocks.issPageValues = issPageValues;
mocks.analyticsPageEvents = analyticsPageEvents;
mocks.GaCategories = GaCategories;
mocks.GaActions = GaActions;
mocks.GaLabels = GaLabels;
mocks.GaEvents = GaEvents;
mocks.ValueToLogTypes = ValueToLogTypes;
mocks.queryStrings = queryStrings;
mocks.routerParams = routerParams;
// Mock $store and $router when accessing this.$store/$router
mocks.$store = mockData.store;
mocks.$router = mockData.router;
mocks.$route = mockData.route;
mocks.$loadScript = mockData.loadScript;
const global = {
mocks: mocks,
mixins: mockData.mixins,
plugins: [pinia],
stubs: { Form }
};
return { global };
}
export function getMockOrderInfo(
mockReferralNumber,
mockCorrelationId,
mockReferralDate,
accountNumber = "0",
savedSessionId,
crmCustomerId
) {
return {
referralNumber: mockReferralNumber,
referralCorrelationId: mockCorrelationId,
referralDate: mockReferralDate,
accountNumber: accountNumber,
savedSessionId: savedSessionId,
crmCustomerId: crmCustomerId,
};
}
*/