-
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtemplate_helpers.test.ts
359 lines (295 loc) · 10.3 KB
/
template_helpers.test.ts
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import {
ABC,
ChordLyricsPair,
CHORUS,
Comment,
Line,
Metadata,
NONE,
Paragraph,
Tag,
templateHelpers,
Ternary,
} from '../src';
import { configure } from '../src/formatter/configuration';
import Font from '../src/chord_sheet/font';
import FontSize from '../src/chord_sheet/font_size';
import { newlinesToBreaks, renderSection } from '../src/template_helpers';
import { createLine, createLiteral, createParagraph } from './utilities';
import { defaultCssClasses } from '../src/formatter/html_formatter';
const {
isChordLyricsPair,
lineHasContents,
isTag,
isComment,
stripHTML,
each,
when,
hasTextContents,
lineClasses,
paragraphClasses,
evaluate,
fontStyleTag,
} = templateHelpers;
describe('template_helpers', () => {
describe('isChordLyricsPair', () => {
it('returns true when the item is a ChordLyricsPair', () => {
expect(isChordLyricsPair(new ChordLyricsPair())).toBe(true);
});
it('returns false for all other items', () => {
expect(isChordLyricsPair(new Comment('test comment'))).toBe(false);
expect(isChordLyricsPair(new Tag('test'))).toBe(false);
expect(isChordLyricsPair(new Ternary({}))).toBe(false);
});
});
describe('lineHasContents', () => {
it('returns true when the line has renderable items', () => {
const line = new Line({
type: NONE,
items: [new ChordLyricsPair('A', 'hello')],
});
expect(lineHasContents(line)).toBe(true);
});
it('returns false when the line has no renderable items', () => {
const line = new Line({
type: NONE,
items: [new Comment('hello')],
});
expect(lineHasContents(line)).toBe(false);
});
it('returns false when the line has no items', () => {
const line = new Line({ type: NONE, items: [] });
expect(lineHasContents(line)).toBe(false);
});
});
describe('isTag', () => {
it('returns true when the item is a ChordLyricsPair', () => {
expect(isTag(new Tag('test'))).toBe(true);
});
it('returns false when the item is not a ChordLyricsPair', () => {
expect(isTag(new ChordLyricsPair())).toBe(false);
expect(isTag(new Comment('test comment'))).toBe(false);
expect(isTag(new Ternary({}))).toBe(false);
});
});
describe('isComment', () => {
it('returns true when a tag is a comment', () => {
expect(isComment(new Tag('comment', 'hello'))).toBe(true);
expect(isComment(new Tag('c', 'hello'))).toBe(true);
});
it('returns false when a tag is not a comment', () => {
expect(isComment(new Tag('start_of_chorus', 'hello'))).toBe(false);
expect(isComment(new Tag('soc', 'hello'))).toBe(false);
});
});
describe('stripHTML', () => {
it('removes all whitespace from a HTML string', () => {
const expandedHTML = `
<span${' '}
class="foo"
> FOO </span>
`;
const strippedHTML = '<span class="foo"> FOO </span>';
expect(stripHTML(expandedHTML)).toEqual(strippedHTML);
});
});
describe('each', () => {
it('invokes the callback which each item and returns a concatenated string', () => {
const callback = (str: string) => str.toUpperCase();
const items = ['foo', 'bar', 'barber', 'barbarian'];
const expectedResult = 'FOOBARBARBERBARBARIAN';
expect(each(items, callback)).toEqual(expectedResult);
});
});
describe('when', () => {
it('returns the callback result when the condition is truthy', () => {
const callback = () => 'foobar';
expect(when(true, callback).toString()).toEqual('foobar');
expect(when('string', callback).toString()).toEqual('foobar');
expect(when({}, callback).toString()).toEqual('foobar');
expect(when(25, callback).toString()).toEqual('foobar');
expect(when([], callback).toString()).toEqual('foobar');
});
it('returns an empty string when the condition is falsy', () => {
const callback = () => 'foobar!';
expect(when(false, callback).toString()).toEqual('');
expect(when(null, callback).toString()).toEqual('');
expect(when(undefined, callback).toString()).toEqual('');
expect(when('', callback).toString()).toEqual('');
expect(when(0, callback).toString()).toEqual('');
});
it('allows chaining with then', () => {
expect(when(true).then(() => 'foobar').toString()).toEqual('foobar');
expect(when(false).then(() => 'foobar').toString()).toEqual('');
});
it('allows chaining with elseWhen', () => {
expect(when(true).then(() => 'when').elseWhen(true, () => 'elseWhen').toString()).toEqual('when');
expect(when(false).then(() => 'when').elseWhen(true, () => 'elseWhen').toString()).toEqual('elseWhen');
expect(when(false).then(() => 'when').elseWhen(false, () => 'elseWhen').toString()).toEqual('');
});
it('allows chaining with elseWhen and else', () => {
expect(
when(false)
.then(() => 'when')
.elseWhen(false, () => 'elseWhen')
.else(() => 'else')
.toString(),
).toEqual('else');
});
it('allows chaining with else then', () => {
expect(
when(false)
.then(() => 'when')
.elseWhen(true)
.then(() => 'elseThen')
.toString(),
).toEqual('elseThen');
});
it('allows chaining with else then else', () => {
expect(
when(false)
.then(() => 'when')
.elseWhen(false)
.then(() => 'elseThen')
.else(() => 'else')
.toString(),
).toEqual('else');
});
it('allows chaining with else', () => {
expect(when(true).then(() => 'then').else(() => 'else').toString()).toEqual('then');
expect(when(false).then(() => 'then').else(() => 'else').toString()).toEqual('else');
});
});
describe('hasTextContents', () => {
it('returns true when an item is a ChordLyricsPair with lyrics', () => {
const line = new Line({
type: NONE,
items: [
new ChordLyricsPair('G', 'hello'),
],
});
expect(hasTextContents(line)).toBe(true);
});
it('returns true when an item is a renderable Tag', () => {
const line = new Line({
type: NONE,
items: [
new Tag('comment', 'hello'),
],
});
expect(hasTextContents(line)).toBe(true);
});
it('returns true when an item is evaluatable', () => {
const line = new Line({
type: NONE,
items: [
new Ternary({ }),
],
});
expect(hasTextContents(line)).toBe(true);
});
it('returns false when an item is an unrenderable Tag', () => {
const line = new Line({
type: NONE,
items: [
new Tag('start_of_chorus', ''),
],
});
expect(hasTextContents(line)).toBe(false);
});
it('returns false when an item is a ChordLyricsPair without lyrics', () => {
const line = new Line({
type: NONE,
items: [
new ChordLyricsPair('G', ''),
],
});
expect(hasTextContents(line)).toBe(false);
});
it('returns false when an item is not evaluatable', () => {
const line = new Line({
type: NONE,
items: [
new Comment('test'),
],
});
expect(hasTextContents(line)).toBe(false);
});
it('returns false when the line does not have any items', () => {
const line = new Line({ type: NONE, items: [] });
expect(hasTextContents(line)).toBe(false);
});
});
describe('lineClasses', () => {
it('returns [row empty-line] when the line has no contents', () => {
const line = new Line({
type: NONE,
items: [],
});
expect(lineClasses(line, defaultCssClasses)).toEqual('row empty-line');
});
it('returns [row] when the line has contents', () => {
const line = new Line({
type: NONE,
items: [new ChordLyricsPair('A', 'hello')],
});
expect(lineClasses(line, defaultCssClasses)).toEqual('row');
});
});
describe('paragraphClasses', () => {
it('returns [paragraph <paragraph type>] when the paragraph has a type', () => {
const line = new Line({ type: CHORUS, items: [] });
const paragraph = new Paragraph();
paragraph.addLine(line);
expect(paragraphClasses(paragraph, defaultCssClasses)).toEqual('paragraph chorus');
});
it('returns [paragraph] when the paragraph type is NONE', () => {
const line = new Line({ type: NONE, items: [] });
const paragraph = new Paragraph();
paragraph.addLine(line);
expect(paragraphClasses(paragraph, defaultCssClasses)).toEqual('paragraph');
});
it('returns [paragraph] when the paragraph type is INDETERMINATE', () => {
const paragraph = new Paragraph();
expect(paragraphClasses(paragraph, defaultCssClasses)).toEqual('paragraph');
});
});
describe('evaluate', () => {
it('evaluates the item', () => {
const item = new Ternary({ variable: 'composer' });
const metadata = new Metadata({ composer: ['John', 'Mary'] });
const configuration = configure({ metadata: { separator: ' and ' } });
expect(evaluate(item, metadata, configuration)).toEqual('John and Mary');
});
});
describe('fontStyleTag', () => {
it('returns a style tag for a font with properties', () => {
const font = new Font({ font: 'Arial', size: new FontSize(15, 'px'), colour: 'black' });
expect(fontStyleTag(font)).toEqual(' style="color: black; font: 15px Arial"');
});
it('returns an empty string for a font without properties', () => {
const font = new Font();
expect(fontStyleTag(font)).toEqual('');
});
});
describe('#newlinesToBreaks', () => {
it('replaces newlines with break tags', () => {
const string = 'hello\nworld\n';
const expectedString = 'hello<br>world<br>';
expect(newlinesToBreaks(string)).toEqual(expectedString);
});
});
describe('#renderSection', () => {
it('returns the result of the delegate function', () => {
const paragraph = createParagraph([
createLine([createLiteral('hello world')], ABC),
]);
const configuration = configure({
delegates: {
abc: (string: string) => string.toUpperCase(),
},
});
expect(renderSection(paragraph, configuration)).toEqual('HELLO WORLD');
});
});
});