11'use strict' ;
22
3+ const Path = require ( 'path' ) ;
34const { assert, expect } = require ( 'chai' ) ;
45const JsonLoader = require ( '../lib/gulp-json-loader' ) ;
56
67const {
78 constants,
89 // BrushColors,
910
10- getAbsolutePath,
1111 brush,
1212 reportAction,
1313 loadImportsAsync,
@@ -18,105 +18,81 @@ const {
1818
1919const Imports = require ( './data' ) ;
2020
21+ const getAbsolutePath = ( subdir ) => Path . join ( process . cwd ( ) , subdir ) ;
2122const getPugFile = ( filename ) => ( {
2223 path : getAbsolutePath ( `./src/data/pages/${ filename } .pug` )
2324} ) ;
2425
2526describe ( 'Gulp Json Loader' , ( ) => {
2627
27- it ( 'getAbsolutePath()' , ( ) => {
28- const absolutePath1 = getAbsolutePath ( './src/html' ) ;
29- const expected1 = process . cwd ( ) + '/src/html' ;
30-
31- const absolutePath2 = getAbsolutePath ( 'src' , 'pug' ) ;
32- const expected2 = process . cwd ( ) + '/src/pug' ;
33-
34- expect ( constants . APP_PATH ) . equal ( process . cwd ( ) ) ;
35- expect ( absolutePath1 ) . equal ( expected1 ) ;
36- expect ( absolutePath2 ) . equal ( expected2 ) ;
37- } ) ;
38-
3928 describe ( 'factory()' , ( ) => {
40- it ( 'Run with default settings' , ( ) => {
29+ it ( 'init with default settings' , ( ) => {
4130 const testMode = true ;
4231 const settings = undefined ;
4332
44- const bindData = JsonLoader ( settings , testMode ) ;
33+ const { context } = JsonLoader ( settings , testMode ) ;
4534
46- expect ( bindData ) . property ( 'pathHtml' , getAbsolutePath ( './src/html' ) ) ;
47- expect ( bindData ) . property ( 'pathData' , getAbsolutePath ( './src/data' ) ) ;
48- expect ( bindData ) . property ( 'sourcePath' , 'src' ) ;
49- expect ( bindData ) . property ( 'dataEntry' , '$' ) ;
50- expect ( bindData ) . property ( 'locales' , 'ru-UA' ) ;
51- expect ( bindData ) . property ( 'CachedData ' ) . eql ( { } ) ;
52- expect ( bindData ) . property ( 'report' , true ) ;
35+ expect ( context ) . property ( 'pathHtml' , getAbsolutePath ( './src/html' ) ) ;
36+ expect ( context ) . property ( 'pathData' , getAbsolutePath ( './src/data' ) ) ;
37+ expect ( context ) . property ( 'sourcePath' , 'src' ) ;
38+ expect ( context ) . property ( 'dataEntry' , '$' ) ;
39+ expect ( context ) . property ( 'locales' , 'ru-UA' ) ;
40+ expect ( context ) . property ( 'cachedData ' ) . eql ( { } ) ;
41+ expect ( context ) . property ( 'report' , true ) ;
5342 } ) ;
5443
55- it ( 'Run with a specific Data entry' , ( ) => {
44+ it ( 'init with a specific Data entry' , ( ) => {
5645 const testMode = true ;
5746 const settings = {
5847 dataEntry : '$Data' ,
5948 } ;
6049
61- const bindData = JsonLoader ( settings , testMode ) ;
50+ const { context } = JsonLoader ( settings , testMode ) ;
6251
63- expect ( bindData ) . property ( 'pathHtml' , getAbsolutePath ( './src/html' ) ) ;
64- expect ( bindData ) . property ( 'pathData' , getAbsolutePath ( './src/data' ) ) ;
65- expect ( bindData ) . property ( 'sourcePath' , 'src' ) ;
66- expect ( bindData ) . property ( 'dataEntry' , '$Data' ) ;
67- expect ( bindData ) . property ( 'locales' , 'en-EN' ) ;
68- expect ( bindData ) . property ( 'CachedData' ) . eql ( { } ) ;
69- expect ( bindData ) . property ( 'report' , true ) ;
52+ expect ( context ) . property ( 'dataEntry' , '$Data' ) ;
7053 } ) ;
7154
72- it ( 'Run with a specific html & data paths' , ( ) => {
55+ it ( 'init with a specific html & data paths' , ( ) => {
7356 const testMode = true ;
7457 const settings = {
7558 pathHtml : './src/pug' ,
7659 pathData : './src/json' ,
7760 } ;
7861
79- const bindData = JsonLoader ( settings , testMode ) ;
62+ const { context } = JsonLoader ( settings , testMode ) ;
8063
81- expect ( bindData ) . property ( 'pathHtml' , getAbsolutePath ( './src/pug' ) ) ;
82- expect ( bindData ) . property ( 'pathData' , getAbsolutePath ( './src/json' ) ) ;
83- expect ( bindData ) . property ( 'sourcePath' , 'src' ) ;
84- expect ( bindData ) . property ( 'dataEntry' , 'data' ) ;
85- expect ( bindData ) . property ( 'locales' , 'en-EN' ) ;
86- expect ( bindData ) . property ( 'CachedData' ) . eql ( { } ) ;
87- expect ( bindData ) . property ( 'report' , true ) ;
64+ expect ( context ) . property ( 'pathHtml' , getAbsolutePath ( './src/pug' ) ) ;
65+ expect ( context ) . property ( 'pathData' , getAbsolutePath ( './src/json' ) ) ;
66+ expect ( context ) . property ( 'dataEntry' , 'data' ) ;
8867 } ) ;
8968
90- it ( 'Run with a specific source path' , ( ) => {
69+ it ( 'init with a specific source path' , ( ) => {
9170 const testMode = true ;
9271 const settings = {
9372 sourcePath : 'source' ,
9473 } ;
9574
96- const bindData = JsonLoader ( settings , testMode ) ;
75+ const { context } = JsonLoader ( settings , testMode ) ;
9776
98- expect ( bindData ) . property ( 'pathHtml' , getAbsolutePath ( './source/html' ) ) ;
99- expect ( bindData ) . property ( 'pathData' , getAbsolutePath ( './source/data' ) ) ;
100- expect ( bindData ) . property ( 'sourcePath' , 'source' ) ;
101- expect ( bindData ) . property ( 'dataEntry' , 'data' ) ;
102- expect ( bindData ) . property ( 'locales' , 'en-EN' ) ;
103- expect ( bindData ) . property ( 'CachedData' ) . eql ( { } ) ;
104- expect ( bindData ) . property ( 'report' , true ) ;
77+ expect ( context ) . property ( 'pathHtml' , getAbsolutePath ( './source/html' ) ) ;
78+ expect ( context ) . property ( 'pathData' , getAbsolutePath ( './source/data' ) ) ;
79+ expect ( context ) . property ( 'sourcePath' , 'source' ) ;
80+ expect ( context ) . property ( 'dataEntry' , 'data' ) ;
10581 } ) ;
10682
107- it ( 'Convert root sign into an internal path' , ( ) => {
83+ it ( 'convert root sign into an internal path' , ( ) => {
10884 const testMode = true ;
10985 const settings = {
11086 sourcePath : '/' ,
11187 } ;
11288
113- const bindData = JsonLoader ( settings , testMode ) ;
89+ const { context } = JsonLoader ( settings , testMode ) ;
11490
115- expect ( bindData ) . property ( 'pathHtml' , getAbsolutePath ( './html' ) ) ;
116- expect ( bindData ) . property ( 'pathData' , getAbsolutePath ( './data' ) ) ;
91+ expect ( context ) . property ( 'pathHtml' , getAbsolutePath ( './html' ) ) ;
92+ expect ( context ) . property ( 'pathData' , getAbsolutePath ( './data' ) ) ;
11793 } ) ;
11894
119- it ( 'throw an error on referencing an external path' , ( ) => {
95+ it ( 'throw error on referencing an external path' , ( ) => {
12096 const testMode = true ;
12197 const settings = {
12298 sourcePath : '../' ,
@@ -126,22 +102,31 @@ describe('Gulp Json Loader', () => {
126102 JsonLoader ( settings , testMode ) ;
127103 assert . fail ( 'should throw an error' ) ;
128104 } catch ( error ) {
129- expect ( error . message ) . equal ( constants . ERR_TOP_DIR ) ;
105+ expect ( error . message ) . equal ( constants . ERR_EXTERNAL_PATH ) ;
130106 }
131107 } ) ;
132108 } ) ;
133109
134110 describe ( 'loadJsonData()' , ( ) => {
135111 it ( 'successfully load JSON data' , async ( ) => {
136- const load = JsonLoader ( { report : false } ) ;
137- const pocket = await load ( getPugFile ( 'about' ) ) ;
112+ const ctx = {
113+ // sourcePath: 'src',
114+ pathHtml : getAbsolutePath ( './src/html' ) ,
115+ pathData : getAbsolutePath ( './src/data' ) ,
116+ dataEntry : '$' ,
117+ locales : 'en-EN' ,
118+ cachedData : { } ,
119+ report : false ,
120+ } ;
121+
122+ const pocket = await loadJsonData . call ( ctx , getPugFile ( 'about' ) ) ;
138123
139124 const cacheKeyImports = 'src/data/imports/genres.json' ;
140125 const cacheKeyData = 'src/data/pages/about.json' ;
141126
142127 // Check out loaded data
143128 expect ( pocket ) . property ( 'filename' , 'about' ) ;
144- expect ( pocket ) . property ( 'data ' ) . eql ( {
129+ expect ( pocket ) . property ( '$ ' ) . eql ( {
145130 name : 'About Us' ,
146131 href : 'about-us.html' ,
147132 visible : true ,
@@ -151,9 +136,114 @@ describe('Gulp Json Loader', () => {
151136 } ) ;
152137
153138 // Check out cached data
154- // expect(CachedData).property(cacheKeyData).eql(pocket);
155- // expect(CachedData).property(cacheKeyImports).eql(pocket.$.imports.genres);
139+ expect ( ctx . cachedData ) . property ( cacheKeyData ) . eql ( pocket ) ;
140+ expect ( ctx . cachedData ) . property ( cacheKeyImports ) . eql ( pocket . $ . imports . genres ) ;
141+ } ) ;
142+ } ) ;
143+
144+ describe ( 'loadImportsAsync()' , ( ) => {
145+ it ( 'import from file' , async ( ) => {
146+ const context = {
147+ pathData : getAbsolutePath ( './src/data' ) ,
148+ report : false ,
149+ } ;
150+ const imports = [ 'genres' ] ;
151+ const cachedData = { } ;
152+
153+ const importedData = await loadImportsAsync ( context , imports , cachedData ) ;
154+ const cacheKey = 'src/data/imports/genres.json' ;
155+
156+ expect ( importedData ) . property ( 'genres' ) . eql ( Imports . genres ) ;
157+ expect ( cachedData ) . property ( cacheKey ) . eql ( Imports . genres ) ;
158+ } ) ;
159+
160+ it ( 'import from cache' , async ( ) => {
161+ const context = {
162+ pathData : getAbsolutePath ( './src/data' ) ,
163+ report : false ,
164+ } ;
165+ const imports = [ 'genres' ] ;
166+ const cacheKey = 'src/data/imports/genres.json' ;
167+ const cachedData = {
168+ [ cacheKey ] : [
169+ { href : '#href1' , name : 'Caption1' } ,
170+ { href : '#href2' , name : 'Caption2' } ,
171+ ]
172+ } ;
173+
174+ const importedData = await loadImportsAsync ( context , imports , cachedData ) ;
175+
176+ expect ( importedData ) . eql ( {
177+ genres : cachedData [ cacheKey ]
178+ } ) ;
179+ } ) ;
180+
181+ it ( 'return empty object on empty array passing' , async ( ) => {
182+ const context = { } ;
183+ const cachedData = { } ;
184+ const imports = [ ] ;
185+
186+ const importedData = await loadImportsAsync ( context , imports , cachedData ) ;
187+ expect ( importedData ) . eql ( { } ) ;
188+ } ) ;
189+
190+ it ( 'throw error on non-array passed' , async ( ) => {
191+ const context = { } ;
192+ const cachedData = { } ;
193+ const imports = undefined ;
194+
195+ try {
196+ await loadImportsAsync ( context , imports , cachedData ) ;
197+ assert . fail ( 'should throw an error' ) ;
198+ } catch ( err ) {
199+ expect ( err . message ) . equal ( 'Imports should be an Array' ) ;
200+ }
156201 } ) ;
157202 } ) ;
158203
204+ describe ( 'loadImportsRecursively()' , ( ) => {
205+ it ( 'successfully load data' , async ( ) => {
206+ const cacheKeyMenu = 'src/data/imports/menu.json' ;
207+ const cacheKeyGenres = 'src/data/imports/genres.json' ;
208+
209+ const options = {
210+ ctx : {
211+ pathData : getAbsolutePath ( './src/data' ) ,
212+ report : false ,
213+ } ,
214+ imports : [ 'menu' , 'genres' ] ,
215+ pocket : { } ,
216+ cachedData : {
217+ [ cacheKeyMenu ] : Imports . menu
218+ } ,
219+ } ;
220+
221+ await loadImportsRecursively ( options , ( error , pocket ) => {
222+ expect ( error ) . equal ( null ) ;
223+
224+ expect ( pocket ) . property ( 'menu' ) . eql ( Imports . menu ) ;
225+ expect ( pocket ) . property ( 'genres' ) . eql ( Imports . genres ) ;
226+
227+ expect ( options . cachedData ) . property ( cacheKeyMenu ) ;
228+ expect ( options . cachedData ) . property ( cacheKeyGenres ) ;
229+ } ) ;
230+ } ) ;
231+
232+ it ( 'throw error on referencing an external path' , async ( ) => {
233+ const options = {
234+ ctx : {
235+ pathData : getAbsolutePath ( '../' ) ,
236+ report : false ,
237+ } ,
238+ imports : [ 'menu' ] ,
239+ pocket : { } ,
240+ cachedData : { } ,
241+ } ;
242+
243+ await loadImportsRecursively ( options , ( error , pocket ) => {
244+ expect ( error . message ) . equal ( constants . ERR_EXTERNAL_PATH ) ;
245+ expect ( pocket ) . equal ( null ) ;
246+ } ) ;
247+ } ) ;
248+ } ) ;
159249} ) ;
0 commit comments