|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | + |
| 3 | +import { createHelpersNamespaceObject } from '../src/helpers.js' |
| 4 | + |
| 5 | +describe('Helpers utilities', () => { |
| 6 | + describe('createHelpersNamespaceObject', () => { |
| 7 | + it('should create namespace object with basic helpers', () => { |
| 8 | + const helpers = { |
| 9 | + helper1: { prop1: 'value1', prop2: 'value2' }, |
| 10 | + helper2: { prop1: 'value3', prop3: 'value4' }, |
| 11 | + } |
| 12 | + const result = createHelpersNamespaceObject(helpers) |
| 13 | + |
| 14 | + expect(result['prop1']).toEqual({ |
| 15 | + helper1: 'value1', |
| 16 | + helper2: 'value3', |
| 17 | + }) |
| 18 | + expect(result['prop2']).toEqual({ helper1: 'value2' }) |
| 19 | + expect(result['prop3']).toEqual({ helper2: 'value4' }) |
| 20 | + }) |
| 21 | + |
| 22 | + it('should handle empty helpers object', () => { |
| 23 | + const helpers = {} |
| 24 | + const result = createHelpersNamespaceObject(helpers) |
| 25 | + expect(result).toEqual({}) |
| 26 | + }) |
| 27 | + |
| 28 | + it('should use defaults when property not found', () => { |
| 29 | + const helpers = { |
| 30 | + helper1: { prop1: 'value1' }, |
| 31 | + helper2: { prop2: 'value2' }, |
| 32 | + } |
| 33 | + const defaults = { helper1: 'default1', helper2: 'default2' } |
| 34 | + const result = createHelpersNamespaceObject(helpers, defaults) |
| 35 | + |
| 36 | + expect(result['prop1']).toEqual({ |
| 37 | + helper1: 'value1', |
| 38 | + helper2: 'default2', |
| 39 | + }) |
| 40 | + expect(result['prop2']).toEqual({ |
| 41 | + helper1: 'default1', |
| 42 | + helper2: 'value2', |
| 43 | + }) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should skip undefined defaults', () => { |
| 47 | + const helpers = { |
| 48 | + helper1: { prop1: 'value1' }, |
| 49 | + } |
| 50 | + const defaults = { helper1: undefined } |
| 51 | + const result = createHelpersNamespaceObject(helpers, defaults) |
| 52 | + |
| 53 | + expect(result['prop1']).toEqual({ helper1: 'value1' }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('should sort property names alphabetically by default', () => { |
| 57 | + const helpers = { |
| 58 | + helper1: { zProp: 'z', aProp: 'a', mProp: 'm' }, |
| 59 | + } |
| 60 | + const result = createHelpersNamespaceObject(helpers) |
| 61 | + |
| 62 | + const keys = Object.keys(result) |
| 63 | + expect(keys).toEqual(['aProp', 'mProp', 'zProp']) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should use custom comparator for sorting', () => { |
| 67 | + const helpers = { |
| 68 | + helper1: { prop1: 'value1', prop2: 'value2', prop3: 'value3' }, |
| 69 | + } |
| 70 | + // Reverse alphabetical order |
| 71 | + const comparator = (a: string, b: string) => b.localeCompare(a) |
| 72 | + const result = createHelpersNamespaceObject(helpers, { comparator }) |
| 73 | + |
| 74 | + const keys = Object.keys(result) |
| 75 | + expect(keys).toEqual(['prop3', 'prop2', 'prop1']) |
| 76 | + }) |
| 77 | + |
| 78 | + it('should handle helpers with null prototype', () => { |
| 79 | + const helper1 = Object.create(null) |
| 80 | + helper1.prop1 = 'value1' |
| 81 | + |
| 82 | + const helpers = { helper1 } |
| 83 | + const result = createHelpersNamespaceObject(helpers) |
| 84 | + |
| 85 | + expect(result['prop1']).toEqual({ helper1: 'value1' }) |
| 86 | + }) |
| 87 | + |
| 88 | + it('should collect unique property names from all helpers', () => { |
| 89 | + const helpers = { |
| 90 | + helper1: { prop1: 'v1', prop2: 'v2' }, |
| 91 | + helper2: { prop2: 'v3', prop3: 'v4' }, |
| 92 | + helper3: { prop1: 'v5', prop3: 'v6', prop4: 'v7' }, |
| 93 | + } |
| 94 | + const result = createHelpersNamespaceObject(helpers) |
| 95 | + |
| 96 | + expect(Object.keys(result).sort()).toEqual([ |
| 97 | + 'prop1', |
| 98 | + 'prop2', |
| 99 | + 'prop3', |
| 100 | + 'prop4', |
| 101 | + ]) |
| 102 | + }) |
| 103 | + |
| 104 | + it('should handle helpers with undefined values', () => { |
| 105 | + const helpers = { |
| 106 | + helper1: { prop1: 'value1', prop2: undefined }, |
| 107 | + helper2: { prop1: undefined, prop2: 'value2' }, |
| 108 | + } |
| 109 | + const result = createHelpersNamespaceObject(helpers) |
| 110 | + |
| 111 | + expect(result['prop1']).toEqual({ helper1: 'value1' }) |
| 112 | + expect(result['prop2']).toEqual({ helper2: 'value2' }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('should handle multiple helpers with overlapping properties', () => { |
| 116 | + const helpers = { |
| 117 | + helper1: { shared: 'value1', unique1: 'u1' }, |
| 118 | + helper2: { shared: 'value2', unique2: 'u2' }, |
| 119 | + helper3: { shared: 'value3', unique3: 'u3' }, |
| 120 | + } |
| 121 | + const result = createHelpersNamespaceObject(helpers) |
| 122 | + |
| 123 | + expect(result['shared']).toEqual({ |
| 124 | + helper1: 'value1', |
| 125 | + helper2: 'value2', |
| 126 | + helper3: 'value3', |
| 127 | + }) |
| 128 | + expect(result['unique1']).toEqual({ helper1: 'u1' }) |
| 129 | + expect(result['unique2']).toEqual({ helper2: 'u2' }) |
| 130 | + expect(result['unique3']).toEqual({ helper3: 'u3' }) |
| 131 | + }) |
| 132 | + }) |
| 133 | +}) |
0 commit comments