-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41f7347
commit 2ccc967
Showing
4 changed files
with
137 additions
and
44 deletions.
There are no files selected for viewing
120 changes: 94 additions & 26 deletions
120
app/javascript/__tests__/hooks/useGTMDataLayer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,127 @@ | ||
import React from "react" | ||
import { renderHook } from "@testing-library/react" | ||
import { useGTMDataLayer } from "../../hooks/analytics/useGTMDataLayer" | ||
import { | ||
useGTMDataLayer, | ||
useGTMDataLayerWithoutUserContext, | ||
} from "../../hooks/analytics/useGTMDataLayer" | ||
import TagManager from "react-gtm-module" | ||
import UserContext from "../../authentication/context/UserContext" | ||
import { mockProfileStub } from "../__util__/accountUtils" | ||
|
||
describe("useGTMDataLayer", () => { | ||
let consoleSpy | ||
const mockedDate = new Date() | ||
|
||
beforeEach(() => { | ||
consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {}) | ||
jest.useFakeTimers() | ||
jest.setSystemTime(mockedDate) | ||
}) | ||
|
||
afterEach(() => { | ||
consoleSpy.mockRestore() | ||
}) | ||
|
||
it("pushes data to the data layer", () => { | ||
const event = "testEvent" | ||
const data = { test: "data" } | ||
describe("User Information is available", () => { | ||
let pushToDataLayer | ||
|
||
const { result } = renderHook(() => useGTMDataLayer()) | ||
beforeEach(() => { | ||
const wrapper = ({ children }) => ( | ||
<UserContext.Provider value={{ profile: mockProfileStub }}>{children}</UserContext.Provider> | ||
) | ||
|
||
result.current.pushToDataLayer(event, data) | ||
const { result } = renderHook(() => useGTMDataLayer(), { wrapper }) | ||
|
||
expect(TagManager.dataLayer).toHaveBeenCalledWith({ dataLayer: { event, ...data } }) | ||
}) | ||
pushToDataLayer = result.current.pushToDataLayer | ||
}) | ||
|
||
it("pushes data to the data layer", () => { | ||
const event = "testEvent" | ||
const data = { test: "data" } | ||
|
||
pushToDataLayer(event, data) | ||
|
||
expect(TagManager.dataLayer).toHaveBeenCalledWith({ | ||
dataLayer: { | ||
event, | ||
event_timestamp: mockedDate.toISOString(), | ||
...data, | ||
user_id: mockProfileStub.id, | ||
}, | ||
}) | ||
}) | ||
|
||
it("errors out when no event is provided", () => { | ||
const event = undefined | ||
const data = { test: "data" } | ||
|
||
pushToDataLayer(event, data) | ||
|
||
it("errors out when no data is provided", () => { | ||
const event = "testEvent" | ||
const data = undefined | ||
expect(consoleSpy).toHaveBeenCalled() | ||
}) | ||
|
||
const { result } = renderHook(() => useGTMDataLayer()) | ||
it("errors out when an event property is provided in the data object", () => { | ||
const event = "testEvent" | ||
const data = { test: "data", event: "testEvent" } | ||
|
||
result.current.pushToDataLayer(event, data) | ||
pushToDataLayer(event, data) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
expect(consoleSpy).toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
it("errors out when no event is provided", () => { | ||
const event = undefined | ||
const data = { test: "data" } | ||
describe("User Information is not available", () => { | ||
let pushToDataLayer | ||
|
||
const { result } = renderHook(() => useGTMDataLayer()) | ||
beforeEach(() => { | ||
const wrapper = ({ children }) => ( | ||
<UserContext.Provider value={{ profile: undefined }}>{children}</UserContext.Provider> | ||
) | ||
|
||
result.current.pushToDataLayer(event, data) | ||
const { result } = renderHook(() => useGTMDataLayer(), { wrapper }) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
pushToDataLayer = result.current.pushToDataLayer | ||
}) | ||
|
||
it("pushed data to the data layer but with an undefined user_id", () => { | ||
const event = "testEvent" | ||
const data = { test: "data" } | ||
|
||
pushToDataLayer(event, data) | ||
|
||
expect(TagManager.dataLayer).toHaveBeenCalledWith({ | ||
dataLayer: { | ||
event, | ||
event_timestamp: mockedDate.toISOString(), | ||
...data, | ||
user_id: undefined, // when a value is undefined it is ignored by GTM | ||
}, | ||
}) | ||
}) | ||
}) | ||
|
||
it("errors out when an event property is provided in the data object", () => { | ||
const event = "testEvent" | ||
const data = { test: "data", event: "testEvent" } | ||
describe("useGTMDataLayerWithoutUserContext", () => { | ||
let pushToDataLayer | ||
|
||
beforeEach(() => { | ||
const { result } = renderHook(() => useGTMDataLayerWithoutUserContext()) | ||
|
||
pushToDataLayer = result.current.pushToDataLayer | ||
}) | ||
|
||
const { result } = renderHook(() => useGTMDataLayer()) | ||
it("pushed data to the data layer but without a user_id", () => { | ||
const event = "testEvent" | ||
const data = { test: "data" } | ||
|
||
result.current.pushToDataLayer(event, data) | ||
pushToDataLayer(event, data) | ||
|
||
expect(consoleSpy).toHaveBeenCalled() | ||
expect(TagManager.dataLayer).toHaveBeenCalledWith({ | ||
dataLayer: { | ||
event, | ||
event_timestamp: mockedDate.toISOString(), | ||
...data, | ||
}, | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters