/
core.spec.js
95 lines (77 loc) · 4.21 KB
/
core.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const assert = require('assert');
const path = require('path');
const core = require('../lib/core');
describe('test [getViewOptions] function ', function() {
it('should return default view options when have no parameters', function() {
const viewOptions = core.getViewOptions();
assert.equal(viewOptions.extension, 'hbs');
assert.equal(viewOptions.map.hbs, 'handlebars');
assert.equal(viewOptions.options.helpers, null);
assert.equal(viewOptions.options.partials, null);
});
it('should return expected view options when pass one folder', function() {
const helperDirs = __dirname + '/fixtures/helpers';
const partialDirs = __dirname + '/fixtures/views';
const viewOptions = core.getViewOptions(__dirname + '/fixtures', {
helperDirs,
partialDirs
});
assert.equal(viewOptions.extension, 'hbs');
assert.equal(viewOptions.map.hbs, 'handlebars');
// helpers
const str = 'hello';
const helpers = viewOptions.options.helpers;
assert.equal(helpers.uppercase(str), str.toUpperCase());
// partials
const partials = viewOptions.options.partials;
assert.equal(partials['includes/header'], getAbsolutePath(partialDirs, 'includes/header', '.hbs'));
assert.equal(partials['includes/footer'], getAbsolutePath(partialDirs, 'includes/footer', '.hbs'));
assert.equal(partials['layout/base'], getAbsolutePath(partialDirs, 'layout/base', '.hbs'));
assert.equal(partials['pages/index'], getAbsolutePath(partialDirs, 'pages/index', '.hbs'));
assert.equal(partials['pages/detail'], getAbsolutePath(partialDirs, 'pages/detail', '.hbs'));
});
it('should return expected view options when pass one folder with releated path', function() {
const viewRootPath = __dirname + '/fixtures';
const helperDirs = './helpers';
const partialDirs = 'views';
const viewOptions = core.getViewOptions(viewRootPath, {
helperDirs,
partialDirs
});
assert.equal(viewOptions.extension, 'hbs');
assert.equal(viewOptions.map.hbs, 'handlebars');
// helpers
const str = 'hello';
const helpers = viewOptions.options.helpers;
assert.equal(helpers.uppercase(str), str.toUpperCase());
// partials
const partials = viewOptions.options.partials;
const abPartialDirs = path.resolve(viewRootPath, partialDirs);
assert.equal(partials['includes/header'], getAbsolutePath(abPartialDirs, 'includes/header', '.hbs'));
assert.equal(partials['includes/footer'], getAbsolutePath(abPartialDirs, 'includes/footer', '.hbs'));
assert.equal(partials['layout/base'], getAbsolutePath(abPartialDirs, 'layout/base', '.hbs'));
assert.equal(partials['pages/index'], getAbsolutePath(abPartialDirs, 'pages/index', '.hbs'));
assert.equal(partials['pages/detail'], getAbsolutePath(abPartialDirs, 'pages/detail', '.hbs'));
});
it('should return expected view options when pass multiple partials folders', function() {
const partialDirs = [__dirname + '/fixtures/views/includes', __dirname + '/fixtures/views/layout', __dirname + '/fixtures/views/pages'];
const viewOptions = core.getViewOptions(__dirname + '/fixtures', {
partialDirs
});
assert.equal(viewOptions.extension, 'hbs');
assert.equal(viewOptions.map.hbs, 'handlebars');
// helpers
assert.equal(viewOptions.options.helpers, null);
// partials
const partials = viewOptions.options.partials;
const viewPath = __dirname + '/fixtures/views';
assert.equal(partials['header'], getAbsolutePath(viewPath, 'includes/header', '.hbs'));
assert.equal(partials['footer'], getAbsolutePath(viewPath, 'includes/footer', '.hbs'));
assert.equal(partials['base'], getAbsolutePath(viewPath, 'layout/base', '.hbs'));
assert.equal(partials['index'], getAbsolutePath(viewPath, 'pages/index', '.hbs'));
assert.equal(partials['detail'], getAbsolutePath(viewPath, 'pages/detail', '.hbs'));
});
});
function getAbsolutePath(rootPath, filePath, ext) {
return path.resolve(rootPath, filePath + ext);
}