toDisplayString.spec.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { computed, ref } from '@vue/reactivity'
  2. import { toDisplayString } from '../src'
  3. describe('toDisplayString', () => {
  4. test('nullish values', () => {
  5. expect(toDisplayString(null)).toBe('')
  6. expect(toDisplayString(undefined)).toBe('')
  7. })
  8. test('primitive values', () => {
  9. expect(toDisplayString(1)).toBe('1')
  10. expect(toDisplayString(true)).toBe('true')
  11. expect(toDisplayString(false)).toBe('false')
  12. expect(toDisplayString('hello')).toBe('hello')
  13. })
  14. test('Object and Arrays', () => {
  15. const obj = { foo: 123 }
  16. expect(toDisplayString(obj)).toBe(JSON.stringify(obj, null, 2))
  17. const arr = [obj]
  18. expect(toDisplayString(arr)).toBe(JSON.stringify(arr, null, 2))
  19. })
  20. test('refs', () => {
  21. const n = ref(1)
  22. const np = computed(() => n.value + 1)
  23. expect(
  24. toDisplayString({
  25. n,
  26. np
  27. })
  28. ).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
  29. })
  30. test('objects with custom toString', () => {
  31. class TestClass {
  32. toString() {
  33. return 'foo'
  34. }
  35. }
  36. const instance = new TestClass()
  37. expect(toDisplayString(instance)).toBe('foo')
  38. const obj = { toString: () => 'bar' }
  39. expect(toDisplayString(obj)).toBe('bar')
  40. })
  41. test('native objects', () => {
  42. const div = document.createElement('div')
  43. expect(toDisplayString(div)).toBe('[object HTMLDivElement]')
  44. expect(toDisplayString({ div })).toMatchInlineSnapshot(`
  45. "{
  46. \\"div\\": \\"[object HTMLDivElement]\\"
  47. }"
  48. `)
  49. })
  50. test('Map and Set', () => {
  51. const m = new Map<any, any>([
  52. [1, 'foo'],
  53. [{ baz: 1 }, { foo: 'bar', qux: 2 }]
  54. ])
  55. const s = new Set<any>([1, { foo: 'bar' }, m])
  56. expect(toDisplayString(m)).toMatchInlineSnapshot(`
  57. "{
  58. \\"Map(2)\\": {
  59. \\"1 =>\\": \\"foo\\",
  60. \\"[object Object] =>\\": {
  61. \\"foo\\": \\"bar\\",
  62. \\"qux\\": 2
  63. }
  64. }
  65. }"
  66. `)
  67. expect(toDisplayString(s)).toMatchInlineSnapshot(`
  68. "{
  69. \\"Set(3)\\": [
  70. 1,
  71. {
  72. \\"foo\\": \\"bar\\"
  73. },
  74. {
  75. \\"Map(2)\\": {
  76. \\"1 =>\\": \\"foo\\",
  77. \\"[object Object] =>\\": {
  78. \\"foo\\": \\"bar\\",
  79. \\"qux\\": 2
  80. }
  81. }
  82. }
  83. ]
  84. }"
  85. `)
  86. expect(
  87. toDisplayString({
  88. m,
  89. s
  90. })
  91. ).toMatchInlineSnapshot(`
  92. "{
  93. \\"m\\": {
  94. \\"Map(2)\\": {
  95. \\"1 =>\\": \\"foo\\",
  96. \\"[object Object] =>\\": {
  97. \\"foo\\": \\"bar\\",
  98. \\"qux\\": 2
  99. }
  100. }
  101. },
  102. \\"s\\": {
  103. \\"Set(3)\\": [
  104. 1,
  105. {
  106. \\"foo\\": \\"bar\\"
  107. },
  108. {
  109. \\"Map(2)\\": {
  110. \\"1 =>\\": \\"foo\\",
  111. \\"[object Object] =>\\": {
  112. \\"foo\\": \\"bar\\",
  113. \\"qux\\": 2
  114. }
  115. }
  116. }
  117. ]
  118. }
  119. }"
  120. `)
  121. })
  122. })