-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.test.js
More file actions
277 lines (232 loc) · 7.37 KB
/
index.test.js
File metadata and controls
277 lines (232 loc) · 7.37 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
'use strict';
const _ = require('lodash'),
filename = __filename.split('/').pop().split('.').shift(),
lib = require('./' + filename),
expect = require('chai').expect,
sinon = require('sinon'),
path = require('path'),
fs = require('fs'),
FAKE_SITE_DIR = path.resolve(__dirname, './test/config/sites/mockSite');
describe(_.startCase(filename), function () {
let sandbox,
logFn;
beforeEach(function () {
sandbox = sinon.sandbox.create();
logFn = sandbox.spy();
lib.setLog(logFn);
});
afterEach(function () {
sandbox.restore();
});
function mockRes(box) {
return {
json: box.spy()
};
}
function mockContent() {
return [
{
_ref: 'some/component/uri1',
role: 'title',
text: 'This is a Story'
},
{
_ref: 'some/unrenderable/component',
text: 'I contribute nothing to the discourse !!'
},
{
_ref: 'some/component/uri3',
role: 'container',
components: [
{
role: 'photo',
URL: 'zoe.com/coolpic.jpg'
},
{
role: 'caption',
text: 'Photo: zoe\'s photo studio'
},
{
_ref: 'some/unrenderable/component',
text: 'I should not be rendered for apple news!'
},
{
role: 'section',
components: [
{
_ref: 'useless/thing'
},
{
_ref: 'useable/thing',
role: 'body',
text: 'hey there!'
}
]
}
]
}
];
}
function mockCmpt() {
return {
_ref: 'some/component/uri3',
role: 'container',
components: [
{
role: 'photo',
URL: 'zoe.com/coolpic.jpg'
},
{
_ref: 'component/ref',
role: 'caption',
text: 'Photo: zoe\'s photo studio'
},
{
_ref: 'some/unrenderable/component',
text: 'I should not be rendered for apple news!'
},
{
role: 'section',
components: [
{
_ref: 'useless/thing'
},
{
_ref: 'useable/thing',
role: 'body',
text: 'hey there!'
}
]
}
]
};
}
function getArgs(spy) {
return spy.getCall(0).args;
}
describe('getSiteConfig', function () {
const data = { site: { dir: FAKE_SITE_DIR, slug: 'verygoodsite' } },
fn = lib[this.title];
it('returns the site config yml as a js Object', function () {
expect(fn(data.site)).to.eql({ language: 'en', version: '1.5', identifier: 'askdfghergmdslfajf' });
});
it('throws an error if site path does not have an anf.yml file', function () {
expect(() => {
fn({dir: 'the/wrong/path'});
}).to.throw();
sinon.assert.calledOnce(logFn);
sinon.assert.calledWith(logFn, 'error');
});
});
describe('sanitizeComponent', function () {
const data = mockCmpt(),
fn = lib[this.title];
it('returns a component without the _ref property', function () {
expect(fn(data)).to.not.have.own.property('_ref');
});
it('correctly sanitizes components with nested component lists', function () {
const sanitized = {
role: 'container',
components: [
{
role: 'photo',
URL: 'zoe.com/coolpic.jpg'
},
{
role: 'caption',
text: 'Photo: zoe\'s photo studio'
},
{
role: 'section',
components: [
{
role: 'body',
text: 'hey there!'
}
]
}
]
};
expect(fn(data)).to.eql(sanitized);
});
});
describe('render', function () {
const fn = lib[this.title],
site = { dir: FAKE_SITE_DIR },
meta = { locals: { site } },
metaWithQuery = { locals: { site, query: { config: true } } };
it('responds with json', function () {
const res = mockRes(sandbox);
fn(mockContent(), meta, res);
sinon.assert.calledOnce(res.json);
});
it('returns an article without a content array', function () {
const res = mockRes(sandbox);
fn({ content: [], title: 'Wow cool' }, meta, res);
sinon.assert.calledWith(res.json, { components: [], title: 'Wow cool' });
});
it('returns an article with the properties from the site config if the request has the "config" query param', function () {
const res = mockRes(sandbox);
fn({ content: mockContent() }, metaWithQuery, res);
expect(getArgs(res.json)[0]).to.have.own.property('language');
});
it('returns an article with the site slug if the request has the "config" query param', function () {
const res = mockRes(sandbox);
fn({ content: mockContent() }, metaWithQuery, res);
expect(getArgs(res.json)[0]).to.have.own.property('siteSlug');
});
it('includes a component if it has a "role" property', function () {
const res = mockRes(sandbox);
fn({ content: mockContent() }, meta, res);
expect(getArgs(res.json)[0].components[0]).to.eql({
role: 'title',
text: 'This is a Story'
});
});
it('does not include components without a "role" property', function () {
const res = mockRes(sandbox);
fn({ content: mockContent() }, meta, res);
expect(_.filter(getArgs(res.json)[0].components, (cmpt) => !cmpt.role)).to.be.empty;
sinon.assert.calledThrice(logFn);
});
it('removes the "_ref" property from all included ANF components', function () {
const res = mockRes(sandbox);
fn({ content: mockContent() }, meta, res);
expect(_.filter(getArgs(res.json)[0].components, (cmpt) => !!cmpt._ref)).to.be.empty;
});
it('does not render the site config if the request does not have the "config" query param', function () {
const res = mockRes(sandbox);
fn(mockCmpt(), meta, res);
expect(getArgs(res.json)[0]).to.not.have.own.property('language');
});
it('replaces the site dir if the request has a "config" and "siteOverride" query param', function () {
const res = mockRes(sandbox);
metaWithQuery.locals.query.siteOverride = 'foo';
metaWithQuery.locals.site.slug = 'bar';
fn(mockCmpt(), metaWithQuery, res);
expect(getArgs(res.json)[0]).to.have.own.property('siteSlug');
});
});
describe('getSitePathBySlug', function () {
const fn = lib[this.title];
beforeEach(function () {
sandbox.stub(fs, 'existsSync');
sandbox.stub(path, 'resolve');
fs.existsSync.returns(false);
fs.existsSync.withArgs('sites/foo').returns(true);
fs.existsSync.withArgs('sites/bar').returns(false);
path.resolve.withArgs(process.cwd(), 'sites', 'foo').returns('sites/foo');
path.resolve.withArgs(process.cwd(), 'sites', 'bar').returns('sites/bar');
});
it('returns an internal path of the site', function () {
const expected = 'sites/foo';
expect(fn('foo')).to.equal(expected);
});
it('returns an empty string if no slug is passed down', function () {
expect(fn()).to.equal('');
});
it('returns an empty string if the directory does not exists', function () {
expect(fn('bar')).to.equal('');
});
});
});