toDisplayString.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 objWithToStringOverride = {
  20. foo: 555,
  21. toString() {
  22. return 'override'
  23. }
  24. }
  25. expect(toDisplayString(objWithToStringOverride)).toBe('override')
  26. const objWithNonInvokeableToString = {
  27. foo: 555,
  28. toString: null
  29. }
  30. expect(toDisplayString(objWithNonInvokeableToString)).toBe(
  31. `{
  32. "foo": 555,
  33. "toString": null
  34. }`
  35. )
  36. // object created from null does not have .toString in its prototype
  37. const nullObjectWithoutToString = Object.create(null)
  38. nullObjectWithoutToString.bar = 1
  39. expect(toDisplayString(nullObjectWithoutToString)).toBe(
  40. `{
  41. "bar": 1
  42. }`
  43. )
  44. // array toString override is ignored
  45. const arrWithToStringOverride = [1, 2, 3]
  46. arrWithToStringOverride.toString = () =>
  47. 'override for array is not supported'
  48. expect(toDisplayString(arrWithToStringOverride)).toBe(
  49. `[
  50. 1,
  51. 2,
  52. 3
  53. ]`
  54. )
  55. })
  56. test('refs', () => {
  57. const n = ref(1)
  58. const np = computed(() => n.value + 1)
  59. expect(
  60. toDisplayString({
  61. n,
  62. np
  63. })
  64. ).toBe(JSON.stringify({ n: 1, np: 2 }, null, 2))
  65. })
  66. test('objects with custom toString', () => {
  67. class TestClass {
  68. toString() {
  69. return 'foo'
  70. }
  71. }
  72. const instance = new TestClass()
  73. expect(toDisplayString(instance)).toBe('foo')
  74. const obj = { toString: () => 'bar' }
  75. expect(toDisplayString(obj)).toBe('bar')
  76. })
  77. test('native objects', () => {
  78. const div = document.createElement('div')
  79. expect(toDisplayString(div)).toBe('[object HTMLDivElement]')
  80. expect(toDisplayString({ div })).toMatchInlineSnapshot(`
  81. "{
  82. \\"div\\": \\"[object HTMLDivElement]\\"
  83. }"
  84. `)
  85. })
  86. test('Map and Set', () => {
  87. const m = new Map<any, any>([
  88. [1, 'foo'],
  89. [{ baz: 1 }, { foo: 'bar', qux: 2 }]
  90. ])
  91. const s = new Set<any>([1, { foo: 'bar' }, m])
  92. expect(toDisplayString(m)).toMatchInlineSnapshot(`
  93. "{
  94. \\"Map(2)\\": {
  95. \\"1 =>\\": \\"foo\\",
  96. \\"[object Object] =>\\": {
  97. \\"foo\\": \\"bar\\",
  98. \\"qux\\": 2
  99. }
  100. }
  101. }"
  102. `)
  103. expect(toDisplayString(s)).toMatchInlineSnapshot(`
  104. "{
  105. \\"Set(3)\\": [
  106. 1,
  107. {
  108. \\"foo\\": \\"bar\\"
  109. },
  110. {
  111. \\"Map(2)\\": {
  112. \\"1 =>\\": \\"foo\\",
  113. \\"[object Object] =>\\": {
  114. \\"foo\\": \\"bar\\",
  115. \\"qux\\": 2
  116. }
  117. }
  118. }
  119. ]
  120. }"
  121. `)
  122. expect(
  123. toDisplayString({
  124. m,
  125. s
  126. })
  127. ).toMatchInlineSnapshot(`
  128. "{
  129. \\"m\\": {
  130. \\"Map(2)\\": {
  131. \\"1 =>\\": \\"foo\\",
  132. \\"[object Object] =>\\": {
  133. \\"foo\\": \\"bar\\",
  134. \\"qux\\": 2
  135. }
  136. }
  137. },
  138. \\"s\\": {
  139. \\"Set(3)\\": [
  140. 1,
  141. {
  142. \\"foo\\": \\"bar\\"
  143. },
  144. {
  145. \\"Map(2)\\": {
  146. \\"1 =>\\": \\"foo\\",
  147. \\"[object Object] =>\\": {
  148. \\"foo\\": \\"bar\\",
  149. \\"qux\\": 2
  150. }
  151. }
  152. }
  153. ]
  154. }
  155. }"
  156. `)
  157. })
  158. })