|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { isString, toJsonString } from '../../src/util/stringUtil' |
| 3 | + |
| 4 | +describe('stringUtil', () => { |
| 5 | + describe(isString, () => { |
| 6 | + test('should return true for a string', () => { |
| 7 | + expect(isString('hello')).toBe(true) |
| 8 | + expect(isString('')).toBe(true) |
| 9 | + }) |
| 10 | + |
| 11 | + test('should return false for non-string values', () => { |
| 12 | + expect(isString(123)).toBe(false) |
| 13 | + expect(isString(true)).toBe(false) |
| 14 | + expect(isString({})).toBe(false) |
| 15 | + expect(isString(null)).toBe(false) |
| 16 | + expect(isString(undefined)).toBe(false) |
| 17 | + expect(isString([])).toBe(false) |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + describe(toJsonString, () => { |
| 22 | + test('should return the string as is if input is a string', () => { |
| 23 | + expect(toJsonString('hello')).toBe('hello') |
| 24 | + }) |
| 25 | + |
| 26 | + test('should return JSON string if input is a serializable object', () => { |
| 27 | + const obj = { foo: 'bar', num: 123 } |
| 28 | + expect(toJsonString(obj)).toBe(JSON.stringify(obj)) |
| 29 | + }) |
| 30 | + |
| 31 | + test('should return string representation if JSON.stringify throws', () => { |
| 32 | + const circular: any = { foo: 'bar' } |
| 33 | + circular.self = circular |
| 34 | + |
| 35 | + expect(toJsonString(circular)).toBe('[object Object]') |
| 36 | + expect(toJsonString(BigInt(9007199254740991))).toBe('9007199254740991') |
| 37 | + }) |
| 38 | + |
| 39 | + test('should return string representation for other types', () => { |
| 40 | + expect(toJsonString(123)).toBe('123') |
| 41 | + expect(toJsonString(true)).toBe('true') |
| 42 | + expect(toJsonString(null)).toBe('null') |
| 43 | + }) |
| 44 | + |
| 45 | + test('should handle undefined correctly', () => { |
| 46 | + expect(toJsonString(undefined)).toBeUndefined() |
| 47 | + }) |
| 48 | + }) |
| 49 | +}) |
0 commit comments