RemoveJSONTrailingComma.utils.test.ts38 lines · main
| 1 | import { describe, expect, it } from 'vitest' |
| 2 | |
| 3 | import { removeJSONTrailingComma } from '@/lib/helpers' |
| 4 | |
| 5 | describe('removeJSONTrailingComma', () => { |
| 6 | it('should handle an empty object', () => { |
| 7 | const jsonString = '{}' |
| 8 | expect(removeJSONTrailingComma(jsonString)).toEqual(jsonString) |
| 9 | }) |
| 10 | |
| 11 | it('should handle an empty array', () => { |
| 12 | const jsonString = '[]' |
| 13 | expect(removeJSONTrailingComma(jsonString)).toEqual(jsonString) |
| 14 | }) |
| 15 | |
| 16 | it('should handle a JSON string without a trailing comma', () => { |
| 17 | const jsonString = '{"name": "John", "age": 25}' |
| 18 | expect(removeJSONTrailingComma(jsonString)).toEqual(jsonString) |
| 19 | }) |
| 20 | |
| 21 | it('should remove a trailing comma for JSON object', () => { |
| 22 | const jsonString = '{"name": "John", "age": 25,}' |
| 23 | const expectedOutput = '{"name": "John", "age": 25}' |
| 24 | expect(removeJSONTrailingComma(jsonString)).toEqual(expectedOutput) |
| 25 | }) |
| 26 | |
| 27 | it('should remove a trailing commas in an array of objects', () => { |
| 28 | const jsonString = '[{"fruit1": "apple","fruit2": "banana",}]' |
| 29 | const expectedOutput = '[{"fruit1": "apple","fruit2": "banana"}]' |
| 30 | expect(removeJSONTrailingComma(jsonString)).toEqual(expectedOutput) |
| 31 | }) |
| 32 | |
| 33 | it('should remove all trailing commas in an array of objects', () => { |
| 34 | const jsonString = '[{"fruit1": "apple","fruit2": "banana",},]' |
| 35 | const expectedOutput = '[{"fruit1": "apple","fruit2": "banana"}]' |
| 36 | expect(removeJSONTrailingComma(jsonString)).toEqual(expectedOutput) |
| 37 | }) |
| 38 | }) |