Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c0269c7
commit 4874c55
Showing
11 changed files
with
181 additions
and
30 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
"env": { | ||
"browser": true, | ||
"node": true, | ||
"es6": true | ||
"es6": true, | ||
"jest": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/*.js | ||
node_modules | ||
package-lock.json | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ src | |
.editorconfig | ||
.eslintrc | ||
.travis.yml | ||
coverage | ||
__tests__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
language: node_js | ||
node_js: node | ||
|
||
before_script: | ||
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter | ||
- chmod +x ./cc-test-reporter | ||
- ./cc-test-reporter before-build | ||
|
||
script: | ||
- npm test | ||
- npm run build | ||
|
||
after_script: | ||
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT | ||
|
||
notifications: | ||
email: false | ||
email: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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', () => { | ||
onRouteUpdate({}) | ||
expect(window._paq.push).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('sends page view in dev mode', () => { | ||
window.dev = true | ||
onRouteUpdate({}) | ||
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 | ||
onRouteUpdate({}) | ||
jest.runAllTimers() | ||
expect(setTimeout).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('sends page view', () => { | ||
onRouteUpdate({}) | ||
jest.runAllTimers() | ||
expect(window._paq.push).toHaveBeenCalledTimes(5) | ||
}) | ||
|
||
it(`uses setTimeout with a minimum delay of 32ms`, () => { | ||
onRouteUpdate({}) | ||
jest.runAllTimers() | ||
expect(setTimeout).toHaveBeenCalledWith(expect.any(Function), 32) | ||
expect(window._paq.push).toHaveBeenCalledTimes(5) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { onRenderBody } from '../gatsby-ssr' | ||
|
||
describe('gatsby-plugin-google-analytics', () => { | ||
describe('gatsby-ssr', () => { | ||
describe('onRenderBody', () => { | ||
describe('in non-production env', () => { | ||
it('does not set tracking script', () => { | ||
const setHeadComponents = jest.fn() | ||
onRenderBody({ setHeadComponents }) | ||
expect(setHeadComponents).not.toHaveBeenCalled() | ||
}) | ||
}) | ||
|
||
describe('in production env', () => { | ||
let env | ||
|
||
beforeAll(() => { | ||
env = process.env.NODE_ENV | ||
process.env.NODE_ENV = 'production' | ||
}) | ||
|
||
afterAll(() => { | ||
process.env.NODE_ENV = env | ||
}) | ||
|
||
const setup = options => { | ||
const setHeadComponents = jest.fn() | ||
const setPostBodyComponents = jest.fn() | ||
|
||
options = Object.assign( | ||
{ | ||
siteId: 'TEST_SITE_ID', | ||
matomoUrl: 'TEST_MATOMO_URL', | ||
siteUrl: 'TEST_SITE_URL' | ||
}, | ||
options | ||
) | ||
|
||
onRenderBody({ setHeadComponents, setPostBodyComponents }, options) | ||
|
||
return { | ||
setHeadComponents, | ||
setPostBodyComponents | ||
} | ||
} | ||
|
||
it('sets tracking script', () => { | ||
const { setHeadComponents, setPostBodyComponents } = setup() | ||
|
||
expect(setHeadComponents).toHaveBeenCalledTimes(1) | ||
expect(setPostBodyComponents).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('sets siteId', () => { | ||
const { setPostBodyComponents } = setup() | ||
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) | ||
expect(result).toMatch(/TEST_SITE_ID/) | ||
}) | ||
|
||
it('sets matomoUrl', () => { | ||
const { setPostBodyComponents } = setup() | ||
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) | ||
expect(result).toMatch(/TEST_MATOMO_URL/) | ||
}) | ||
|
||
it('sets siteUrl', () => { | ||
const { setPostBodyComponents } = setup() | ||
const result = JSON.stringify(setPostBodyComponents.mock.calls[0][0]) | ||
expect(result).toMatch(/TEST_SITE_URL/) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters