patchProps.spec.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. patchProp(el, 'multiple', null, true)
  43. expect(el.multiple).toBe(true)
  44. patchProp(el, 'multiple', null, 0)
  45. expect(el.multiple).toBe(false)
  46. patchProp(el, 'multiple', null, '0')
  47. expect(el.multiple).toBe(true)
  48. patchProp(el, 'multiple', null, false)
  49. expect(el.multiple).toBe(false)
  50. patchProp(el, 'multiple', null, 1)
  51. expect(el.multiple).toBe(true)
  52. patchProp(el, 'multiple', null, undefined)
  53. expect(el.multiple).toBe(false)
  54. })
  55. test('innerHTML unmount prev children', () => {
  56. const fn = jest.fn()
  57. const comp = {
  58. render: () => 'foo',
  59. unmounted: fn
  60. }
  61. const root = document.createElement('div')
  62. render(h('div', null, [h(comp)]), root)
  63. expect(root.innerHTML).toBe(`<div>foo</div>`)
  64. render(h('div', { innerHTML: 'bar' }), root)
  65. expect(root.innerHTML).toBe(`<div>bar</div>`)
  66. expect(fn).toHaveBeenCalled()
  67. })
  68. // #954
  69. test('(svg) innerHTML unmount prev children', () => {
  70. const fn = jest.fn()
  71. const comp = {
  72. render: () => 'foo',
  73. unmounted: fn
  74. }
  75. const root = document.createElement('div')
  76. render(h('div', null, [h(comp)]), root)
  77. expect(root.innerHTML).toBe(`<div>foo</div>`)
  78. render(h('svg', { innerHTML: '<g></g>' }), root)
  79. expect(root.innerHTML).toBe(`<svg><g></g></svg>`)
  80. expect(fn).toHaveBeenCalled()
  81. })
  82. test('textContent unmount prev children', () => {
  83. const fn = jest.fn()
  84. const comp = {
  85. render: () => 'foo',
  86. unmounted: fn
  87. }
  88. const root = document.createElement('div')
  89. render(h('div', null, [h(comp)]), root)
  90. expect(root.innerHTML).toBe(`<div>foo</div>`)
  91. render(h('div', { textContent: 'bar' }), root)
  92. expect(root.innerHTML).toBe(`<div>bar</div>`)
  93. expect(fn).toHaveBeenCalled()
  94. })
  95. // #1049
  96. test('set value as-is for non string-value props', () => {
  97. const el = document.createElement('video')
  98. // jsdom doesn't really support video playback. srcObject in a real browser
  99. // should default to `null`, but in jsdom it's `undefined`.
  100. // anyway, here we just want to make sure Vue doesn't set non-string props
  101. // to an empty string on nullish values - it should reset to its default
  102. // value.
  103. const initialValue = el.srcObject
  104. const fakeObject = {}
  105. patchProp(el, 'srcObject', null, fakeObject)
  106. expect(el.srcObject).not.toBe(fakeObject)
  107. patchProp(el, 'srcObject', null, null)
  108. expect(el.srcObject).toBe(initialValue)
  109. })
  110. test('catch and warn prop set TypeError', () => {
  111. const el = document.createElement('div')
  112. Object.defineProperty(el, 'someProp', {
  113. set() {
  114. throw new TypeError('Invalid type')
  115. }
  116. })
  117. patchProp(el, 'someProp', null, 'foo')
  118. expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
  119. })
  120. // #1576
  121. test('remove attribute when value is falsy', () => {
  122. const el = document.createElement('div')
  123. patchProp(el, 'id', null, '')
  124. expect(el.hasAttribute('id')).toBe(true)
  125. patchProp(el, 'id', null, null)
  126. expect(el.hasAttribute('id')).toBe(false)
  127. patchProp(el, 'id', null, '')
  128. expect(el.hasAttribute('id')).toBe(true)
  129. patchProp(el, 'id', null, undefined)
  130. expect(el.hasAttribute('id')).toBe(false)
  131. patchProp(el, 'id', null, '')
  132. expect(el.hasAttribute('id')).toBe(true)
  133. // #2677
  134. const img = document.createElement('img')
  135. patchProp(img, 'width', null, '')
  136. expect(el.hasAttribute('width')).toBe(false)
  137. patchProp(img, 'width', null, 0)
  138. expect(img.hasAttribute('width')).toBe(true)
  139. patchProp(img, 'width', null, null)
  140. expect(img.hasAttribute('width')).toBe(false)
  141. patchProp(img, 'width', null, 0)
  142. expect(img.hasAttribute('width')).toBe(true)
  143. patchProp(img, 'width', null, undefined)
  144. expect(img.hasAttribute('width')).toBe(false)
  145. patchProp(img, 'width', null, 0)
  146. expect(img.hasAttribute('width')).toBe(true)
  147. })
  148. test('form attribute', () => {
  149. const el = document.createElement('input')
  150. patchProp(el, 'form', null, 'foo')
  151. // non existant element
  152. expect(el.form).toBe(null)
  153. expect(el.getAttribute('form')).toBe('foo')
  154. // remove attribute
  155. patchProp(el, 'form', 'foo', null)
  156. expect(el.getAttribute('form')).toBe(null)
  157. })
  158. test('readonly type prop on textarea', () => {
  159. const el = document.createElement('textarea')
  160. // just to verify that it doesn't throw when i.e. switching a dynamic :is from an 'input' to a 'textarea'
  161. // see https://github.com/vuejs/vue-next/issues/2766
  162. patchProp(el, 'type', 'text', null)
  163. })
  164. test('force patch as prop', () => {
  165. const el = document.createElement('div') as any
  166. patchProp(el, '.x', null, 1)
  167. expect(el.x).toBe(1)
  168. })
  169. test('force patch as attribute', () => {
  170. const el = document.createElement('div') as any
  171. el.x = 1
  172. patchProp(el, '^x', null, 2)
  173. expect(el.x).toBe(1)
  174. expect(el.getAttribute('x')).toBe('2')
  175. })
  176. test('input with size', () => {
  177. const el = document.createElement('input')
  178. patchProp(el, 'size', null, 100)
  179. expect(el.size).toBe(100)
  180. patchProp(el, 'size', 100, null)
  181. expect(el.getAttribute('size')).toBe(null)
  182. })
  183. test('patch value for select', () => {
  184. const root = document.createElement('div')
  185. render(
  186. h('select', { value: 'foo' }, [
  187. h('option', { value: 'foo' }, 'foo'),
  188. h('option', { value: 'bar' }, 'bar')
  189. ]),
  190. root
  191. )
  192. const el = root.children[0] as HTMLSelectElement
  193. expect(el.value).toBe('foo')
  194. render(
  195. h('select', { value: 'baz' }, [
  196. h('option', { value: 'foo' }, 'foo'),
  197. h('option', { value: 'baz' }, 'baz')
  198. ]),
  199. root
  200. )
  201. expect(el.value).toBe('baz')
  202. })
  203. })