patchProps.spec.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. const option = document.createElement('option')
  25. patchProp(option, 'textContent', null, 'foo')
  26. expect(option.value).toBe('foo')
  27. expect(option.getAttribute('value')).toBe(null)
  28. patchProp(option, 'value', null, 'foo')
  29. expect(option.value).toBe('foo')
  30. expect(option.getAttribute('value')).toBe('foo')
  31. })
  32. test('value for custom elements', () => {
  33. class TestElement extends HTMLElement {
  34. constructor() {
  35. super()
  36. }
  37. // intentionally uses _value because this is used in "normal" HTMLElement for storing the object of the set property value
  38. private _value: any
  39. get value() {
  40. return this._value
  41. }
  42. set value(val) {
  43. this._value = val
  44. this.setterCalled++
  45. }
  46. public setterCalled: number = 0
  47. }
  48. window.customElements.define('patch-props-test-element', TestElement)
  49. const el = document.createElement('patch-props-test-element') as TestElement
  50. patchProp(el, 'value', null, 'foo')
  51. expect(el.value).toBe('foo')
  52. expect(el.setterCalled).toBe(1)
  53. patchProp(el, 'value', null, null)
  54. expect(el.value).toBe('')
  55. expect(el.setterCalled).toBe(2)
  56. expect(el.getAttribute('value')).toBe(null)
  57. const obj = {}
  58. patchProp(el, 'value', null, obj)
  59. expect(el.value).toBe(obj)
  60. expect(el.setterCalled).toBe(3)
  61. })
  62. // For <input type="text">, setting el.value won't create a `value` attribute
  63. // so we need to add tests for other elements
  64. test('value for non-text input', () => {
  65. const el = document.createElement('option')
  66. el.textContent = 'foo' // #4956
  67. patchProp(el, 'value', null, 'foo')
  68. expect(el.getAttribute('value')).toBe('foo')
  69. expect(el.value).toBe('foo')
  70. patchProp(el, 'value', null, null)
  71. el.textContent = ''
  72. expect(el.value).toBe('')
  73. // #3475
  74. expect(el.getAttribute('value')).toBe(null)
  75. })
  76. test('boolean prop', () => {
  77. const el = document.createElement('select')
  78. patchProp(el, 'multiple', null, '')
  79. expect(el.multiple).toBe(true)
  80. patchProp(el, 'multiple', null, null)
  81. expect(el.multiple).toBe(false)
  82. patchProp(el, 'multiple', null, true)
  83. expect(el.multiple).toBe(true)
  84. patchProp(el, 'multiple', null, 0)
  85. expect(el.multiple).toBe(false)
  86. patchProp(el, 'multiple', null, '0')
  87. expect(el.multiple).toBe(true)
  88. patchProp(el, 'multiple', null, false)
  89. expect(el.multiple).toBe(false)
  90. patchProp(el, 'multiple', null, 1)
  91. expect(el.multiple).toBe(true)
  92. patchProp(el, 'multiple', null, undefined)
  93. expect(el.multiple).toBe(false)
  94. })
  95. test('innerHTML unmount prev children', () => {
  96. const fn = vi.fn()
  97. const comp = {
  98. render: () => 'foo',
  99. unmounted: fn
  100. }
  101. const root = document.createElement('div')
  102. render(h('div', null, [h(comp)]), root)
  103. expect(root.innerHTML).toBe(`<div>foo</div>`)
  104. render(h('div', { innerHTML: 'bar' }), root)
  105. expect(root.innerHTML).toBe(`<div>bar</div>`)
  106. expect(fn).toHaveBeenCalled()
  107. })
  108. // #954
  109. test('(svg) innerHTML unmount prev children', () => {
  110. const fn = vi.fn()
  111. const comp = {
  112. render: () => 'foo',
  113. unmounted: fn
  114. }
  115. const root = document.createElement('div')
  116. render(h('div', null, [h(comp)]), root)
  117. expect(root.innerHTML).toBe(`<div>foo</div>`)
  118. render(h('svg', { innerHTML: '<g></g>' }), root)
  119. expect(root.innerHTML).toBe(`<svg><g></g></svg>`)
  120. expect(fn).toHaveBeenCalled()
  121. })
  122. test('textContent unmount prev children', () => {
  123. const fn = vi.fn()
  124. const comp = {
  125. render: () => 'foo',
  126. unmounted: fn
  127. }
  128. const root = document.createElement('div')
  129. render(h('div', null, [h(comp)]), root)
  130. expect(root.innerHTML).toBe(`<div>foo</div>`)
  131. render(h('div', { textContent: 'bar' }), root)
  132. expect(root.innerHTML).toBe(`<div>bar</div>`)
  133. expect(fn).toHaveBeenCalled()
  134. })
  135. // #1049
  136. test('set value as-is for non string-value props', () => {
  137. const el = document.createElement('video')
  138. // jsdom doesn't really support video playback. srcObject in a real browser
  139. // should default to `null`, but in jsdom it's `undefined`.
  140. // anyway, here we just want to make sure Vue doesn't set non-string props
  141. // to an empty string on nullish values - it should reset to its default
  142. // value.
  143. const initialValue = el.srcObject
  144. const fakeObject = {}
  145. patchProp(el, 'srcObject', null, fakeObject)
  146. expect(el.srcObject).not.toBe(fakeObject)
  147. patchProp(el, 'srcObject', null, null)
  148. expect(el.srcObject).toBe(initialValue)
  149. })
  150. test('catch and warn prop set TypeError', () => {
  151. const el = document.createElement('div')
  152. Object.defineProperty(el, 'someProp', {
  153. set() {
  154. throw new TypeError('Invalid type')
  155. }
  156. })
  157. patchProp(el, 'someProp', null, 'foo')
  158. expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
  159. })
  160. // #1576
  161. test('remove attribute when value is falsy', () => {
  162. const el = document.createElement('div')
  163. patchProp(el, 'id', null, '')
  164. expect(el.hasAttribute('id')).toBe(true)
  165. patchProp(el, 'id', null, null)
  166. expect(el.hasAttribute('id')).toBe(false)
  167. patchProp(el, 'id', null, '')
  168. expect(el.hasAttribute('id')).toBe(true)
  169. patchProp(el, 'id', null, undefined)
  170. expect(el.hasAttribute('id')).toBe(false)
  171. patchProp(el, 'id', null, '')
  172. expect(el.hasAttribute('id')).toBe(true)
  173. // #2677
  174. const img = document.createElement('img')
  175. patchProp(img, 'width', null, '')
  176. expect(el.hasAttribute('width')).toBe(false)
  177. patchProp(img, 'width', null, 0)
  178. expect(img.hasAttribute('width')).toBe(true)
  179. patchProp(img, 'width', null, null)
  180. expect(img.hasAttribute('width')).toBe(false)
  181. patchProp(img, 'width', null, 0)
  182. expect(img.hasAttribute('width')).toBe(true)
  183. patchProp(img, 'width', null, undefined)
  184. expect(img.hasAttribute('width')).toBe(false)
  185. patchProp(img, 'width', null, 0)
  186. expect(img.hasAttribute('width')).toBe(true)
  187. })
  188. test('form attribute', () => {
  189. const el = document.createElement('input')
  190. patchProp(el, 'form', null, 'foo')
  191. // non existent element
  192. expect(el.form).toBe(null)
  193. expect(el.getAttribute('form')).toBe('foo')
  194. // remove attribute
  195. patchProp(el, 'form', 'foo', null)
  196. expect(el.getAttribute('form')).toBe(null)
  197. })
  198. test('readonly type prop on textarea', () => {
  199. const el = document.createElement('textarea')
  200. // just to verify that it doesn't throw when i.e. switching a dynamic :is from an 'input' to a 'textarea'
  201. // see https://github.com/vuejs/core/issues/2766
  202. patchProp(el, 'type', 'text', null)
  203. })
  204. test('force patch as prop', () => {
  205. const el = document.createElement('div') as any
  206. patchProp(el, '.x', null, 1)
  207. expect(el.x).toBe(1)
  208. })
  209. test('force patch as attribute', () => {
  210. const el = document.createElement('div') as any
  211. el.x = 1
  212. patchProp(el, '^x', null, 2)
  213. expect(el.x).toBe(1)
  214. expect(el.getAttribute('x')).toBe('2')
  215. })
  216. test('input with size (number property)', () => {
  217. const el = document.createElement('input')
  218. patchProp(el, 'size', null, 100)
  219. expect(el.size).toBe(100)
  220. patchProp(el, 'size', 100, null)
  221. expect(el.getAttribute('size')).toBe(null)
  222. expect('Failed setting prop "size" on <input>').not.toHaveBeenWarned()
  223. patchProp(el, 'size', null, 'foobar')
  224. expect('Failed setting prop "size" on <input>').toHaveBeenWarnedLast()
  225. })
  226. test('select with type (string property)', () => {
  227. const el = document.createElement('select')
  228. patchProp(el, 'type', null, 'test')
  229. expect(el.type).toBe('select-one')
  230. expect('Failed setting prop "type" on <select>').toHaveBeenWarnedLast()
  231. })
  232. test('select with willValidate (boolean property)', () => {
  233. const el = document.createElement('select')
  234. patchProp(el, 'willValidate', true, null)
  235. expect(el.willValidate).toBe(true)
  236. expect(
  237. 'Failed setting prop "willValidate" on <select>'
  238. ).toHaveBeenWarnedLast()
  239. })
  240. test('patch value for select', () => {
  241. const root = document.createElement('div')
  242. render(
  243. h('select', { value: 'foo' }, [
  244. h('option', { value: 'foo' }, 'foo'),
  245. h('option', { value: 'bar' }, 'bar')
  246. ]),
  247. root
  248. )
  249. const el = root.children[0] as HTMLSelectElement
  250. expect(el.value).toBe('foo')
  251. render(
  252. h('select', { value: 'baz' }, [
  253. h('option', { value: 'foo' }, 'foo'),
  254. h('option', { value: 'baz' }, 'baz')
  255. ]),
  256. root
  257. )
  258. expect(el.value).toBe('baz')
  259. })
  260. test('translate attribute', () => {
  261. const el = document.createElement('div')
  262. patchProp(el, 'translate', null, 'no')
  263. expect(el.translate).toBeFalsy()
  264. expect(el.getAttribute('translate')).toBe('no')
  265. })
  266. })