Skip to content

Commit c15a595

Browse files
test(gatsby-plugin-facebook-analytics): add tests for facebook-analytics plugin (#12481)
1 parent 3a4587d commit c15a595

File tree

5 files changed

+221
-2
lines changed

5 files changed

+221
-2
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`gatsby-plugin-facebook-analytics onRenderBody in production mode sets the correct post body components 1`] = `
4+
Array [
5+
Array [
6+
Array [
7+
<script
8+
dangerouslySetInnerHTML={
9+
Object {
10+
"__html": Array [
11+
"
12+
window.fbAsyncInit = function() {
13+
FB.init({
14+
appId : ",
15+
",
16+
xfbml : true,
17+
version : 'v2.12'
18+
});
19+
20+
FB.AppEvents.logPageView();
21+
22+
};
23+
24+
(function(d, s, id){
25+
var js, fjs = d.getElementsByTagName(s)[0];
26+
if (d.getElementById(id)) {return;}
27+
js = d.createElement(s); js.id = id;
28+
js.src = \\"https://connect.facebook.net/",
29+
"/sdk",
30+
"\\";
31+
fjs.parentNode.insertBefore(js, fjs);
32+
}(document, 'script', 'facebook-jssdk'));",
33+
],
34+
}
35+
}
36+
/>,
37+
],
38+
],
39+
]
40+
`;
41+
42+
exports[`gatsby-plugin-facebook-analytics onRenderBody in production mode sets the correct script src during debug 1`] = `
43+
Array [
44+
Array [
45+
Array [
46+
<script
47+
dangerouslySetInnerHTML={
48+
Object {
49+
"__html": Array [
50+
"
51+
window.fbAsyncInit = function() {
52+
FB.init({
53+
appId : ",
54+
",
55+
xfbml : true,
56+
version : 'v2.12'
57+
});
58+
59+
FB.AppEvents.logPageView();
60+
61+
};
62+
63+
(function(d, s, id){
64+
var js, fjs = d.getElementsByTagName(s)[0];
65+
if (d.getElementById(id)) {return;}
66+
js = d.createElement(s); js.id = id;
67+
js.src = \\"https://connect.facebook.net/",
68+
"/sdk",
69+
"\\";
70+
fjs.parentNode.insertBefore(js, fjs);
71+
}(document, 'script', 'facebook-jssdk'));",
72+
],
73+
}
74+
}
75+
/>,
76+
],
77+
],
78+
]
79+
`;
80+
81+
exports[`gatsby-plugin-facebook-analytics onRenderBody in production mode sets the correct script src for other languages than en_US 1`] = `
82+
Array [
83+
Array [
84+
Array [
85+
<script
86+
dangerouslySetInnerHTML={
87+
Object {
88+
"__html": Array [
89+
"
90+
window.fbAsyncInit = function() {
91+
FB.init({
92+
appId : ",
93+
",
94+
xfbml : true,
95+
version : 'v2.12'
96+
});
97+
98+
FB.AppEvents.logPageView();
99+
100+
};
101+
102+
(function(d, s, id){
103+
var js, fjs = d.getElementsByTagName(s)[0];
104+
if (d.getElementById(id)) {return;}
105+
js = d.createElement(s); js.id = id;
106+
js.src = \\"https://connect.facebook.net/",
107+
"/sdk",
108+
"\\";
109+
fjs.parentNode.insertBefore(js, fjs);
110+
}(document, 'script', 'facebook-jssdk'));",
111+
],
112+
}
113+
}
114+
/>,
115+
],
116+
],
117+
]
118+
`;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { onRouteUpdate } from "../gatsby-browser"
2+
3+
describe(`gatsby-plugin-facebook-analytics`, () => {
4+
describe(`onRouteUpdate`, () => {
5+
describe(`in development mode`, () => {
6+
it(`does not log page views`, () => {
7+
const logPageView = jest.fn()
8+
window.FB = function() {}
9+
window.FB.AppEvents = { logPageView }
10+
11+
onRouteUpdate()
12+
13+
expect(logPageView).not.toHaveBeenCalled()
14+
})
15+
})
16+
17+
describe(`in production mode`, () => {
18+
let env
19+
20+
beforeAll(() => {
21+
env = process.env.NODE_ENV
22+
process.env.NODE_ENV = `production`
23+
})
24+
25+
afterAll(() => {
26+
process.env.NODE_ENV = env
27+
})
28+
29+
it(`logs page views`, () => {
30+
const logPageView = jest.fn()
31+
window.FB = function() {}
32+
window.FB.AppEvents = { logPageView }
33+
34+
onRouteUpdate()
35+
36+
expect(logPageView).toHaveBeenCalledTimes(1)
37+
})
38+
})
39+
})
40+
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
jest.mock(`common-tags`, () => {
2+
return {
3+
stripIndent: args => args,
4+
}
5+
})
6+
7+
import { onRenderBody } from "../gatsby-ssr"
8+
9+
describe(`gatsby-plugin-facebook-analytics`, () => {
10+
describe(`onRenderBody`, () => {
11+
describe(`in development mode`, () => {
12+
it(`does not set any post body components`, () => {
13+
const setPostBodyComponents = jest.fn()
14+
15+
onRenderBody({ setPostBodyComponents }, {})
16+
17+
expect(setPostBodyComponents).not.toHaveBeenCalled()
18+
})
19+
})
20+
21+
describe(`in production mode`, () => {
22+
let env
23+
24+
beforeAll(() => {
25+
env = process.env.NODE_ENV
26+
process.env.NODE_ENV = `production`
27+
})
28+
29+
afterAll(() => {
30+
process.env.NODE_ENV = env
31+
})
32+
33+
it(`sets the correct post body components`, () => {
34+
const setPostBodyComponents = jest.fn()
35+
const pluginOptions = { appId: 1 }
36+
37+
onRenderBody({ setPostBodyComponents }, pluginOptions)
38+
39+
expect(setPostBodyComponents.mock.calls).toMatchSnapshot()
40+
})
41+
42+
it(`sets the correct script src during debug`, () => {
43+
const setPostBodyComponents = jest.fn()
44+
const pluginOptions = { appId: 1, debug: true }
45+
46+
onRenderBody({ setPostBodyComponents }, pluginOptions)
47+
48+
expect(setPostBodyComponents.mock.calls).toMatchSnapshot()
49+
})
50+
51+
it(`sets the correct script src for other languages than en_US`, () => {
52+
const setPostBodyComponents = jest.fn()
53+
const pluginOptions = { appId: 1, language: `sv` }
54+
55+
onRenderBody({ setPostBodyComponents }, pluginOptions)
56+
57+
expect(setPostBodyComponents.mock.calls).toMatchSnapshot()
58+
})
59+
})
60+
})
61+
})

packages/gatsby-plugin-facebook-analytics/src/gatsby-browser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exports.onRouteUpdate = function() {
1+
export const onRouteUpdate = () => {
22
// Don't track while developing.
33
if (process.env.NODE_ENV === `production` && typeof FB === `function`) {
44
window.FB.AppEvents.logPageView()

packages/gatsby-plugin-facebook-analytics/src/gatsby-ssr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react"
22
import { stripIndent } from "common-tags"
33

4-
exports.onRenderBody = ({ setPostBodyComponents }, pluginOptions) => {
4+
export const onRenderBody = ({ setPostBodyComponents }, pluginOptions) => {
55
const {
66
appId,
77
includeInDevelopment = false,

0 commit comments

Comments
 (0)