|
| 1 | +import type { BaseTranslation } from 'packages/runtime/src/core.mjs' |
| 2 | +import { suite } from 'uvu' |
| 3 | +import * as assert from 'uvu/assert' |
| 4 | +import { extendDictionary } from './index.mjs' |
| 5 | + |
| 6 | +const test = suite('utils') |
| 7 | + |
| 8 | +const translation = { |
| 9 | + simple: 'Hello', |
| 10 | + nested: { value: 'Hello nested' }, |
| 11 | +} |
| 12 | + |
| 13 | +test('does no mutation', () => { |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 15 | + const base = {} as any |
| 16 | + extendDictionary(base, { hey: 'there' }) |
| 17 | + assert.not(base.hey) |
| 18 | +}) |
| 19 | + |
| 20 | +test('empty base', () => { |
| 21 | + const extended = extendDictionary({}, { hey: 'there' }) |
| 22 | + assert.equal(extended, { |
| 23 | + hey: 'there', |
| 24 | + }) |
| 25 | +}) |
| 26 | + |
| 27 | +test('empty part', () => { |
| 28 | + const extended = extendDictionary({ hey: 'there' }, {}) |
| 29 | + assert.equal(extended, { |
| 30 | + hey: 'there', |
| 31 | + }) |
| 32 | +}) |
| 33 | + |
| 34 | +test('simple extend', () => { |
| 35 | + const extended = extendDictionary(translation, { simple: 'Hello extended' }) |
| 36 | + assert.equal(extended, { |
| 37 | + simple: 'Hello extended', |
| 38 | + nested: { |
| 39 | + value: 'Hello nested', |
| 40 | + }, |
| 41 | + }) |
| 42 | +}) |
| 43 | + |
| 44 | +test('nested extend', () => { |
| 45 | + const extended = extendDictionary(translation, { nested: { value: 'Hello nested extended' } }) |
| 46 | + assert.equal(extended, { |
| 47 | + simple: 'Hello', |
| 48 | + nested: { |
| 49 | + value: 'Hello nested extended', |
| 50 | + }, |
| 51 | + }) |
| 52 | +}) |
| 53 | + |
| 54 | +test('nested extend with simple', () => { |
| 55 | + const extended = extendDictionary(translation, { |
| 56 | + simple: 'Hello extended', |
| 57 | + nested: { value: 'Hello nested extended' }, |
| 58 | + }) |
| 59 | + assert.equal(extended, { |
| 60 | + simple: 'Hello extended', |
| 61 | + nested: { |
| 62 | + value: 'Hello nested extended', |
| 63 | + }, |
| 64 | + }) |
| 65 | +}) |
| 66 | + |
| 67 | +test('add prop', () => { |
| 68 | + const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, { |
| 69 | + add: 'test', |
| 70 | + }) |
| 71 | + |
| 72 | + assert.equal(extended, { |
| 73 | + simple: 'Hello', |
| 74 | + add: 'test', |
| 75 | + nested: { |
| 76 | + value: 'Hello nested', |
| 77 | + }, |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +test('add nested prop', () => { |
| 82 | + const extended = extendDictionary<BaseTranslation, BaseTranslation>(translation, { |
| 83 | + add: { |
| 84 | + nested: 'test', |
| 85 | + }, |
| 86 | + }) |
| 87 | + |
| 88 | + assert.equal(extended, { |
| 89 | + simple: 'Hello', |
| 90 | + add: { |
| 91 | + nested: 'test', |
| 92 | + }, |
| 93 | + nested: { |
| 94 | + value: 'Hello nested', |
| 95 | + }, |
| 96 | + }) |
| 97 | +}) |
| 98 | + |
| 99 | +test('array', () => { |
| 100 | + const extended = extendDictionary<BaseTranslation, BaseTranslation>( |
| 101 | + {}, |
| 102 | + { |
| 103 | + add: ['hey'], |
| 104 | + }, |
| 105 | + ) |
| 106 | + assert.equal(extended, { |
| 107 | + add: ['hey'], |
| 108 | + }) |
| 109 | + |
| 110 | + const extended2 = extendDictionary<BaseTranslation, BaseTranslation>(extended, { |
| 111 | + add: ['ho'], |
| 112 | + }) |
| 113 | + |
| 114 | + assert.equal(extended2, { |
| 115 | + add: ['ho'], |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +test.run() |
0 commit comments