|
| 1 | +import { assert, describe, expect, test } from 'vitest'; |
| 2 | + |
| 3 | +import { |
| 4 | + addToRenderMap, |
| 5 | + BaseFragment, |
| 6 | + createRenderMap, |
| 7 | + mapRenderMapContent, |
| 8 | + mapRenderMapContentAsync, |
| 9 | + mergeRenderMaps, |
| 10 | + removeFromRenderMap, |
| 11 | +} from '../src'; |
| 12 | + |
| 13 | +describe('createRenderMap', () => { |
| 14 | + test('it creates an empty render map', () => { |
| 15 | + expect(createRenderMap()).toStrictEqual(new Map()); |
| 16 | + }); |
| 17 | + |
| 18 | + test('it creates a render map from a path and a string', () => { |
| 19 | + expect(createRenderMap('some/path', 'Some content')).toStrictEqual(new Map([['some/path', 'Some content']])); |
| 20 | + }); |
| 21 | + |
| 22 | + test('it creates a render map from a path and a fragment', () => { |
| 23 | + const fragment: BaseFragment = { content: 'Some fragment content' }; |
| 24 | + expect(createRenderMap('some/fragment/path', fragment)).toStrictEqual( |
| 25 | + new Map([['some/fragment/path', 'Some fragment content']]), |
| 26 | + ); |
| 27 | + }); |
| 28 | + |
| 29 | + test('it creates a render map from a record of entries', () => { |
| 30 | + expect( |
| 31 | + createRenderMap({ |
| 32 | + 'some/fragment/path': { content: 'Some fragment content' }, |
| 33 | + 'some/path': 'Some content', |
| 34 | + }), |
| 35 | + ).toStrictEqual( |
| 36 | + new Map([ |
| 37 | + ['some/fragment/path', 'Some fragment content'], |
| 38 | + ['some/path', 'Some content'], |
| 39 | + ]), |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + test('it removes undefined entries from the provided record', () => { |
| 44 | + expect( |
| 45 | + createRenderMap({ |
| 46 | + 'some/path': 'Some content', |
| 47 | + 'some/path/undefined': undefined, |
| 48 | + }), |
| 49 | + ).toStrictEqual(new Map([['some/path', 'Some content']])); |
| 50 | + }); |
| 51 | + |
| 52 | + test('it freezes the returned render map', () => { |
| 53 | + assert.isFrozen(createRenderMap()); |
| 54 | + assert.isFrozen(createRenderMap('some/path', 'Some content')); |
| 55 | + assert.isFrozen(createRenderMap({ 'some/path': 'Some content' })); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('addToRenderMap', () => { |
| 60 | + test('it adds new entries to render map', () => { |
| 61 | + const renderMap = createRenderMap('some/path', 'Some content'); |
| 62 | + expect(addToRenderMap(renderMap, 'some/new/path', 'Some new content')).toStrictEqual( |
| 63 | + new Map([ |
| 64 | + ['some/path', 'Some content'], |
| 65 | + ['some/new/path', 'Some new content'], |
| 66 | + ]), |
| 67 | + ); |
| 68 | + }); |
| 69 | + |
| 70 | + test('it overwrites existing entries in the render map', () => { |
| 71 | + const renderMap = createRenderMap('some/path', 'Some content'); |
| 72 | + expect(addToRenderMap(renderMap, 'some/path', 'Some new content')).toStrictEqual( |
| 73 | + new Map([['some/path', 'Some new content']]), |
| 74 | + ); |
| 75 | + }); |
| 76 | + |
| 77 | + test('it freezes the returned render map', () => { |
| 78 | + assert.isFrozen(addToRenderMap(createRenderMap(), 'some/new/path', 'Some new content')); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe('removeFromRenderMap', () => { |
| 83 | + test('it removes existing entries from a render map', () => { |
| 84 | + const renderMap = createRenderMap({ pathA: 'Content A', pathB: 'Content B' }); |
| 85 | + expect(removeFromRenderMap(renderMap, 'pathA')).toStrictEqual(new Map([['pathB', 'Content B']])); |
| 86 | + }); |
| 87 | + |
| 88 | + test('it can remove the last entry of a render map', () => { |
| 89 | + const renderMap = createRenderMap('pathA', 'Content A'); |
| 90 | + expect(removeFromRenderMap(renderMap, 'pathA')).toStrictEqual(new Map()); |
| 91 | + }); |
| 92 | + |
| 93 | + test('it ignores missing paths', () => { |
| 94 | + const renderMap = createRenderMap(); |
| 95 | + expect(removeFromRenderMap(renderMap, 'missingPaths')).toStrictEqual(new Map()); |
| 96 | + }); |
| 97 | + |
| 98 | + test('it freezes the returned render map', () => { |
| 99 | + assert.isFrozen(removeFromRenderMap(createRenderMap(), 'some/path')); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('mergeRenderMaps', () => { |
| 104 | + test('it returns an empty render map when no maps are provided', () => { |
| 105 | + expect(mergeRenderMaps([])).toStrictEqual(new Map()); |
| 106 | + }); |
| 107 | + |
| 108 | + test('it returns the first render map as-is when only one map is provided', () => { |
| 109 | + const renderMap = createRenderMap('pathA', 'ContentA'); |
| 110 | + expect(mergeRenderMaps([renderMap])).toBe(renderMap); |
| 111 | + }); |
| 112 | + |
| 113 | + test('it merges the entries of two render maps', () => { |
| 114 | + expect( |
| 115 | + mergeRenderMaps([createRenderMap('pathA', 'ContentA'), createRenderMap('pathB', 'ContentB')]), |
| 116 | + ).toStrictEqual( |
| 117 | + new Map([ |
| 118 | + ['pathA', 'ContentA'], |
| 119 | + ['pathB', 'ContentB'], |
| 120 | + ]), |
| 121 | + ); |
| 122 | + }); |
| 123 | + |
| 124 | + test('later entries overwrite earlier entries', () => { |
| 125 | + expect( |
| 126 | + mergeRenderMaps([createRenderMap('samePath', 'Old content'), createRenderMap('samePath', 'New content')]), |
| 127 | + ).toStrictEqual(new Map([['samePath', 'New content']])); |
| 128 | + }); |
| 129 | + |
| 130 | + test('it merges the entries of two render maps', () => { |
| 131 | + expect( |
| 132 | + mergeRenderMaps([createRenderMap('pathA', 'ContentA'), createRenderMap('pathB', 'ContentB')]), |
| 133 | + ).toStrictEqual( |
| 134 | + new Map([ |
| 135 | + ['pathA', 'ContentA'], |
| 136 | + ['pathB', 'ContentB'], |
| 137 | + ]), |
| 138 | + ); |
| 139 | + }); |
| 140 | + |
| 141 | + test('it freezes the returned render map', () => { |
| 142 | + assert.isFrozen(mergeRenderMaps([])); |
| 143 | + assert.isFrozen(mergeRenderMaps([createRenderMap('pathA', 'ContentA')])); |
| 144 | + assert.isFrozen(mergeRenderMaps([createRenderMap('pathA', 'ContentA'), createRenderMap('pathB', 'ContentB')])); |
| 145 | + }); |
| 146 | +}); |
| 147 | + |
| 148 | +describe('mapRenderMapContent', () => { |
| 149 | + test('it maps the content of all entries inside a render map', () => { |
| 150 | + expect( |
| 151 | + mapRenderMapContent( |
| 152 | + createRenderMap({ |
| 153 | + pathA: 'ContentA', |
| 154 | + pathB: 'ContentB', |
| 155 | + }), |
| 156 | + content => `Mapped: ${content}`, |
| 157 | + ), |
| 158 | + ).toStrictEqual( |
| 159 | + new Map([ |
| 160 | + ['pathA', 'Mapped: ContentA'], |
| 161 | + ['pathB', 'Mapped: ContentB'], |
| 162 | + ]), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + test('it freezes the returned render map', () => { |
| 167 | + assert.isFrozen(mapRenderMapContent(createRenderMap(), c => c)); |
| 168 | + }); |
| 169 | +}); |
| 170 | + |
| 171 | +describe('mapRenderMapContentAsync', () => { |
| 172 | + test('it maps the content of all entries inside a render map', async () => { |
| 173 | + expect( |
| 174 | + await mapRenderMapContentAsync( |
| 175 | + createRenderMap({ |
| 176 | + pathA: 'ContentA', |
| 177 | + pathB: 'ContentB', |
| 178 | + }), |
| 179 | + content => Promise.resolve(`Mapped: ${content}`), |
| 180 | + ), |
| 181 | + ).toStrictEqual( |
| 182 | + new Map([ |
| 183 | + ['pathA', 'Mapped: ContentA'], |
| 184 | + ['pathB', 'Mapped: ContentB'], |
| 185 | + ]), |
| 186 | + ); |
| 187 | + }); |
| 188 | + |
| 189 | + test('it freezes the returned render map', async () => { |
| 190 | + assert.isFrozen(await mapRenderMapContentAsync(createRenderMap(), c => Promise.resolve(c))); |
| 191 | + }); |
| 192 | +}); |
0 commit comments