351 lines
10 KiB
JavaScript
351 lines
10 KiB
JavaScript
import analyticsMixin from "@/mixins/analytics-mixin";
|
|
import { setupMocksForJsFiles, setupCookies } from "@/helpers/unit-test-helper.js";
|
|
import { storeActions } from "@/constants/store-actions";
|
|
import {
|
|
analyticsPageEvents,
|
|
GaCategories,
|
|
GaActions,
|
|
GaLabels,
|
|
GaEvents,
|
|
ValueToLogTypes,
|
|
} from "@/constants/analytics";
|
|
import store from "@/store";
|
|
|
|
// Mock only the regenerate functions; window.crypto is not available in testing.
|
|
jest.mock("@/helpers/heritage-integration/cookie-helper", () => {
|
|
const originalModule = jest.requireActual("@/helpers/heritage-integration/cookie-helper");
|
|
|
|
return {
|
|
__esModule: true,
|
|
...originalModule,
|
|
regenerateDeviceId: jest.fn(),
|
|
regenerateUserId: jest.fn(),
|
|
};
|
|
});
|
|
|
|
describe("analyticsMixin.js", () => {
|
|
test("logPageView: calls dispatch with type and payload", async () => {
|
|
const type = "";
|
|
const payload = {};
|
|
|
|
const mockData = {
|
|
actionList: [
|
|
{
|
|
actionName: storeActions.LOG_PAGE_VIEW,
|
|
},
|
|
{
|
|
actionName: storeActions.INITIALIZE_SESSION,
|
|
},
|
|
],
|
|
};
|
|
const mocks = setupMocksForJsFiles(mockData);
|
|
|
|
const testCookieValue = {
|
|
sid: "10000000-0000-0000-0000-000000000001",
|
|
};
|
|
|
|
setupCookies({ funnelCookieValue: JSON.stringify(testCookieValue) });
|
|
|
|
await analyticsMixin.methods.logPageView(type, payload);
|
|
|
|
expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled();
|
|
});
|
|
|
|
test("logCustomEvent: calls dispatch with type and payload", async () => {
|
|
const mockData = {
|
|
actionList: [
|
|
{
|
|
actionName: storeActions.LOG_CUSTOM_EVENT,
|
|
},
|
|
{
|
|
actionName: storeActions.INITIALIZE_SESSION,
|
|
},
|
|
],
|
|
};
|
|
const mocks = setupMocksForJsFiles(mockData);
|
|
|
|
await analyticsMixin.methods.logCustomEvent(
|
|
"someCat",
|
|
"someAction",
|
|
"someLabel",
|
|
"someVal"
|
|
);
|
|
|
|
expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled();
|
|
});
|
|
|
|
test("pushEventToGA, should call dataLayer push and logCustomEvent too", async () => {
|
|
// Arrange
|
|
window.dataLayer = [];
|
|
const mockData = {
|
|
actionList: [
|
|
{
|
|
actionName: storeActions.LOG_CUSTOM_EVENT,
|
|
},
|
|
{
|
|
actionName: storeActions.INITIALIZE_SESSION,
|
|
},
|
|
],
|
|
};
|
|
const mocks = setupMocksForJsFiles(mockData);
|
|
var mockDataLayer = [];
|
|
mockDataLayer.push({
|
|
event: "event",
|
|
category: "category",
|
|
action: "action",
|
|
label: "label",
|
|
value: undefined,
|
|
path: "/fmg/?fmgPage=",
|
|
});
|
|
|
|
// Act
|
|
await analyticsMixin.methods.pushEventToGA("category", "action", "label", true);
|
|
|
|
// Assert
|
|
expect(mockDataLayer).toEqual(expect.arrayContaining(window.dataLayer));
|
|
expect(mocks.baseMixin.methods.dispatchStoreAction).toBeCalled();
|
|
});
|
|
|
|
test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 only logs last 5 of label", async () => {
|
|
// Arrange
|
|
window.dataLayer = [];
|
|
var expectedDataLayer = [];
|
|
expectedDataLayer.push({
|
|
event: "event",
|
|
category: "category",
|
|
action: "action",
|
|
label: "33333",
|
|
value: undefined,
|
|
path: "/fmg/?fmgPage=",
|
|
});
|
|
|
|
const mockData = {
|
|
actionList: [
|
|
{
|
|
actionName: storeActions.LOG_CUSTOM_EVENT,
|
|
},
|
|
{
|
|
actionName: storeActions.INITIALIZE_SESSION,
|
|
},
|
|
],
|
|
};
|
|
|
|
const mocks = setupMocksForJsFiles(mockData);
|
|
|
|
// Act
|
|
await analyticsMixin.methods.pushEventToGA(
|
|
"category",
|
|
"action",
|
|
"1111122222333333",
|
|
false,
|
|
ValueToLogTypes.LAST_5
|
|
);
|
|
|
|
// Assert
|
|
expect(expectedDataLayer).toEqual(expect.arrayContaining(window.dataLayer));
|
|
});
|
|
|
|
test("pushEventToGA, should call dataLayer push and ValueToLogTypes.LAST_5 logs only the last 3 characters for a 3 character string", async () => {
|
|
// Arrange
|
|
window.dataLayer = [];
|
|
var expectedDataLayer = [];
|
|
expectedDataLayer.push({
|
|
event: "event",
|
|
category: "category",
|
|
action: "action",
|
|
label: "111",
|
|
value: undefined,
|
|
path: "/fmg/?fmgPage=",
|
|
});
|
|
|
|
const mockData = {
|
|
actionList: [
|
|
{
|
|
actionName: storeActions.LOG_CUSTOM_EVENT,
|
|
},
|
|
{
|
|
actionName: storeActions.INITIALIZE_SESSION,
|
|
},
|
|
],
|
|
};
|
|
|
|
const mocks = setupMocksForJsFiles(mockData);
|
|
|
|
// Act
|
|
await analyticsMixin.methods.pushEventToGA(
|
|
"category",
|
|
"action",
|
|
"111",
|
|
false,
|
|
ValueToLogTypes.LAST_5
|
|
);
|
|
|
|
// Assert
|
|
expect(expectedDataLayer).toEqual(expect.arrayContaining(window.dataLayer));
|
|
});
|
|
|
|
test("Experiments, should push to dataLayer with default Google Custom Dimension Index", () => {
|
|
// Arrange
|
|
window.dataLayer = [];
|
|
|
|
const mockExperimentData = [
|
|
{
|
|
settings: {},
|
|
variationName: "test",
|
|
universeName: "testUniverse",
|
|
},
|
|
];
|
|
|
|
// Mock store
|
|
jest.mock(
|
|
"@/store",
|
|
() => {
|
|
return {};
|
|
},
|
|
{ virtual: true }
|
|
);
|
|
|
|
store.getters = {
|
|
applicationUser: {
|
|
experiments: [
|
|
{
|
|
settings: {},
|
|
variationName: "test",
|
|
universeName: "testUniverse",
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
// Act
|
|
analyticsMixin.methods.pushExperimentsToDataLayer(mockExperimentData);
|
|
|
|
// Assert
|
|
expect(window.dataLayer).toEqual([
|
|
{
|
|
experimentId_99: undefined,
|
|
variationId_99: undefined,
|
|
experimentName_99: "testUniverse",
|
|
variationName_99: "test",
|
|
customDimension_99: "undefined_undefined_testUniverse_test",
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("Experiments, should push to dataLayer with custom Google Custom Dimension Index", () => {
|
|
// Arrange
|
|
window.dataLayer = [];
|
|
|
|
const mockExperimentData = [
|
|
{
|
|
settings: { "Google Custom Dimension Index": "5" },
|
|
variationName: "test",
|
|
universeName: "testUniverse",
|
|
},
|
|
];
|
|
|
|
// Mock store
|
|
jest.mock(
|
|
"@/store",
|
|
() => {
|
|
return {};
|
|
},
|
|
{ virtual: true }
|
|
);
|
|
|
|
store.getters = {
|
|
applicationUser: {
|
|
experiments: [
|
|
{
|
|
settings: { "Google Custom Dimension Index": "5" },
|
|
variationName: "test",
|
|
universeName: "testUniverse",
|
|
},
|
|
],
|
|
},
|
|
};
|
|
|
|
// Act
|
|
analyticsMixin.methods.pushExperimentsToDataLayer(mockExperimentData);
|
|
|
|
// Assert
|
|
expect(window.dataLayer).toEqual([
|
|
{
|
|
experimentId_5: undefined,
|
|
variationId_5: undefined,
|
|
experimentName_5: "testUniverse",
|
|
variationName_5: "test",
|
|
customDimension_5: "undefined_undefined_testUniverse_test",
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("Obj is not null after action prepended", () => {
|
|
//Arrange
|
|
const obj = { baseMethodName: "testMethodName", data: "testData" };
|
|
const method = { name: "testMethodName", data: "testData" };
|
|
const action = "testAction";
|
|
|
|
//Act
|
|
analyticsMixin.methods.prependActionToMethod(obj, method, action);
|
|
|
|
//Assert
|
|
expect(obj != null);
|
|
});
|
|
test("Obj method name does not include bound", () => {
|
|
//Arrange
|
|
const obj = { baseMethodName: "testMethodName", data: "testData" };
|
|
const method = { name: "testMethodName", data: "testData" };
|
|
const action = "testAction";
|
|
|
|
//Act
|
|
analyticsMixin.methods.prependActionToMethod(obj, method, action);
|
|
|
|
//Assert
|
|
expect(method.name.startsWith("bound ")).toBe(false);
|
|
});
|
|
test("Prepended action does not include bound", () => {
|
|
//Arrange
|
|
const obj = { baseMethodName: "testMethodName", data: "testData" };
|
|
const method = { name: "testMethodName", data: "testData" };
|
|
const action = "testAction";
|
|
|
|
//Act
|
|
analyticsMixin.methods.prependActionToMethod(obj, method, action);
|
|
|
|
//Assert
|
|
expect(action.startsWith("bound ")).toBe(false);
|
|
});
|
|
|
|
test("analyticsPageEvents returns constants analyticsPageEvents", () => {
|
|
//Act
|
|
const analyticsPE = analyticsMixin.computed.analyticsPageEvents();
|
|
|
|
//Assert
|
|
expect(analyticsPE).toEqual(analyticsPageEvents);
|
|
});
|
|
|
|
test("GaActions returns constants GaActions", () => {
|
|
//Act
|
|
const gaActions = analyticsMixin.computed.GaActions();
|
|
|
|
//Assert
|
|
expect(gaActions).toEqual(GaActions);
|
|
});
|
|
|
|
test("GaCategories returns constants GaCategories", () => {
|
|
//Act
|
|
const gaCategories = analyticsMixin.computed.GaCategories();
|
|
|
|
//Assert
|
|
expect(gaCategories).toEqual(GaCategories);
|
|
});
|
|
|
|
test("GaLabels returns constants GaLabels", () => {
|
|
//Act
|
|
const gaLabels = analyticsMixin.computed.GaLabels();
|
|
|
|
//Assert
|
|
expect(gaLabels).toEqual(GaLabels);
|
|
});
|
|
});
|