|
1
|
import React from "react" |
|
2
|
import { describe, expect, test, vi } from "vitest" |
|
3
|
import { render } from "@testing-library/react" |
|
4
|
|
|
5
|
const mockGoogleAnalytics = vi.fn(() => <script data-testid="ga-script" />) |
|
6
|
|
|
7
|
vi.mock("@next/third-parties/google", () => ({ |
|
8
|
GoogleAnalytics: (props: { gaId: string }) => mockGoogleAnalytics(props), |
|
9
|
})) |
|
10
|
|
|
11
|
import { BareboneLayout } from "../barebone" |
|
12
|
|
|
13
|
describe("BareboneLayout", () => { |
|
14
|
test("does not render GoogleAnalytics when gaId is not provided", () => { |
|
15
|
const { queryByTestId } = render( |
|
16
|
<BareboneLayout> |
|
17
|
<div>content</div> |
|
18
|
</BareboneLayout> |
|
19
|
) |
|
20
|
expect(queryByTestId("ga-script")).not.toBeInTheDocument() |
|
21
|
expect(mockGoogleAnalytics).not.toHaveBeenCalled() |
|
22
|
}) |
|
23
|
|
|
24
|
test("renders GoogleAnalytics with the provided gaId", () => { |
|
25
|
const { queryByTestId } = render( |
|
26
|
<BareboneLayout gaId="G-TESTID"> |
|
27
|
<div>content</div> |
|
28
|
</BareboneLayout> |
|
29
|
) |
|
30
|
expect(queryByTestId("ga-script")).toBeInTheDocument() |
|
31
|
expect(mockGoogleAnalytics).toHaveBeenCalledWith( |
|
32
|
expect.objectContaining({ gaId: "G-TESTID" }) |
|
33
|
) |
|
34
|
}) |
|
35
|
}) |