1
0
mirror of https://github.com/kremalicious/gatsby-plugin-matomo.git synced 2024-12-22 17:23:23 +01:00

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

View File

@ -22,9 +22,7 @@ export const onRouteUpdate = ({ location, prevLocation }, pluginOptions) => {
prevLocation &&
prevLocation.pathname + prevLocation.search + prevLocation.hash
const {
trackLoad = true
} = pluginOptions
const { trackLoad = true } = pluginOptions
// document.title workaround stolen from:
// 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
if (trackLoad) {
_paq.push(['trackEvent', 'javascript', 'load', 'duration', getDuration()])
_paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration()
])
}
if (dev) {