]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor: adjust runtime-dom test structure + tests for dom props
authorEvan You <yyx990803@gmail.com>
Fri, 10 Apr 2020 19:37:30 +0000 (15:37 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 10 Apr 2020 19:37:30 +0000 (15:37 -0400)
packages/runtime-dom/__tests__/patchAttrs.spec.ts [moved from packages/runtime-dom/__tests__/modules/attrs.spec.ts with 92% similarity]
packages/runtime-dom/__tests__/patchClass.spec.ts [moved from packages/runtime-dom/__tests__/modules/class.spec.ts with 84% similarity]
packages/runtime-dom/__tests__/patchEvents.spec.ts [moved from packages/runtime-dom/__tests__/modules/events.spec.ts with 98% similarity]
packages/runtime-dom/__tests__/patchProps.spec.ts [new file with mode: 0644]
packages/runtime-dom/__tests__/patchStyle.spec.ts [moved from packages/runtime-dom/__tests__/modules/style.spec.ts with 98% similarity]
packages/runtime-dom/src/modules/props.ts

similarity index 92%
rename from packages/runtime-dom/__tests__/modules/attrs.spec.ts
rename to packages/runtime-dom/__tests__/patchAttrs.spec.ts
index 2a3bad0d37f073e49efba1d167e6f3c8e8ea58df..932c4a589cc5457c66e623a78f28efc4c93ddeac 100644 (file)
@@ -1,5 +1,5 @@
-import { patchProp } from '../../src/patchProp'
-import { xlinkNS } from '../../src/modules/attrs'
+import { patchProp } from '../src/patchProp'
+import { xlinkNS } from '../src/modules/attrs'
 
 describe('runtime-dom: attrs patching', () => {
   test('xlink attributes', () => {
similarity index 84%
rename from packages/runtime-dom/__tests__/modules/class.spec.ts
rename to packages/runtime-dom/__tests__/patchClass.spec.ts
index d6bfdeca7b54ba75a6c3a6260d9de4ecd8d40177..a784c7d543f35dc8c7feba8f8c943d9f5f76aa83 100644 (file)
@@ -1,6 +1,6 @@
-import { patchProp } from '../../src/patchProp'
-import { ElementWithTransition } from '../../src/components/Transition'
-import { svgNS } from '../../src/nodeOps'
+import { patchProp } from '../src/patchProp'
+import { ElementWithTransition } from '../src/components/Transition'
+import { svgNS } from '../src/nodeOps'
 
 describe('runtime-dom: class patching', () => {
   test('basics', () => {
similarity index 98%
rename from packages/runtime-dom/__tests__/modules/events.spec.ts
rename to packages/runtime-dom/__tests__/patchEvents.spec.ts
index 3b365e1ca3ae6c89e629302686e5a251705bc5cd..b870a8be344013476eb40ca05d74082bc89c3309 100644 (file)
@@ -1,4 +1,4 @@
-import { patchProp } from '../../src/patchProp'
+import { patchProp } from '../src/patchProp'
 
 const timeout = () => new Promise(r => setTimeout(r))
 
diff --git a/packages/runtime-dom/__tests__/patchProps.spec.ts b/packages/runtime-dom/__tests__/patchProps.spec.ts
new file mode 100644 (file)
index 0000000..96f3037
--- /dev/null
@@ -0,0 +1,62 @@
+import { patchProp } from '../src/patchProp'
+import { render, h } from '../src'
+
+describe('runtime-dom: props patching', () => {
+  test('basic', () => {
+    const el = document.createElement('div')
+    patchProp(el, 'id', null, 'foo')
+    expect(el.id).toBe('foo')
+    patchProp(el, 'id', null, null)
+    expect(el.id).toBe('')
+  })
+
+  test('value', () => {
+    const el = document.createElement('input')
+    patchProp(el, 'value', null, 'foo')
+    expect(el.value).toBe('foo')
+    patchProp(el, 'value', null, null)
+    expect(el.value).toBe('')
+    const obj = {}
+    patchProp(el, 'value', null, obj)
+    expect(el.value).toBe(obj.toString())
+    expect((el as any)._value).toBe(obj)
+  })
+
+  test('boolean prop', () => {
+    const el = document.createElement('select')
+    patchProp(el, 'multiple', null, '')
+    expect(el.multiple).toBe(true)
+    patchProp(el, 'multiple', null, null)
+    expect(el.multiple).toBe(false)
+  })
+
+  test('innerHTML unmount prev children', () => {
+    const fn = jest.fn()
+    const comp = {
+      render: () => 'foo',
+      unmounted: fn
+    }
+    const root = document.createElement('div')
+    render(h('div', null, [h(comp)]), root)
+    expect(root.innerHTML).toBe(`<div>foo</div>`)
+
+    render(h('div', { innerHTML: 'bar' }), root)
+    expect(root.innerHTML).toBe(`<div>bar</div>`)
+    expect(fn).toHaveBeenCalled()
+  })
+
+  test('textContent unmount prev children', () => {
+    const fn = jest.fn()
+    const comp = {
+      render: () => 'foo',
+      unmounted: fn
+    }
+    const root = document.createElement('div')
+    render(h('div', null, [h(comp)]), root)
+    expect(root.innerHTML).toBe(`<div>foo</div>`)
+
+    render(h('div', { textContent: 'bar' }), root)
+    expect(root.innerHTML).toBe(`<div>bar</div>`)
+    expect(fn).toHaveBeenCalled()
+  })
+})
similarity index 98%
rename from packages/runtime-dom/__tests__/modules/style.spec.ts
rename to packages/runtime-dom/__tests__/patchStyle.spec.ts
index 90fd60f0db0e574a1e272ca0ecaa95994fdc2949..c488e51ecab2221fec92c609b9c6f2886f652eb2 100644 (file)
@@ -1,4 +1,4 @@
-import { patchProp } from '../../src/patchProp'
+import { patchProp } from '../src/patchProp'
 
 describe(`runtime-dom: style patching`, () => {
   it('string', () => {
index 15f95ccadd34b72e1ad1a99f99b6b054f2dbb94e..0b36578eb88be0f0d4db8d1b0dc9a8159e393bb6 100644 (file)
@@ -14,8 +14,10 @@ export function patchDOMProp(
   parentSuspense: any,
   unmountChildren: any
 ) {
-  if ((key === 'innerHTML' || key === 'textContent') && prevChildren) {
-    unmountChildren(prevChildren, parentComponent, parentSuspense)
+  if (key === 'innerHTML' || key === 'textContent') {
+    if (prevChildren) {
+      unmountChildren(prevChildren, parentComponent, parentSuspense)
+    }
     el[key] = value == null ? '' : value
     return
   }