patchProps.spec.ts 9.0 KB

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