onRouteUpdate test tweaks

This commit is contained in:
Matthias Kretschmann 2020-04-06 10:55:51 +02:00
parent d64e7ffbbc
commit 5871f667b9
Signed by: m
GPG Key ID: 606EEEF3C479A91F
2 changed files with 13 additions and 9 deletions

View File

@ -18,13 +18,13 @@ describe('gatsby-plugin-matomo', () => {
}) })
it('does not send page view', () => { it('does not send page view', () => {
onRouteUpdate({}) onRouteUpdate({}, {})
expect(window._paq.push).not.toHaveBeenCalled() expect(window._paq.push).not.toHaveBeenCalled()
}) })
it('sends page view in dev mode', () => { it('sends page view in dev mode', () => {
window.dev = true window.dev = true
onRouteUpdate({}) onRouteUpdate({}, {})
expect(window._paq.push).toHaveBeenCalledTimes(1) expect(window._paq.push).toHaveBeenCalledTimes(1)
}) })
}) })
@ -43,19 +43,19 @@ describe('gatsby-plugin-matomo', () => {
it('does not send page view when _paq is undefined', () => { it('does not send page view when _paq is undefined', () => {
delete window._paq delete window._paq
onRouteUpdate({}) onRouteUpdate({}, {})
jest.runAllTimers() jest.runAllTimers()
expect(setTimeout).not.toHaveBeenCalled() expect(setTimeout).not.toHaveBeenCalled()
}) })
it('sends page view', () => { it('sends page view', () => {
onRouteUpdate({}) onRouteUpdate({}, {})
jest.runAllTimers() jest.runAllTimers()
expect(window._paq.push).toHaveBeenCalledTimes(5) expect(window._paq.push).toHaveBeenCalledTimes(5)
}) })
it('uses setTimeout with a minimum delay of 32ms', () => { it('uses setTimeout with a minimum delay of 32ms', () => {
onRouteUpdate({}) onRouteUpdate({}, {})
jest.runAllTimers() jest.runAllTimers()
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 32) expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 32)
expect(window._paq.push).toHaveBeenCalledTimes(5) expect(window._paq.push).toHaveBeenCalledTimes(5)

View File

@ -22,9 +22,7 @@ export const onRouteUpdate = ({ location, prevLocation }, pluginOptions) => {
prevLocation && prevLocation &&
prevLocation.pathname + prevLocation.search + prevLocation.hash prevLocation.pathname + prevLocation.search + prevLocation.hash
const { const { trackLoad = true } = pluginOptions
trackLoad = true
} = pluginOptions
// document.title workaround stolen from: // document.title workaround stolen from:
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js // https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
@ -51,7 +49,13 @@ export const onRouteUpdate = ({ location, prevLocation }, pluginOptions) => {
first = false first = false
if (trackLoad) { if (trackLoad) {
_paq.push(['trackEvent', 'javascript', 'load', 'duration', getDuration()]) _paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration()
])
} }
if (dev) { if (dev) {