Skip to content

Commit

Permalink
feat(testing): Replace mocking store with renderWrappedHook state
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninfa-Jeon committed Feb 18, 2025
1 parent 2e9bef6 commit 7c230f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/components/LogIn/LogIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ export default function LogIn() {
if (userIsLoggedIn) {
sendAnalytics({
category: "Authentication",
action: "User Login",
action: `User Login (${config?.authMethod})`,
});
}
}, [userIsLoggedIn, sendAnalytics]);
}, [userIsLoggedIn, sendAnalytics, config]);

let form: ReactNode = null;
switch (config?.authMethod) {
Expand Down
55 changes: 38 additions & 17 deletions src/hooks/useAnalytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import ReactGA from "react-ga4";
import type { MockInstance } from "vitest";
import { vi } from "vitest";

import * as store from "store/store";
import { rootStateFactory } from "testing/factories";
import { configFactory, generalStateFactory } from "testing/factories/general";
import { renderWrappedHook } from "testing/utils";

import useAnalytics from "./useAnalytics";
Expand Down Expand Up @@ -34,31 +35,46 @@ describe("useAnalytics", () => {
});

it("does not send events in development", () => {
vi.spyOn(store, "useAppSelector").mockImplementation(
vi.fn().mockReturnValue(true),
);
vi.stubEnv("PROD", false);
const { result } = renderWrappedHook(() => useAnalytics());
const { result } = renderWrappedHook(() => useAnalytics(), {
state: rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
analyticsEnabled: true,
}),
}),
}),
});
result.current({ path: "/some/path" });
expect(eventSpy).not.toHaveBeenCalled();
expect(pageviewSpy).not.toHaveBeenCalled();
});

it("does not send events if analytics are disabled", () => {
vi.spyOn(store, "useAppSelector").mockImplementation(
vi.fn().mockReturnValue(false),
);
const { result } = renderWrappedHook(() => useAnalytics());
const { result } = renderWrappedHook(() => useAnalytics(), {
state: rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
analyticsEnabled: false,
}),
}),
}),
});
result.current({ path: "/some/path" });
expect(eventSpy).not.toHaveBeenCalled();
expect(pageviewSpy).not.toHaveBeenCalled();
});

it("can send pageview events", () => {
vi.spyOn(store, "useAppSelector").mockImplementation(
vi.fn().mockReturnValue(true),
);
const { result } = renderWrappedHook(() => useAnalytics());
const { result } = renderWrappedHook(() => useAnalytics(), {
state: rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
analyticsEnabled: true,
}),
}),
}),
});
result.current({ path: "/some/path" });
expect(pageviewSpy).toHaveBeenCalledWith({
hitType: "pageview",
Expand All @@ -67,10 +83,15 @@ describe("useAnalytics", () => {
});

it("can send events", () => {
vi.spyOn(store, "useAppSelector").mockImplementation(
vi.fn().mockReturnValue(true),
);
const { result } = renderWrappedHook(() => useAnalytics());
const { result } = renderWrappedHook(() => useAnalytics(), {
state: rootStateFactory.build({
general: generalStateFactory.build({
config: configFactory.build({
analyticsEnabled: true,
}),
}),
}),
});
result.current({ category: "sidebar", action: "toggle" });
expect(eventSpy).toHaveBeenCalledWith({
category: "sidebar",
Expand Down

0 comments on commit 7c230f2

Please sign in to comment.