patchProps.spec.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { patchProp } from '../src/patchProp'
  2. import { render, h } from '../src'
  3. describe('runtime-dom: props patching', () => {
  4. test('basic', () => {
  5. const el = document.createElement('div')
  6. patchProp(el, 'id', null, 'foo')
  7. expect(el.id).toBe('foo')
  8. // prop with string value should be set to empty string on null values
  9. patchProp(el, 'id', null, null)
  10. expect(el.id).toBe('')
  11. expect(el.getAttribute('id')).toBe(null)
  12. })
  13. test('value', () => {
  14. const el = document.createElement('input')
  15. patchProp(el, 'value', null, 'foo')
  16. expect(el.value).toBe('foo')
  17. patchProp(el, 'value', null, null)
  18. expect(el.value).toBe('')
  19. expect(el.getAttribute('value')).toBe(null)
  20. const obj = {}
  21. patchProp(el, 'value', null, obj)
  22. expect(el.value).toBe(obj.toString())
  23. expect((el as any)._value).toBe(obj)
  24. })
  25. // For <input type="text">, setting el.value won't create a `value` attribute
  26. // so we need to add tests for other elements
  27. test('value for non-text input', () => {
  28. const el = document.createElement('option')
  29. patchProp(el, 'value', null, 'foo')
  30. expect(el.value).toBe('foo')
  31. patchProp(el, 'value', null, null)
  32. expect(el.value).toBe('')
  33. // #3475
  34. expect(el.getAttribute('value')).toBe(null)
  35. })
  36. test('boolean prop', () => {
  37. const el = document.createElement('select')
  38. patchProp(el, 'multiple', null, '')
  39. expect(el.multiple).toBe(true)
  40. patchProp(el, 'multiple', null, null)
  41. expect(el.multiple).toBe(false)
  42. })
  43. test('innerHTML unmount prev children', () => {
  44. const fn = jest.fn()
  45. const comp = {
  46. render: () => 'foo',
  47. unmounted: fn
  48. }
  49. const root = document.createElement('div')
  50. render(h('div', null, [h(comp)]), root)
  51. expect(root.innerHTML).toBe(`<div>foo</div>`)
  52. render(h('div', { innerHTML: 'bar' }), root)
  53. expect(root.innerHTML).toBe(`<div>bar</div>`)
  54. expect(fn).toHaveBeenCalled()
  55. })
  56. // #954
  57. test('(svg) innerHTML unmount prev children', () => {
  58. const fn = jest.fn()
  59. const comp = {
  60. render: () => 'foo',
  61. unmounted: fn
  62. }
  63. const root = document.createElement('div')
  64. render(h('div', null, [h(comp)]), root)
  65. expect(root.innerHTML).toBe(`<div>foo</div>`)
  66. render(h('svg', { innerHTML: '<g></g>' }), root)
  67. expect(root.innerHTML).toBe(`<svg><g></g></svg>`)
  68. expect(fn).toHaveBeenCalled()
  69. })
  70. test('textContent unmount prev children', () => {
  71. const fn = jest.fn()
  72. const comp = {
  73. render: () => 'foo',
  74. unmounted: fn
  75. }
  76. const root = document.createElement('div')
  77. render(h('div', null, [h(comp)]), root)
  78. expect(root.innerHTML).toBe(`<div>foo</div>`)
  79. render(h('div', { textContent: 'bar' }), root)
  80. expect(root.innerHTML).toBe(`<div>bar</div>`)
  81. expect(fn).toHaveBeenCalled()
  82. })
  83. // #1049
  84. test('set value as-is for non string-value props', () => {
  85. const el = document.createElement('video')
  86. // jsdom doesn't really support video playback. srcObject in a real browser
  87. // should default to `null`, but in jsdom it's `undefined`.
  88. // anyway, here we just want to make sure Vue doesn't set non-string props
  89. // to an empty string on nullish values - it should reset to its default
  90. // value.
  91. const initialValue = el.srcObject
  92. const fakeObject = {}
  93. patchProp(el, 'srcObject', null, fakeObject)
  94. expect(el.srcObject).not.toBe(fakeObject)
  95. patchProp(el, 'srcObject', null, null)
  96. expect(el.srcObject).toBe(initialValue)
  97. })
  98. test('catch and warn prop set TypeError', () => {
  99. const el = document.createElement('div')
  100. Object.defineProperty(el, 'someProp', {
  101. set() {
  102. throw new TypeError('Invalid type')
  103. }
  104. })
  105. patchProp(el, 'someProp', null, 'foo')
  106. expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
  107. })
  108. // #1576
  109. test('remove attribute when value is falsy', () => {
  110. const el = document.createElement('div')
  111. patchProp(el, 'id', null, '')
  112. expect(el.hasAttribute('id')).toBe(true)
  113. patchProp(el, 'id', null, null)
  114. expect(el.hasAttribute('id')).toBe(false)
  115. patchProp(el, 'id', null, '')
  116. expect(el.hasAttribute('id')).toBe(true)
  117. patchProp(el, 'id', null, undefined)
  118. expect(el.hasAttribute('id')).toBe(false)
  119. patchProp(el, 'id', null, '')
  120. expect(el.hasAttribute('id')).toBe(true)
  121. // #2677
  122. const img = document.createElement('img')
  123. patchProp(img, 'width', null, '')
  124. expect(el.hasAttribute('width')).toBe(false)
  125. patchProp(img, 'width', null, 0)
  126. expect(img.hasAttribute('width')).toBe(true)
  127. patchProp(img, 'width', null, null)
  128. expect(img.hasAttribute('width')).toBe(false)
  129. patchProp(img, 'width', null, 0)
  130. expect(img.hasAttribute('width')).toBe(true)
  131. patchProp(img, 'width', null, undefined)
  132. expect(img.hasAttribute('width')).toBe(false)
  133. patchProp(img, 'width', null, 0)
  134. expect(img.hasAttribute('width')).toBe(true)
  135. })
  136. test('form attribute', () => {
  137. const el = document.createElement('input')
  138. patchProp(el, 'form', null, 'foo')
  139. // non existant element
  140. expect(el.form).toBe(null)
  141. expect(el.getAttribute('form')).toBe('foo')
  142. // remove attribute
  143. patchProp(el, 'form', 'foo', null)
  144. expect(el.getAttribute('form')).toBe(null)
  145. })
  146. test('readonly type prop on textarea', () => {
  147. const el = document.createElement('textarea')
  148. // just to verify that it doesn't throw when i.e. switching a dynamic :is from an 'input' to a 'textarea'
  149. // see https://github.com/vuejs/vue-next/issues/2766
  150. patchProp(el, 'type', 'text', null)
  151. })
  152. test('force patch as prop', () => {
  153. const el = document.createElement('div') as any
  154. patchProp(el, '.x', null, 1)
  155. expect(el.x).toBe(1)
  156. })
  157. test('force patch as attribute', () => {
  158. const el = document.createElement('div') as any
  159. el.x = 1
  160. patchProp(el, '^x', null, 2)
  161. expect(el.x).toBe(1)
  162. expect(el.getAttribute('x')).toBe('2')
  163. })
  164. test('input with size', () => {
  165. const el = document.createElement('input')
  166. patchProp(el, 'size', null, 100)
  167. expect(el.size).toBe(100)
  168. patchProp(el, 'size', 100, null)
  169. expect(el.getAttribute('size')).toBe(null)
  170. })
  171. test('patch value for select', () => {
  172. const root = document.createElement('div')
  173. render(
  174. h('select', { value: 'foo' }, [
  175. h('option', { value: 'foo' }, 'foo'),
  176. h('option', { value: 'bar' }, 'bar')
  177. ]),
  178. root
  179. )
  180. const el = root.children[0] as HTMLSelectElement
  181. expect(el.value).toBe('foo')
  182. render(
  183. h('select', { value: 'baz' }, [
  184. h('option', { value: 'foo' }, 'foo'),
  185. h('option', { value: 'baz' }, 'baz')
  186. ]),
  187. root
  188. )
  189. expect(el.value).toBe('baz')
  190. })
  191. })