toDisplayString.spec.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. const foo = Object.create(null)
  20. foo.bar = 1
  21. expect(toDisplayString(foo)).toBe(JSON.stringify(foo, null, 2))
  22. })
  23. test('refs', () => {
  24. const n = ref(1)
  25. const np = computed(() => n.value + 1)
  26. expect(
  27. toDisplayString({
  28. n,
  29. np
  30. })
  31. ).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
  32. })
  33. test('objects with custom toString', () => {
  34. class TestClass {
  35. toString() {
  36. return 'foo'
  37. }
  38. }
  39. const instance = new TestClass()
  40. expect(toDisplayString(instance)).toBe('foo')
  41. const obj = { toString: () => 'bar' }
  42. expect(toDisplayString(obj)).toBe('bar')
  43. })
  44. test('native objects', () => {
  45. const div = document.createElement('div')
  46. expect(toDisplayString(div)).toBe('[object HTMLDivElement]')
  47. expect(toDisplayString({ div })).toMatchInlineSnapshot(`
  48. "{
  49. \\"div\\": \\"[object HTMLDivElement]\\"
  50. }"
  51. `)
  52. })
  53. test('Map and Set', () => {
  54. const m = new Map<any, any>([
  55. [1, 'foo'],
  56. [{ baz: 1 }, { foo: 'bar', qux: 2 }]
  57. ])
  58. const s = new Set<any>([1, { foo: 'bar' }, m])
  59. expect(toDisplayString(m)).toMatchInlineSnapshot(`
  60. "{
  61. \\"Map(2)\\": {
  62. \\"1 =>\\": \\"foo\\",
  63. \\"[object Object] =>\\": {
  64. \\"foo\\": \\"bar\\",
  65. \\"qux\\": 2
  66. }
  67. }
  68. }"
  69. `)
  70. expect(toDisplayString(s)).toMatchInlineSnapshot(`
  71. "{
  72. \\"Set(3)\\": [
  73. 1,
  74. {
  75. \\"foo\\": \\"bar\\"
  76. },
  77. {
  78. \\"Map(2)\\": {
  79. \\"1 =>\\": \\"foo\\",
  80. \\"[object Object] =>\\": {
  81. \\"foo\\": \\"bar\\",
  82. \\"qux\\": 2
  83. }
  84. }
  85. }
  86. ]
  87. }"
  88. `)
  89. expect(
  90. toDisplayString({
  91. m,
  92. s
  93. })
  94. ).toMatchInlineSnapshot(`
  95. "{
  96. \\"m\\": {
  97. \\"Map(2)\\": {
  98. \\"1 =>\\": \\"foo\\",
  99. \\"[object Object] =>\\": {
  100. \\"foo\\": \\"bar\\",
  101. \\"qux\\": 2
  102. }
  103. }
  104. },
  105. \\"s\\": {
  106. \\"Set(3)\\": [
  107. 1,
  108. {
  109. \\"foo\\": \\"bar\\"
  110. },
  111. {
  112. \\"Map(2)\\": {
  113. \\"1 =>\\": \\"foo\\",
  114. \\"[object Object] =>\\": {
  115. \\"foo\\": \\"bar\\",
  116. \\"qux\\": 2
  117. }
  118. }
  119. }
  120. ]
  121. }
  122. }"
  123. `)
  124. })
  125. })