|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -var tap = require('tap'); |
4 | | -var rewire = require("rewire"); |
| 3 | +const tap = require('tap'); |
| 4 | +const rewire = require("rewire"); |
| 5 | +const _ = require('lodash'); |
| 6 | +const fs = require('fs-extra'); |
5 | 7 | var config = require('./util/patternlab-config.json'); |
6 | 8 |
|
7 | 9 | var plEngineModule = rewire('../core/lib/patternlab'); |
8 | 10 |
|
9 | 11 | //set up a global mocks - we don't want to be writing/rendering any files right now |
10 | | -var uiBuilderMock = { |
| 12 | +const uiBuilderMock = { |
11 | 13 | buildFrontend: function (patternlab) { } |
12 | 14 | }; |
13 | 15 |
|
| 16 | +const fsMock = { |
| 17 | + outputFileSync: function (path, content) { /* INTENTIONAL NOOP */}, |
| 18 | + readJSONSync: function(path, encoding) { |
| 19 | + return fs.readJSONSync(path, encoding); |
| 20 | + }, |
| 21 | + removeSync: function(path) { fs.removeSync(path); }, |
| 22 | + emptyDirSync: function(path) { fs.emptyDirSync(path); }, |
| 23 | + readFileSync: function(path, encoding) { return fs.readFileSync(path, encoding); }, |
| 24 | +} |
| 25 | + |
14 | 26 | //set our mocks in place of usual require() |
15 | 27 | plEngineModule.__set__({ |
16 | | - 'ui_builder': uiBuilderMock |
| 28 | + 'ui_builder': uiBuilderMock, |
| 29 | + 'fs': fsMock |
17 | 30 | }); |
18 | 31 |
|
19 | | -var pl = new plEngineModule(config); |
20 | | - |
| 32 | +tap.test('buildPatternData - should merge all JSON files in the data folder except listitems', function(test){ |
| 33 | + var data_dir = './test/files/_data/'; |
21 | 34 |
|
22 | | -// tap.test('buildPatternData - should merge all JSON files in the data folder except listitems', function(test){ |
23 | | -// var fs = require('fs-extra'); |
24 | | -// var plMain = require('../core/lib/patternlab'); |
25 | | -// var data_dir = './test/files/_data/'; |
26 | | -// |
27 | | -// var dataResult = plMain.build_pattern_data(data_dir, fs); |
28 | | -// test.equals(dataResult.data, "test"); |
29 | | -// test.equals(dataResult.foo, "bar"); |
30 | | -// test.equals(dataResult.test_list_item, undefined); |
31 | | -// test.end(); |
32 | | -// }); |
| 35 | + var dataResult = plEngineModule.build_pattern_data(data_dir, fs); |
| 36 | + test.equals(dataResult.data, "test"); |
| 37 | + test.equals(dataResult.foo, "bar"); |
| 38 | + test.equals(dataResult.test_list_item, undefined); |
| 39 | + test.end(); |
| 40 | +}); |
33 | 41 |
|
34 | 42 | tap.test('buildPatterns - should replace data link even when pattern parameter present', function(test) { |
35 | 43 | //arrange |
36 | 44 |
|
| 45 | + var patternExporterMock = { |
| 46 | + /* |
| 47 | + In this test, we actually take advantage of the pattern export functionality post-build to inspect what |
| 48 | + the contents of the patterns look like. This, coupled with a mocking of fs and the ui_builder, allow us to focus |
| 49 | + only on the order of events within build. |
| 50 | + */ |
| 51 | + export_patterns: function (patternlab) { |
| 52 | + var pattern = _.find(patternlab.patterns, (pattern) => { |
| 53 | + return pattern.patternPartial === 'test-paramParent'; |
| 54 | + }); |
| 55 | + //assert |
| 56 | + test.equals(pattern.patternPartialCode.indexOf('00-test-00-foo.rendered.html') > -1, true, 'data link should be replaced properly'); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + plEngineModule.__set__({ |
| 61 | + 'pattern_exporter': patternExporterMock |
| 62 | + }); |
| 63 | + |
| 64 | + config.patternExportPatternPartials = ['test-paramParent']; |
| 65 | + var pl = new plEngineModule(config); |
| 66 | + |
37 | 67 | //act |
38 | 68 | pl.build(function() { |
39 | | - |
40 | | - test.equals(1,1); |
41 | 69 | test.end(); |
42 | | - |
43 | 70 | }, true); |
44 | 71 |
|
45 | | - //assert |
46 | 72 |
|
47 | 73 | }); |
0 commit comments