Skip to content

Commit 53039f9

Browse files
committed
Improve assertion in OutboundLink tests
1 parent 0bdad1f commit 53039f9

File tree

2 files changed

+46
-47
lines changed

2 files changed

+46
-47
lines changed

packages/gatsby-plugin-google-gtag/src/__tests__/__snapshots__/index.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`index.js <OutboundLink /> matches basic snapshot 1`] = `
3+
exports[`<OutboundLink /> matches basic snapshot 1`] = `
44
<div>
55
<a>
66
link

packages/gatsby-plugin-google-gtag/src/__tests__/index.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,61 @@ import React from "react"
22
import { cleanup, fireEvent, render } from "@testing-library/react"
33
import { OutboundLink } from "../"
44

5-
describe(`index.js`, () => {
6-
describe(`<OutboundLink />`, () => {
7-
afterEach(() => {
8-
cleanup()
9-
jest.resetAllMocks()
10-
})
5+
describe(`<OutboundLink />`, () => {
6+
afterEach(() => {
7+
cleanup()
8+
jest.resetAllMocks()
9+
})
1110

12-
const setup = props => {
13-
const utils = render(<OutboundLink {...props}>link</OutboundLink>)
11+
const setup = props => {
12+
const utils = render(<OutboundLink {...props}>link</OutboundLink>)
1413

15-
return Object.assign({}, utils, {
16-
link: utils.getByText(`link`),
17-
})
18-
}
14+
return Object.assign({}, utils, {
15+
link: utils.getByText(`link`),
16+
})
17+
}
18+
19+
it(`matches basic snapshot`, () => {
20+
const { container } = setup()
1921

20-
it(`matches basic snapshot`, () => {
21-
const { container } = setup()
22+
expect(container).toMatchSnapshot()
23+
})
2224

23-
expect(container).toMatchSnapshot()
25+
describe(`tracking`, () => {
26+
beforeEach(() => {
27+
window.gtag = jest.fn()
2428
})
29+
it(`sends tracking event when clicked`, () => {
30+
const { link } = setup()
2531

26-
describe(`tracking`, () => {
27-
beforeEach(() => {
28-
window.gtag = jest.fn()
32+
fireEvent.click(link)
33+
34+
expect(window.gtag).toHaveBeenCalledTimes(1)
35+
})
36+
it(`sends custom tracking event when clicked`, () => {
37+
const eventCategory = `eventcategory`
38+
const eventAction = `eventaction`
39+
const eventLabel = `eventlabel`
40+
const eventValue = 55
41+
const { link } = setup({
42+
eventCategory,
43+
eventAction,
44+
eventLabel,
45+
eventValue,
2946
})
30-
it(`sends tracking event when clicked`, () => {
31-
const { link } = setup()
3247

33-
fireEvent.click(link)
48+
fireEvent.click(link)
3449

35-
expect(window.gtag).toHaveBeenCalled()
36-
})
37-
it(`sends custom tracking event when clicked`, () => {
38-
const eventCategory = `eventcategory`
39-
const eventAction = `eventaction`
40-
const eventLabel = `eventlabel`
41-
const eventValue = 55
42-
const { link } = setup({
43-
eventCategory,
44-
eventAction,
45-
eventLabel,
46-
eventValue,
50+
expect(window.gtag).toHaveBeenCalledTimes(1)
51+
expect(window.gtag).toHaveBeenCalledWith(
52+
`event`,
53+
eventAction,
54+
expect.objectContaining({
55+
event_category: eventCategory,
56+
event_label: eventLabel,
57+
event_value: eventValue,
4758
})
48-
49-
fireEvent.click(link)
50-
51-
expect(window.gtag).toHaveBeenCalledWith(
52-
`event`,
53-
eventAction,
54-
expect.objectContaining({
55-
event_category: eventCategory,
56-
event_label: eventLabel,
57-
event_value: eventValue,
58-
})
59-
)
60-
})
59+
)
6160
})
6261
})
6362
})

0 commit comments

Comments
 (0)