-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils-overrides.test.js
458 lines (410 loc) · 19.8 KB
/
utils-overrides.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
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import { formDiffingTool, navFormUtils } from '@navikt/skjemadigitalisering-shared-domain';
import moment from 'moment/moment';
import panelDiffDeletedDatagrid from '../../../../test/test-data/diff/diff-deleted-datagrid';
import panelDiffDeletedRadiopanel from '../../../../test/test-data/diff/diff-deleted-radio';
import formNavSelectChanges from '../../../../test/test-data/form/form-navSelect-changes';
import publishedForm from '../../../../test/test-data/form/published-form';
import UtilsOverrides from './utils-overrides';
describe('utils-overrides', () => {
describe('sanitizeJavaScriptCode', () => {
it('does not change a string without chained lookups', () => {
const aSentence = 'this is a string.';
expect(UtilsOverrides.sanitizeJavaScriptCode(aSentence)).toEqual(aSentence);
const aRealisticInputWithoutChainedLookups = "show = a === 'b'";
expect(UtilsOverrides.sanitizeJavaScriptCode(aRealisticInputWithoutChainedLookups)).toEqual(
aRealisticInputWithoutChainedLookups,
);
});
it('correctly adds null/undefined checks for chained lookups', () => {
const inputWithChainedLookups = "show = a.b === 'c'";
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithChainedLookups)).toBe("show = (a && a.b) === 'c'");
});
it('correctly adds null/undefined checks for multiple chained lookups', () => {
const inputWithMultipleChainedLookups = "show = a.b === 'c' || d.e === 'f'";
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithMultipleChainedLookups)).toBe(
"show = (a && a.b) === 'c' || (d && d.e) === 'f'",
);
});
it('correctly adds null/undefined checks for deeply chained lookups', () => {
const inputWithDeeplyChainedLookups = "show = a.b.c.d.e === 'f'";
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithDeeplyChainedLookups)).toBe(
"show = (a && a.b && a.b.c && a.b.c.d && a.b.c.d.e) === 'f'",
);
});
it('correctly add null/undefined checks for multiple equal chained lookups', () => {
const inputWithMultipleEqualChainedLookups = "show = a.b === 'c' || a.b === 'd'";
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithMultipleEqualChainedLookups)).toBe(
"show = (a && a.b) === 'c' || (a && a.b) === 'd'",
);
});
it('correctly add null/undefined checks when variable names includes numbers', () => {
const inputWithMultipleEqualChainedLookups = "show = a1.b2 === 'c'";
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithMultipleEqualChainedLookups)).toBe(
"show = (a1 && a1.b2) === 'c'",
);
});
it('will not change a partial expression', () => {
const inputWithTwoChainedWhereOneIsAPartialOfTheOther =
"show = anObject.aString === 'c' || anObject.aString1 === 'd'";
const actual = UtilsOverrides.sanitizeJavaScriptCode(inputWithTwoChainedWhereOneIsAPartialOfTheOther);
expect(actual).toBe("show = (anObject && anObject.aString) === 'c' || (anObject && anObject.aString1) === 'd'");
});
it('will not change a partial expression that ends in an equal expression to another complete expression', () => {
const inputWithOneChainedExpressionEndingInAnotherExpression =
"show = anObject.aString === 'c' || Object.aString === 'd'";
const actual = UtilsOverrides.sanitizeJavaScriptCode(inputWithOneChainedExpressionEndingInAnotherExpression);
expect(actual).toBe("show = (anObject && anObject.aString) === 'c' || (Object && Object.aString) === 'd'");
});
describe('When the code includes function calls', () => {
it('does not add null checks for functions on instance', () => {
const inputWithInstanceFunctionCall = 'valid = instance.validate(input)';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithInstanceFunctionCall)).toBe(
'valid = instance.validate(input)',
);
});
it('does not add null checks for functions on util', () => {
const inputWithUtilFunctionCall = 'valid = util.fun(input)';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithUtilFunctionCall)).toBe('valid = util.fun(input)');
});
it('does not add null checks for functions on utils', () => {
const inputWithUtilsFunctionCall = 'valid = utils.fun(input)';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithUtilsFunctionCall)).toBe('valid = utils.fun(input)');
});
it('does not add null checks for functions on lodash', () => {
const inputWithLodashFunctionCall = 'valid = _.fun(input)';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithLodashFunctionCall)).toBe('valid = _.fun(input)');
});
it('does not add null checks for nested functions on a reserved word', () => {
const inputWithNestedFunctionCalls = 'valid = instance.fun1(_.some(data, (a) => util.fun2(a.b.c)))';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithNestedFunctionCalls)).toBe(
'valid = instance.fun1(_.some(data, (a) => util.fun2((a && a.b && a.b.c))))',
);
});
it('does not add null checks for function calls', () => {
const inputWithFunctionCall = 'valid = obj.myFunction()';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithFunctionCall)).toBe('valid = obj.myFunction()');
const inputWithFunctionThatTakesParams = 'valid = obj.myFunction(someInput)';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithFunctionThatTakesParams)).toBe(
'valid = obj.myFunction(someInput)',
);
const inputWithNestedFunctionCall = 'valid = parentObject.childObject.nestedFunction()';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithNestedFunctionCall)).toBe(
'valid = parentObject.childObject.nestedFunction()',
);
const inputWithNestedObjectReferenceAndFunctionCall = 'valid = obj.someVar || obj.someFunction()';
expect(UtilsOverrides.sanitizeJavaScriptCode(inputWithNestedObjectReferenceAndFunctionCall)).toBe(
'valid = (obj && obj.someVar) || obj.someFunction()',
);
});
});
describe('composite component key inside expression', () => {
it('is not null checked when wrapped in single qoutes', () => {
const code = "show = utils.isBornBeforeYear(1964, 'container.fnr', submission);";
expect(UtilsOverrides.sanitizeJavaScriptCode(code)).toBe(code);
});
it('is not null checked when wrapped in double qoutes', () => {
const code = 'show = utils.isBornBeforeYear(1964, "container.fnr", submission);';
expect(UtilsOverrides.sanitizeJavaScriptCode(code)).toBe(code);
});
});
});
describe('navFormDiffToHtml', () => {
it('generates html list with changed properties', () => {
const changes = {
status: 'Endring',
diff: {
label: { status: 'Endring', originalValue: 'Fornavn', value: 'Oppgi fornavn' },
id: { status: 'Endring', originalValue: 'ehnemzu', value: 'e6s77h' },
},
key: 'fornavn',
type: 'textfield',
};
const diffSummary = formDiffingTool.createDiffSummary(changes);
const html = UtilsOverrides.navFormDiffToHtml(diffSummary);
expect(html).toMatchSnapshot();
});
it('ignores props whos value changes from undefined to falsy', () => {
const changes = {
status: 'Endring',
diff: {
label: {
status: 'Endring',
originalValue: 'WIP',
value: 'Svar ja eller nei?',
},
validate: {
status: 'Endring',
diff: {
customMessage: {
status: 'Ny',
value: '',
},
json: {
status: 'Ny',
value: '',
},
},
},
defaultValue: {
status: 'Ny',
value: '',
},
additionalDescription: {
status: 'Ny',
value: false,
},
},
key: 'svarJaEllerNei',
type: 'radiopanel',
};
const diffSummary = formDiffingTool.createDiffSummary(changes);
const html = UtilsOverrides.navFormDiffToHtml(diffSummary);
expect(html).toMatchSnapshot();
});
it('generates html list with deleted radiopanel from panel', () => {
const diffSummary = formDiffingTool.createDiffSummary(panelDiffDeletedRadiopanel);
const html = UtilsOverrides.navFormDiffToHtml(diffSummary);
expect(html).toMatchSnapshot();
});
it('generates nested html list with deleted datagrid and its components', () => {
const diffSummary = formDiffingTool.createDiffSummary(panelDiffDeletedDatagrid);
const html = UtilsOverrides.navFormDiffToHtml(diffSummary);
expect(html).toMatchSnapshot();
});
it('handles failure when diffSummary is of unexpected type', () => {
const originalConsoleError = console.error;
console.error = () => {};
try {
const diffSummary = { some: 'thing', is: 'wrong', with: ['this', 'obj', 'ect'] };
const html = UtilsOverrides.navFormDiffToHtml(diffSummary);
expect(html).toMatchSnapshot();
} catch (e) {
throw new Error(`Should never fail, but it did: ${e.message}`);
} finally {
console.error = originalConsoleError;
}
});
describe('Form -> diff -> html', () => {
it('empty html when navSelect is not changed', () => {
const navSelect = navFormUtils.findByNavId('e0a8kbj', publishedForm.components);
const componentDiff = formDiffingTool.getComponentDiff(navSelect, publishedForm);
const html = UtilsOverrides.navFormDiffToHtml(componentDiff);
expect(html).toBe('');
expect(html).toMatchSnapshot();
});
it('should list changes for navSelect', () => {
const navSelect = navFormUtils.findByNavId('e0a8kbj', formNavSelectChanges.components);
const componentDiff = formDiffingTool.getComponentDiff(navSelect, publishedForm);
const html = UtilsOverrides.navFormDiffToHtml(componentDiff);
expect(html).toMatchSnapshot();
});
it('should list changed key', () => {
const radiopanel = navFormUtils.findByKey('doYouLiveInNorway', formNavSelectChanges.components);
const componentDiff = formDiffingTool.getComponentDiff(radiopanel, publishedForm);
const html = UtilsOverrides.navFormDiffToHtml(componentDiff);
expect(html).toMatchSnapshot();
});
it('should list change in conditional', () => {
const alertstripe = navFormUtils.findByKey('alertstripeArstid', formNavSelectChanges.components);
const componentDiff = formDiffingTool.getComponentDiff(alertstripe, publishedForm);
const html = UtilsOverrides.navFormDiffToHtml(componentDiff);
expect(html).toMatchSnapshot();
});
});
});
describe('getDiffTag', () => {
it('returns no tag when component is unchanged', () => {
const navSelect = navFormUtils.findByNavId('e0a8kbj', publishedForm.components);
const ctx = {
builder: true,
component: navSelect,
config: {
publishedForm,
},
self: {
mergeSchema: (comp) => comp,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
it("returns tag 'Endring' when component is changed", () => {
const navSelect = navFormUtils.findByNavId('e0a8kbj', formNavSelectChanges.components);
const ctx = {
builder: true,
component: navSelect,
config: {
publishedForm,
},
self: {
mergeSchema: (comp) => comp,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
it('returns no tag even if component is changed when it renders outside builder', () => {
const navSelect = navFormUtils.findByNavId('e0a8kbj', formNavSelectChanges.components);
const ctx = {
builder: false,
component: navSelect,
config: {
publishedForm,
},
self: {
mergeSchema: (comp) => comp,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
it("returns tag 'Ny' when component does not exist in published form", () => {
const component = { navId: '123456', key: 'aaaa' };
const ctx = {
builder: true,
component,
config: {
publishedForm,
},
self: {
mergeSchema: (comp) => comp,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
describe('component has new default prop in schema', () => {
const MERGE_SCHEMA_NEW_PROP = (comp) => ({ ...comp, newValue2: true });
it('returns no tag if component has not changed, only default schema', () => {
const navSelect = MERGE_SCHEMA_NEW_PROP(navFormUtils.findByNavId('e0a8kbj', publishedForm.components));
const ctx = {
builder: true,
component: navSelect,
config: {
publishedForm,
},
self: {
mergeSchema: MERGE_SCHEMA_NEW_PROP,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
it('returns tag "NY" if component does not exist in published form', () => {
const newComponent = MERGE_SCHEMA_NEW_PROP({
navId: '123457',
key: 'bbbb',
type: 'textfield',
label: 'Nytt tekstfelt',
});
const ctx = {
builder: true,
component: newComponent,
config: {
publishedForm,
},
self: {
mergeSchema: MERGE_SCHEMA_NEW_PROP,
},
};
const html = UtilsOverrides.getDiffTag(ctx);
expect(html).toMatchSnapshot();
});
});
});
describe('isBornBeforeYear', () => {
it('handles undefined and empty input', () => {
expect(UtilsOverrides.isBornBeforeYear(1964, 'fnr', undefined)).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1964, 'fnr', { data: undefined })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1964, 'fnr', { data: {} })).toBe(false);
});
it('handles invalid fnr', () => {
expect(UtilsOverrides.isBornBeforeYear(1964, 'fnr', { data: { fnr: '11013912345' } })).toBe(false);
});
describe('birth date 18.09.1922', () => {
const FNR = '18092200163';
it.each([`${FNR}`, ` ${FNR}`, `${FNR} `])('submission value "%s" is correctly evaluated', (FNR) => {
expect(UtilsOverrides.isBornBeforeYear(1921, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1922, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1923, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(1924, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2021, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2022, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2023, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2024, 'fnr', { data: { fnr: FNR } })).toBe(true);
});
});
it('birth date 01.05.1956', () => {
const FNR = '01055600046';
expect(UtilsOverrides.isBornBeforeYear(1955, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1956, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1957, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2054, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2055, 'fnr', { data: { fnr: FNR } })).toBe(true);
expect(UtilsOverrides.isBornBeforeYear(2056, 'fnr', { data: { fnr: FNR } })).toBe(true);
});
it('birth date 13.04.2039', () => {
const FNR = '13043950287';
expect(UtilsOverrides.isBornBeforeYear(1938, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1939, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1940, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1941, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1964, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(2038, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(2039, 'fnr', { data: { fnr: FNR } })).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(2040, 'fnr', { data: { fnr: FNR } })).toBe(true);
});
it('handles composite key', () => {
const FNR = '01055600046';
const submission = { data: { fornavn: '', container: { fodselsnummer: FNR } } };
expect(UtilsOverrides.isBornBeforeYear(1955, 'container.fodselsnummer', submission)).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1956, 'container.fodselsnummer', submission)).toBe(false);
expect(UtilsOverrides.isBornBeforeYear(1957, 'container.fodselsnummer', submission)).toBe(true);
});
});
const pointInTime = (ddmmyyyy) => {
return moment(ddmmyyyy, 'DD.MM.YYYY');
};
describe('getAge', () => {
it('handles undefined and empty submission', () => {
expect(UtilsOverrides.getAge('fnr', undefined, pointInTime('15.10.2024'))).toBeUndefined();
expect(UtilsOverrides.getAge('fnr', {}, pointInTime('15.10.2024'))).toBeUndefined();
expect(UtilsOverrides.getAge('fnr', { data: undefined }, pointInTime('15.10.2024'))).toBeUndefined();
expect(UtilsOverrides.getAge('fnr', { data: {} }, pointInTime('15.10.2024'))).toBeUndefined();
expect(UtilsOverrides.getAge('fnr', { data: { fnr: undefined } }, pointInTime('15.10.2024'))).toBeUndefined();
});
it('handles invalid fnr', () => {
const FNR_INVALID = '01055612345';
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR_INVALID } }, pointInTime('01.05.1957'))).toBeUndefined();
});
it('returns correct age', () => {
const FNR = '01055631685';
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR } }, pointInTime('30.04.1957'))).toBe(0);
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR } }, pointInTime('01.05.1957'))).toBe(1);
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR } }, pointInTime('15.10.2024'))).toBe(68);
});
it('handles future fnr', () => {
const FNR = '06073350288';
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR } }, pointInTime('15.10.2024'))).toBe(-8);
expect(UtilsOverrides.getAge('fnr', { data: { fnr: FNR } }, pointInTime('06.07.2033'))).toBe(0);
});
});
describe('isAgeBetween', () => {
it('handles undefined and empty input', () => {
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', undefined)).toBe(false);
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', {})).toBe(false);
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', { data: undefined })).toBe(false);
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', { data: {} })).toBe(false);
});
it('checks if age is inside interval', () => {
const FNR = '01055631685';
const submission = { data: { fnr: FNR } };
// lower boundary
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', submission, pointInTime('30.04.1974'))).toBe(false);
expect(UtilsOverrides.isAgeBetween([18, 64], 'fnr', submission, pointInTime('01.05.1974'))).toBe(true);
// upper boundary
expect(UtilsOverrides.isAgeBetween([18, 58], 'fnr', submission, pointInTime('30.04.2015'))).toBe(true);
expect(UtilsOverrides.isAgeBetween([18, 58], 'fnr', submission, pointInTime('01.05.2015'))).toBe(false);
});
});
});