| 1 |
import { |
| 2 |
convertToValidParamsArray, |
| 3 |
convertToValidParamsString, |
| 4 |
mapToneValuesToObjects, |
| 5 |
} from '@shared/utils/convert-to-valid-params'; |
| 6 |
|
| 7 |
const ALLOWED = [ |
| 8 |
{ label: 'Professional', value: 'professional' }, |
| 9 |
{ label: 'Friendly', value: 'friendly' }, |
| 10 |
]; |
| 11 |
|
| 12 |
describe('convertToValidParamsArray', () => { |
| 13 |
const allowed = ['info', 'pages', 'layout']; |
| 14 |
|
| 15 |
test('returns empty array if params is null', () => { |
| 16 |
expect(convertToValidParamsArray(null, allowed)).toEqual([]); |
| 17 |
}); |
| 18 |
|
| 19 |
test('returns empty array if allowedItems is empty', () => { |
| 20 |
expect(convertToValidParamsArray('info', [])).toEqual([]); |
| 21 |
}); |
| 22 |
|
| 23 |
test('filters out values not in allowed list', () => { |
| 24 |
expect(convertToValidParamsArray('info,invalid', allowed)).toEqual([ |
| 25 |
'info', |
| 26 |
]); |
| 27 |
}); |
| 28 |
|
| 29 |
test('removes duplicates', () => { |
| 30 |
expect(convertToValidParamsArray('info,info,pages', allowed)).toEqual([ |
| 31 |
'info', |
| 32 |
'pages', |
| 33 |
]); |
| 34 |
}); |
| 35 |
|
| 36 |
test('trims spaces around values', () => { |
| 37 |
expect(convertToValidParamsArray(' info , pages ', allowed)).toEqual([ |
| 38 |
'info', |
| 39 |
'pages', |
| 40 |
]); |
| 41 |
}); |
| 42 |
|
| 43 |
test('returns empty array if none are valid', () => { |
| 44 |
expect(convertToValidParamsArray('foo,bar', allowed)).toEqual([]); |
| 45 |
}); |
| 46 |
}); |
| 47 |
|
| 48 |
describe('convertToValidParamsString', () => { |
| 49 |
const allowed = ['info', 'pages', 'layout']; |
| 50 |
|
| 51 |
test('returns empty string if params is null', () => { |
| 52 |
expect(convertToValidParamsString(null, allowed)).toBe(''); |
| 53 |
}); |
| 54 |
|
| 55 |
test('returns single valid item as string', () => { |
| 56 |
expect(convertToValidParamsString('info', allowed)).toBe('info'); |
| 57 |
}); |
| 58 |
|
| 59 |
test('returns multiple valid items joined by comma', () => { |
| 60 |
expect(convertToValidParamsString('info,pages', allowed)).toBe( |
| 61 |
'info,pages', |
| 62 |
); |
| 63 |
}); |
| 64 |
|
| 65 |
test('filters out invalid values', () => { |
| 66 |
expect(convertToValidParamsString('info,unknown', allowed)).toBe('info'); |
| 67 |
}); |
| 68 |
|
| 69 |
test('removes duplicates', () => { |
| 70 |
expect(convertToValidParamsString('info,info,pages', allowed)).toBe( |
| 71 |
'info,pages', |
| 72 |
); |
| 73 |
}); |
| 74 |
|
| 75 |
test('trims spaces around values', () => { |
| 76 |
expect(convertToValidParamsString(' info , pages ', allowed)).toBe( |
| 77 |
'info,pages', |
| 78 |
); |
| 79 |
}); |
| 80 |
}); |
| 81 |
|
| 82 |
describe('mapToneValuesToObjects', () => { |
| 83 |
it('returns [] for null, undefined or empty array', () => { |
| 84 |
expect(mapToneValuesToObjects(null, ALLOWED)).toEqual([]); |
| 85 |
expect(mapToneValuesToObjects(undefined, ALLOWED)).toEqual([]); |
| 86 |
expect(mapToneValuesToObjects([], ALLOWED)).toEqual([]); |
| 87 |
}); |
| 88 |
|
| 89 |
it('maps valid values to objects', () => { |
| 90 |
expect(mapToneValuesToObjects(['professional'], ALLOWED)).toEqual([ |
| 91 |
{ label: 'Professional', value: 'professional' }, |
| 92 |
]); |
| 93 |
}); |
| 94 |
|
| 95 |
it('ignores invalid values', () => { |
| 96 |
expect(mapToneValuesToObjects(['x', 'friendly'], ALLOWED)).toEqual([ |
| 97 |
{ label: 'Friendly', value: 'friendly' }, |
| 98 |
]); |
| 99 |
}); |
| 100 |
|
| 101 |
it('keeps order and duplicates', () => { |
| 102 |
expect( |
| 103 |
mapToneValuesToObjects( |
| 104 |
['friendly', 'professional', 'professional'], |
| 105 |
ALLOWED, |
| 106 |
), |
| 107 |
).toEqual([ |
| 108 |
{ label: 'Friendly', value: 'friendly' }, |
| 109 |
{ label: 'Professional', value: 'professional' }, |
| 110 |
{ label: 'Professional', value: 'professional' }, |
| 111 |
]); |
| 112 |
}); |
| 113 |
}); |
| 114 |
|