gatsby-plugin-matomo/src/__tests__/gatsby-browser.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-02-15 15:21:24 +01:00
import { onRouteUpdate } from '../gatsby-browser'
describe('gatsby-plugin-matomo', () => {
describe('gatsby-browser', () => {
beforeEach(() => {
jest.useFakeTimers()
window._paq = { push: jest.fn() }
})
afterEach(() => {
jest.resetAllMocks()
})
describe('onRouteUpdate', () => {
describe('in non-production env', () => {
beforeAll(() => {
window._paq = { push: jest.fn() }
})
it('does not send page view', () => {
2020-04-06 10:55:51 +02:00
onRouteUpdate({}, {})
2020-02-15 15:21:24 +01:00
expect(window._paq.push).not.toHaveBeenCalled()
})
it('sends page view in dev mode', () => {
window.dev = true
2020-04-06 10:55:51 +02:00
onRouteUpdate({}, {})
2020-02-15 15:21:24 +01:00
expect(window._paq.push).toHaveBeenCalledTimes(1)
})
})
describe('in production env', () => {
let env
beforeAll(() => {
env = process.env.NODE_ENV
process.env.NODE_ENV = 'production'
})
afterAll(() => {
process.env.NODE_ENV = env
})
it('does not send page view when _paq is undefined', () => {
delete window._paq
2020-04-06 10:55:51 +02:00
onRouteUpdate({}, {})
2020-02-15 15:21:24 +01:00
jest.runAllTimers()
expect(setTimeout).not.toHaveBeenCalled()
})
it('sends page view', () => {
2020-04-06 10:55:51 +02:00
onRouteUpdate({}, {})
2020-02-15 15:21:24 +01:00
jest.runAllTimers()
expect(window._paq.push).toHaveBeenCalledTimes(5)
})
it('uses setTimeout with a minimum delay of 32ms', () => {
2020-04-06 10:55:51 +02:00
onRouteUpdate({}, {})
2020-02-15 15:21:24 +01:00
jest.runAllTimers()
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 32)
expect(window._paq.push).toHaveBeenCalledTimes(5)
})
})
})
})
})