|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 LibreSign contributors |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe, expect, it, beforeEach, vi } from 'vitest' |
| 7 | + |
| 8 | +// Mock console.warn to suppress logs during tests |
| 9 | +const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {}) |
| 10 | + |
| 11 | +import { normalizeRouteRecord } from '../../services/routeNormalization.ts' |
| 12 | + |
| 13 | +describe('normalizeRouteRecord', () => { |
| 14 | + beforeEach(() => { |
| 15 | + warnSpy.mockClear() |
| 16 | + }) |
| 17 | + |
| 18 | + it('returns empty object for non-object input', () => { |
| 19 | + expect(normalizeRouteRecord(null, 'params')).toEqual({}) |
| 20 | + expect(normalizeRouteRecord(undefined, 'query')).toEqual({}) |
| 21 | + expect(normalizeRouteRecord('string', 'params')).toEqual({}) |
| 22 | + expect(normalizeRouteRecord(123, 'query')).toEqual({}) |
| 23 | + }) |
| 24 | + |
| 25 | + it('rejects array input and returns empty object', () => { |
| 26 | + const result = normalizeRouteRecord(['value1', 'value2'], 'params') |
| 27 | + |
| 28 | + expect(result).toEqual({}) |
| 29 | + }) |
| 30 | + |
| 31 | + it('rejects array query input and returns empty object', () => { |
| 32 | + const result = normalizeRouteRecord(['param1', 'param2'], 'query') |
| 33 | + |
| 34 | + expect(result).toEqual({}) |
| 35 | + }) |
| 36 | + |
| 37 | + it('normalizes object with string values', () => { |
| 38 | + const input = { |
| 39 | + uuid: 'test-uuid', |
| 40 | + fileId: '123', |
| 41 | + name: 'document.pdf', |
| 42 | + } |
| 43 | + |
| 44 | + const result = normalizeRouteRecord(input, 'params') |
| 45 | + |
| 46 | + expect(result).toEqual(input) |
| 47 | + }) |
| 48 | + |
| 49 | + it('filters non-string values', () => { |
| 50 | + const input = { |
| 51 | + uuid: 'test-uuid', |
| 52 | + count: 123, |
| 53 | + active: true, |
| 54 | + nullable: null, |
| 55 | + } |
| 56 | + |
| 57 | + const result = normalizeRouteRecord(input, 'query') |
| 58 | + |
| 59 | + expect(result).toEqual({ uuid: 'test-uuid' }) |
| 60 | + }) |
| 61 | + |
| 62 | + it('handles mixed string and non-string values', () => { |
| 63 | + const input = { |
| 64 | + id: 'abc123', |
| 65 | + status: 'pending', |
| 66 | + count: 0, |
| 67 | + flag: false, |
| 68 | + name: '', |
| 69 | + } |
| 70 | + |
| 71 | + const result = normalizeRouteRecord(input, 'params') |
| 72 | + |
| 73 | + expect(result).toEqual({ |
| 74 | + id: 'abc123', |
| 75 | + status: 'pending', |
| 76 | + name: '', |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + it('handles empty object', () => { |
| 81 | + const result = normalizeRouteRecord({}, 'query') |
| 82 | + |
| 83 | + expect(result).toEqual({}) |
| 84 | + }) |
| 85 | + |
| 86 | + it('preserves empty string values (they are valid)', () => { |
| 87 | + const input = { |
| 88 | + uuid: '', |
| 89 | + name: 'test', |
| 90 | + } |
| 91 | + |
| 92 | + const result = normalizeRouteRecord(input, 'params') |
| 93 | + |
| 94 | + expect(result).toEqual(input) |
| 95 | + }) |
| 96 | + |
| 97 | + it('prevents numeric keys from array conversion by rejecting arrays', () => { |
| 98 | + // When Vue Router provides query: ['a', 'b', 'c'], without our protection, |
| 99 | + // Object.entries() would create { '0': 'a', '1': 'b', '2': 'c' } |
| 100 | + // Our fix rejects arrays entirely, preventing this issue |
| 101 | + const arrayInput: any = ['value1', 'value2', 'value3'] |
| 102 | + |
| 103 | + const result = normalizeRouteRecord(arrayInput, 'query') |
| 104 | + |
| 105 | + // Should return empty object, preventing the numeric keys |
| 106 | + expect(result).toEqual({}) |
| 107 | + }) |
| 108 | +}) |
| 109 | + |
0 commit comments