-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
208 lines (157 loc) · 5.34 KB
/
index.test.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const _ = require('lodash'),
filename = __filename.split('/').pop().split('.').shift(),
expect = require('chai').expect,
sinon = require('sinon'),
fs = require('fs'),
path = require('path'),
yaml = require('js-yaml'),
glob = require('glob'),
lib = require('./' + filename);
describe(_.startCase(filename), function () {
let req, sandbox;
function createMockStat(options) {
return {
isDirectory: _.constant(!!options.isDirectory)
};
}
beforeEach(function () {
sandbox = sinon.sandbox.create();
sandbox.stub(fs, 'statSync');
sandbox.stub(fs, 'readdirSync');
sandbox.stub(fs, 'existsSync');
sandbox.stub(fs, 'readFileSync');
sandbox.stub(path, 'resolve');
sandbox.stub(yaml);
sandbox.stub(glob, 'sync');
// clear the caches
lib.getYaml.cache = new _.memoize.Cache();
lib.getFiles.cache = new _.memoize.Cache();
lib.getFolders.cache = new _.memoize.Cache();
lib.isDirectory.cache = new _.memoize.Cache();
lib.fileExists.cache = new _.memoize.Cache();
lib.tryRequire.cache = new _.memoize.Cache();
lib.readFilePromise.cache = new _.memoize.Cache();
// require shouldn't be called dynamically, but here we go
req = sandbox.stub();
req.resolve = sandbox.stub();
lib.setRequire(req);
});
afterEach(function () {
sandbox.restore();
lib.setRequire(require);
});
describe('fileExists', function () {
const fn = lib[this.title];
it('is true if exists', function () {
fs.statSync.returns({});
expect(fn('a')).to.equal(true);
});
it('is false if falsy', function () {
expect(fn('a')).to.equal(false);
});
it('is false if throws', function () {
fs.statSync.throws();
expect(fn('a')).to.equal(false);
});
});
describe('getFiles', function () {
const fn = lib[this.title];
it('gets a list of folders', function () {
fs.readdirSync.returns(['isAFile', 'isADirectory']);
fs.statSync.withArgs('isAFile').returns(createMockStat({isDirectory: false}));
fs.statSync.withArgs('isADirectory').returns(createMockStat({isDirectory: true}));
path.resolve.returnsArg(0);
expect(fn('.')).to.contain('isAFile');
});
it('returns empty array on error', function () {
fs.readdirSync.throws();
expect(fn('.')).to.deep.equal([]);
});
});
describe('getFolders', function () {
const fn = lib[this.title];
it('gets a list of folders', function () {
fs.readdirSync.returns(['isAFolder', 'isNotAFolder']);
fs.statSync.withArgs('isAFolder').returns(createMockStat({isDirectory: true}));
fs.statSync.withArgs('isNotAFolder').returns(createMockStat({isDirectory: false}));
path.resolve.returnsArg(0);
expect(fn('.')).to.contain('isAFolder');
});
it('returns false if no directories', function () {
fs.readdirSync.returns(undefined);
expect(fn('.')).to.deep.equal([]);
});
});
describe('getYaml', function () {
let fn = lib[this.title];
it('returns result', function () {
const filename = 'some-name',
result = 'some result';
yaml.safeLoad.returns(result);
expect(fn(filename)).to.equal(result);
});
it('returns result from first file', function () {
const filename = 'some-name',
result = 'some result';
fs.readFileSync.onCall(0).returns(result);
fs.readFileSync.onCall(1).throws();
yaml.safeLoad.returnsArg(0);
expect(fn(filename)).to.equal(result);
});
it('returns result from second file', function () {
const filename = 'some-name',
result = 'some result';
fs.readFileSync.onCall(0).throws();
fs.readFileSync.onCall(1).returns(result);
yaml.safeLoad.returnsArg(0);
expect(fn(filename)).to.equal(result);
});
});
describe('readFilePromise', function () {
const fn = lib[this.title];
it('returns file contents', function () {
const result = 'result',
name = 'article.css';
sandbox.stub(fs, 'readFile', function (path, options, callback) {
return callback(null, result);
});
fn(name).then(function (fileResult) {
expect(fileResult).to.equal(result);
});
});
it('throws error', function () {
sandbox.stub(fs, 'readFile', function (path, x, callback) {
return callback(new Error(), '');
});
fn('.').catch(function (result) {
expect(result).to.equal('');
});
});
});
describe('isDirectory', function () {
const fn = lib[this.title];
it('returns false if not a directory', function () {
fs.statSync.withArgs('isNotAFolder').returns(createMockStat({isDirectory: false}));
expect(fn()).to.be.false;
});
});
describe('tryRequire', function () {
const fn = lib[this.title];
it('returns a module if one exists', function () {
req.resolve.withArgs('.').returns('/path/to/file');
req.returns({});
expect(fn('.')).to.deep.equal({});
});
it('returns undefined in no file exists', function () {
req.resolve.withArgs('.').returns(undefined);
expect(fn('.')).to.be.undefined;
});
});
describe('tryRequireEach', function () {
const fn = lib[this.title];
it('iterates through filepaths and tries to require them', function () {
expect(fn(['path/to/file'])).to.be.undefined;
});
});
});