patchProps.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. import { patchProp } from '../src/patchProp'
  2. import { h, nextTick, ref, render } 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('patch innerHTML porp', async () => {
  123. const root = document.createElement('div')
  124. const state = ref(false)
  125. const Comp = {
  126. render: () => {
  127. if (state.value) {
  128. return h('div', [h('del', null, 'baz')])
  129. } else {
  130. return h('div', { innerHTML: 'baz' })
  131. }
  132. },
  133. }
  134. render(h(Comp), root)
  135. expect(root.innerHTML).toBe(`<div>baz</div>`)
  136. state.value = true
  137. await nextTick()
  138. expect(root.innerHTML).toBe(`<div><del>baz</del></div>`)
  139. })
  140. test('patch innerHTML porp w/ undefined value', async () => {
  141. const root = document.createElement('div')
  142. render(h('div', { innerHTML: undefined }), root)
  143. expect(root.innerHTML).toBe(`<div></div>`)
  144. })
  145. test('textContent unmount prev children', () => {
  146. const fn = vi.fn()
  147. const comp = {
  148. render: () => 'foo',
  149. unmounted: fn,
  150. }
  151. const root = document.createElement('div')
  152. render(h('div', null, [h(comp)]), root)
  153. expect(root.innerHTML).toBe(`<div>foo</div>`)
  154. render(h('div', { textContent: 'bar' }), root)
  155. expect(root.innerHTML).toBe(`<div>bar</div>`)
  156. expect(fn).toHaveBeenCalled()
  157. })
  158. // #1049
  159. test('set value as-is for non string-value props', () => {
  160. const el = document.createElement('video')
  161. // jsdom doesn't really support video playback. srcObject in a real browser
  162. // should default to `null`, but in jsdom it's `undefined`.
  163. // anyway, here we just want to make sure Vue doesn't set non-string props
  164. // to an empty string on nullish values - it should reset to its default
  165. // value.
  166. const initialValue = el.srcObject
  167. const fakeObject = {}
  168. patchProp(el, 'srcObject', null, fakeObject)
  169. expect(el.srcObject).not.toBe(fakeObject)
  170. patchProp(el, 'srcObject', null, null)
  171. expect(el.srcObject).toBe(initialValue)
  172. })
  173. test('catch and warn prop set TypeError', () => {
  174. const el = document.createElement('div')
  175. Object.defineProperty(el, 'someProp', {
  176. set() {
  177. throw new TypeError('Invalid type')
  178. },
  179. })
  180. patchProp(el, 'someProp', null, 'foo')
  181. expect(`Failed setting prop "someProp" on <div>`).toHaveBeenWarnedLast()
  182. })
  183. // #1576
  184. test('remove attribute when value is falsy', () => {
  185. const el = document.createElement('div')
  186. patchProp(el, 'id', null, '')
  187. expect(el.hasAttribute('id')).toBe(true)
  188. patchProp(el, 'id', null, null)
  189. expect(el.hasAttribute('id')).toBe(false)
  190. patchProp(el, 'id', null, '')
  191. expect(el.hasAttribute('id')).toBe(true)
  192. patchProp(el, 'id', null, undefined)
  193. expect(el.hasAttribute('id')).toBe(false)
  194. patchProp(el, 'id', null, '')
  195. expect(el.hasAttribute('id')).toBe(true)
  196. // #2677
  197. const img = document.createElement('img')
  198. patchProp(img, 'width', null, '')
  199. expect(el.hasAttribute('width')).toBe(false)
  200. patchProp(img, 'width', null, 0)
  201. expect(img.hasAttribute('width')).toBe(true)
  202. patchProp(img, 'width', null, null)
  203. expect(img.hasAttribute('width')).toBe(false)
  204. patchProp(img, 'width', null, 0)
  205. expect(img.hasAttribute('width')).toBe(true)
  206. patchProp(img, 'width', null, undefined)
  207. expect(img.hasAttribute('width')).toBe(false)
  208. patchProp(img, 'width', null, 0)
  209. expect(img.hasAttribute('width')).toBe(true)
  210. })
  211. test('form attribute', () => {
  212. const el = document.createElement('input')
  213. patchProp(el, 'form', null, 'foo')
  214. // non existent element
  215. expect(el.form).toBe(null)
  216. expect(el.getAttribute('form')).toBe('foo')
  217. // remove attribute
  218. patchProp(el, 'form', 'foo', null)
  219. expect(el.getAttribute('form')).toBe(null)
  220. })
  221. test('readonly type prop on textarea', () => {
  222. const el = document.createElement('textarea')
  223. // just to verify that it doesn't throw when i.e. switching a dynamic :is from an 'input' to a 'textarea'
  224. // see https://github.com/vuejs/core/issues/2766
  225. patchProp(el, 'type', 'text', null)
  226. })
  227. test('force patch as prop', () => {
  228. const el = document.createElement('div') as any
  229. patchProp(el, '.x', null, 1)
  230. expect(el.x).toBe(1)
  231. })
  232. test('force patch as attribute', () => {
  233. const el = document.createElement('div') as any
  234. el.x = 1
  235. patchProp(el, '^x', null, 2)
  236. expect(el.x).toBe(1)
  237. expect(el.getAttribute('x')).toBe('2')
  238. })
  239. test('input with size (number property)', () => {
  240. const el = document.createElement('input')
  241. patchProp(el, 'size', null, 100)
  242. expect(el.size).toBe(100)
  243. patchProp(el, 'size', 100, null)
  244. expect(el.getAttribute('size')).toBe(null)
  245. expect('Failed setting prop "size" on <input>').not.toHaveBeenWarned()
  246. patchProp(el, 'size', null, 'foobar')
  247. expect('Failed setting prop "size" on <input>').toHaveBeenWarnedLast()
  248. })
  249. test('select with type (string property)', () => {
  250. const el = document.createElement('select')
  251. patchProp(el, 'type', null, 'test')
  252. expect(el.type).toBe('select-one')
  253. expect('Failed setting prop "type" on <select>').toHaveBeenWarnedLast()
  254. })
  255. test('select with willValidate (boolean property)', () => {
  256. const el = document.createElement('select')
  257. patchProp(el, 'willValidate', true, null)
  258. expect(el.willValidate).toBe(true)
  259. expect(
  260. 'Failed setting prop "willValidate" on <select>',
  261. ).toHaveBeenWarnedLast()
  262. })
  263. test('patch value for select', () => {
  264. const root = document.createElement('div')
  265. render(
  266. h('select', { value: 'foo' }, [
  267. h('option', { value: 'foo' }, 'foo'),
  268. h('option', { value: 'bar' }, 'bar'),
  269. ]),
  270. root,
  271. )
  272. const el = root.children[0] as HTMLSelectElement
  273. expect(el.value).toBe('foo')
  274. render(
  275. h('select', { value: 'baz' }, [
  276. h('option', { value: 'foo' }, 'foo'),
  277. h('option', { value: 'baz' }, 'baz'),
  278. ]),
  279. root,
  280. )
  281. expect(el.value).toBe('baz')
  282. })
  283. test('init empty value for option', () => {
  284. const root = document.createElement('div')
  285. render(
  286. h('select', { value: 'foo' }, [h('option', { value: '' }, 'foo')]),
  287. root,
  288. )
  289. const select = root.children[0] as HTMLSelectElement
  290. const option = select.children[0] as HTMLOptionElement
  291. expect(select.value).toBe('')
  292. expect(option.value).toBe('')
  293. })
  294. // #8780
  295. test('embedded tag with width and height', () => {
  296. // Width and height of some embedded element such as img、video、source、canvas
  297. // must be set as attribute
  298. const el = document.createElement('img')
  299. patchProp(el, 'width', null, '24px')
  300. expect(el.getAttribute('width')).toBe('24px')
  301. })
  302. // # 9762 should fallthrough to `key in el` logic for non embedded tags
  303. test('width and height on custom elements', () => {
  304. const el = document.createElement('foobar')
  305. patchProp(el, 'width', null, '24px')
  306. expect(el.getAttribute('width')).toBe('24px')
  307. })
  308. test('translate attribute', () => {
  309. const el = document.createElement('div')
  310. patchProp(el, 'translate', null, 'no')
  311. expect(el.translate).toBeFalsy()
  312. expect(el.getAttribute('translate')).toBe('no')
  313. })
  314. })