|
| 1 | +const appConfigStub = { |
| 2 | + commonDataTableOptions: { |
| 3 | + pageLength: { |
| 4 | + XS: 5, |
| 5 | + M: 25, |
| 6 | + }, |
| 7 | + lengthMenu: { |
| 8 | + XS: [[5, 10], ['5', '10']], |
| 9 | + M: [[10, 25, 50], ['10', '25', '50']], |
| 10 | + }, |
| 11 | + }, |
| 12 | +}; |
| 13 | + |
| 14 | +let commonUtils; |
| 15 | +let ko; |
| 16 | +let appConfig; |
| 17 | + |
| 18 | +beforeAll(() => { |
| 19 | + if (!requirejsInstance.defined('atlas-state')) { |
| 20 | + requirejsInstance.define('atlas-state', [], () => ({ |
| 21 | + selectedConceptsIndex: {}, |
| 22 | + localeSettings: {}, |
| 23 | + })); |
| 24 | + } |
| 25 | + if (!requirejsInstance.defined('appConfig')) { |
| 26 | + requirejsInstance.define('appConfig', [], () => appConfigStub); |
| 27 | + } |
| 28 | + if (!requirejsInstance.defined('pages/Page')) { |
| 29 | + requirejsInstance.define('pages/Page', [], () => class MockPage {}); |
| 30 | + } |
| 31 | + if (!requirejsInstance.defined('services/MomentAPI')) { |
| 32 | + requirejsInstance.define('services/MomentAPI', [], () => ({ |
| 33 | + DESIGN_DATE_TIME_FORMAT: 'YYYY-MM-DD H:mm', |
| 34 | + formatDateTimeWithFormat: (value, format) => `${value}:${format}`, |
| 35 | + })); |
| 36 | + } |
| 37 | + if (!requirejsInstance.defined('const')) { |
| 38 | + requirejsInstance.define('const', [], () => ({ |
| 39 | + maxEntityNameLength: 255, |
| 40 | + })); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +beforeAll(async () => { |
| 45 | + [commonUtils, ko, appConfig] = await requireAmd(['utils/CommonUtils', 'knockout', 'appConfig']); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('CommonUtils', () => { |
| 49 | + test('normalizeUrl combines segments without duplicate slashes', () => { |
| 50 | + expect(commonUtils.normalizeUrl('/path/', '/to/', 'resource')).toBe('/path/to/resource'); |
| 51 | + }); |
| 52 | + |
| 53 | + test('cartesian produces all permutations across multiple arrays', () => { |
| 54 | + const result = commonUtils.cartesian([1, 2], ['a'], ['x', 'y']); |
| 55 | + expect(result).toEqual([ |
| 56 | + [1, 'a', 'x'], |
| 57 | + [1, 'a', 'y'], |
| 58 | + [2, 'a', 'x'], |
| 59 | + [2, 'a', 'y'], |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + test('escapeTooltip escapes both single and double quotes', () => { |
| 64 | + const escaped = commonUtils.escapeTooltip(`Bob's "quote"`); |
| 65 | + expect(escaped).toBe(`Bob\\'s "quote"`); |
| 66 | + }); |
| 67 | + |
| 68 | + test('getSelectedConcepts returns copies of selected items without selection flag', () => { |
| 69 | + const concepts = ko.observableArray([ |
| 70 | + { id: 1, name: 'A', isSelected: () => true }, |
| 71 | + { id: 2, name: 'B', isSelected: () => false }, |
| 72 | + ]); |
| 73 | + |
| 74 | + const result = commonUtils.getSelectedConcepts(concepts); |
| 75 | + expect(result).toEqual([{ id: 1, name: 'A' }]); |
| 76 | + expect(result[0]).not.toHaveProperty('isSelected'); |
| 77 | + }); |
| 78 | + |
| 79 | + test('clearConceptsSelectionState resets observable selection flags', () => { |
| 80 | + const first = { id: 1, isSelected: ko.observable(true) }; |
| 81 | + const second = { id: 2, isSelected: ko.observable(false) }; |
| 82 | + const conceptList = ko.observableArray([first, second]); |
| 83 | + |
| 84 | + commonUtils.clearConceptsSelectionState(conceptList); |
| 85 | + |
| 86 | + expect(first.isSelected()).toBe(false); |
| 87 | + expect(second.isSelected()).toBe(false); |
| 88 | + }); |
| 89 | + |
| 90 | + test('buildConceptSetItems merges shared options into each entry', () => { |
| 91 | + const options = ko.observable({ |
| 92 | + includeDescendants: true, |
| 93 | + isExcluded: false, |
| 94 | + }); |
| 95 | + const concepts = [ |
| 96 | + { conceptId: 1 }, |
| 97 | + { conceptId: 2 }, |
| 98 | + ]; |
| 99 | + |
| 100 | + const items = commonUtils.buildConceptSetItems(concepts, options); |
| 101 | + |
| 102 | + expect(items).toEqual([ |
| 103 | + { concept: concepts[0], includeDescendants: true, isExcluded: false }, |
| 104 | + { concept: concepts[1], includeDescendants: true, isExcluded: false }, |
| 105 | + ]); |
| 106 | + }); |
| 107 | + |
| 108 | + test('getTableOptions returns variant specific datatable preferences', () => { |
| 109 | + expect(commonUtils.getTableOptions('XS')).toEqual({ |
| 110 | + pageLength: appConfig.commonDataTableOptions.pageLength.XS, |
| 111 | + lengthMenu: appConfig.commonDataTableOptions.lengthMenu.XS, |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments