File tree 2 files changed +67
-10
lines changed
packages/gatsby-plugin-guess-js/src
2 files changed +67
-10
lines changed Original file line number Diff line number Diff line change
1
+ import { onPreInit , onPreBootstrap } from "../gatsby-node"
2
+ import { GuessPlugin } from "guess-webpack"
3
+
4
+ jest . mock ( `guess-webpack` )
5
+
6
+ describe ( `gatsby-plugin-guess-js` , ( ) => {
7
+ describe ( `onPreInit` , ( ) => {
8
+ it ( `should delete jwt pluginOptions` , ( ) => {
9
+ const pluginOptions = {
10
+ jwt : `mykeys` ,
11
+ }
12
+
13
+ onPreInit ( { } , pluginOptions )
14
+
15
+ expect ( pluginOptions ) . not . toHaveProperty ( `jwt` )
16
+ } )
17
+ } )
18
+
19
+ describe ( `onPreBootstrap` , ( ) => {
20
+ it ( `should still have a jwt token to be used in jwt` , ( ) => {
21
+ const pluginOptions = {
22
+ jwt : `mykeys` ,
23
+ // period: {
24
+ // start: `2019-10-09`,
25
+ // end: `2019-10-10`,
26
+ // },
27
+ GAViewID : `1234` ,
28
+ }
29
+
30
+ onPreInit ( { } , pluginOptions )
31
+ onPreBootstrap ( { } , pluginOptions )
32
+
33
+ expect ( GuessPlugin ) . toHaveBeenNthCalledWith (
34
+ 1 ,
35
+ expect . objectContaining ( {
36
+ jwt : `mykeys` ,
37
+ GA : `1234` ,
38
+ } )
39
+ )
40
+ } )
41
+ } )
42
+ } )
Original file line number Diff line number Diff line change 1
1
const { GuessPlugin } = require ( `guess-webpack` )
2
2
3
3
let guessPlugin
4
+ let jwt
5
+
4
6
exports . onPreInit = ( _ , pluginOptions ) => {
5
- const { period, jwt, GAViewID, reportProvider } = pluginOptions
6
- // delete sensitive information after use
7
+ jwt = pluginOptions . jwt
8
+
9
+ // remove jwt from our config as we don't want it to leak into gatsby-browser.js
7
10
delete pluginOptions . jwt
8
- period . startDate = new Date ( period . startDate )
9
- period . endDate = new Date ( period . endDate )
11
+ }
12
+
13
+ exports . onPreBootstrap = ( _ , pluginOptions ) => {
14
+ const { GAViewID, reportProvider } = pluginOptions
15
+ let { period } = pluginOptions
16
+
17
+ if ( period ) {
18
+ period . startDate = new Date ( period . startDate )
19
+ period . endDate = new Date ( period . endDate )
20
+ } else {
21
+ const startDate = new Date ( )
22
+ // We'll load 1 year of data if no period is being specified
23
+ startDate . setDate ( startDate . getDate ( ) - 365 )
24
+ period = {
25
+ startDate,
26
+ endDate : new Date ( ) ,
27
+ }
28
+ }
29
+
10
30
guessPlugin = new GuessPlugin ( {
11
31
// GA view ID.
12
32
GA : GAViewID ,
@@ -28,12 +48,7 @@ exports.onPreInit = (_, pluginOptions) => {
28
48
29
49
// Optional argument. It takes the data for the last year if not
30
50
// specified.
31
- period : period
32
- ? period
33
- : {
34
- startDate : new Date ( `2018-1-1` ) ,
35
- endDate : new Date ( ) ,
36
- } ,
51
+ period,
37
52
} )
38
53
}
39
54
You can’t perform that action at this time.
0 commit comments