1+ const fs = require ( `fs` )
2+
13const { onCreatePage } = require ( `../src/gatsby-node` )
24
35const actions = {
@@ -9,10 +11,54 @@ const mocks = {
911 actions,
1012 page : {
1113 path : "/" ,
14+ context : { } ,
1215 } ,
1316}
1417
15- it ( `does not crash when no pluginConfig is provided` , ( ) => {
18+ beforeAll ( ( ) => {
19+ // Create file src/en.json because the default option for reading a file is `./en.json`
20+ fs . writeFileSync (
21+ `${ __dirname } /../src/en.json` ,
22+ JSON . stringify ( { english : `English version` } )
23+ )
24+ } )
25+
26+ afterAll ( ( ) => {
27+ fs . unlinkSync ( `${ __dirname } /../src/en.json` )
28+ } )
29+
30+ it ( `should not crash when no pluginConfig is provided` , async ( ) => {
1631 const pluginOptions = { }
17- onCreatePage ( mocks , pluginOptions )
32+
33+ await onCreatePage ( mocks , pluginOptions )
34+ } )
35+
36+ it ( `should read translations from file and create corresponding pages` , async ( ) => {
37+ const pluginOptions = {
38+ languages : [ `es` ] ,
39+ defaultLanguage : `es` ,
40+ path : `${ __dirname } /fixtures/intl` ,
41+ }
42+
43+ await onCreatePage ( mocks , pluginOptions )
44+
45+ expect ( actions . createPage . mock . calls . length ) . toBe ( 4 )
46+
47+ // assert the pages created match the requested languages
48+ expect ( actions . createPage . mock . calls [ 0 ] [ 0 ] . path ) . toBe ( `/` )
49+ expect ( actions . createPage . mock . calls [ 1 ] [ 0 ] . path ) . toBe ( `/en/` )
50+ expect ( actions . createPage . mock . calls [ 2 ] [ 0 ] . path ) . toBe ( `/` )
51+ expect ( actions . createPage . mock . calls [ 3 ] [ 0 ] . path ) . toBe ( `/es/` )
52+ } )
53+
54+ it ( `should crash when translations file doesn't exist` , async ( ) => {
55+ const pluginOptions = {
56+ languages : [ `es` , `en` ] ,
57+ defaultLanguage : `es` ,
58+ path : `${ __dirname } /fixtures/intl` ,
59+ }
60+
61+ await expect ( onCreatePage ( mocks , pluginOptions ) ) . rejects . toThrow (
62+ `Cannot find module`
63+ )
1864} )
0 commit comments