dom.spec.ts 4.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { UniEventListener } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
2
import { createPageNode } from '../../../src/service/framework/dom/Page'
fxy060608's avatar
fxy060608 已提交
3 4 5
import {
  createElement,
  createTextNode,
fxy060608's avatar
fxy060608 已提交
6
  withModifiers,
fxy060608's avatar
fxy060608 已提交
7
} from '../../../../uni-app-vue/lib/service.runtime.esm'
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16
import {
  InsertAction,
  ACTION_TYPE_INSERT,
  SetAttributeAction,
  ACTION_TYPE_SET_ATTRIBUTE,
  ACTION_TYPE_REMOVE_ATTRIBUTE,
  ACTION_TYPE_SET_TEXT,
  ACTION_TYPE_REMOVE,
} from '../../../src/PageAction'
fxy060608's avatar
fxy060608 已提交
17 18

import { EventModifierFlags } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
19 20 21
describe('dom', () => {
  const pageId = 1
  const root = createPageNode(pageId, {
fxy060608's avatar
fxy060608 已提交
22
    css: true,
fxy060608's avatar
fxy060608 已提交
23
    route: 'pages/index/index',
fxy060608's avatar
fxy060608 已提交
24 25
    version: 1,
    locale: 'zh_CN',
fxy060608's avatar
fxy060608 已提交
26 27 28
    platform: 'ios',
    pixelRatio: 1,
    windowWidth: 375,
fxy060608's avatar
fxy060608 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    disableScroll: false,
    onPageScroll: false,
    onPageReachBottom: false,
    onReachBottomDistance: 50,
    statusbarHeight: 24,
    windowTop: 0,
    windowBottom: 0,
  })
  test('proxyNode', () => {
    const viewElem = createElement('view')
    viewElem.setAttribute('id', 'view')
    root.appendChild(viewElem)
    viewElem.setAttribute('hidden', true)
    const { updateActions } = root
    const addElementAction = updateActions[0] as InsertAction
    expect(addElementAction[0]).toBe(ACTION_TYPE_INSERT)
    expect(addElementAction[1]).toBe(1) // nodeId
    expect(addElementAction[2]).toBe(0) // parentNodeId
fxy060608's avatar
fxy060608 已提交
47
    expect(addElementAction[3]).toBe(-1) // index
fxy060608's avatar
fxy060608 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    const setAttributeAction = updateActions[1] as SetAttributeAction
    expect(setAttributeAction[0]).toBe(ACTION_TYPE_SET_ATTRIBUTE)
    expect(setAttributeAction[1]).toBe(1)
    expect(setAttributeAction[2]).toBe('hidden')
    expect(setAttributeAction[3]).toBe(true)

    root.updateActions.length = 0
    viewElem.removeAttribute('hidden')
    const {
      updateActions: [removeAttributeAction],
    } = root
    expect(removeAttributeAction[0]).toBe(ACTION_TYPE_REMOVE_ATTRIBUTE)
    expect(removeAttributeAction[1]).toBe(1)
    expect(removeAttributeAction[2]).toBe('hidden')

    root.updateActions.length = 0
    viewElem.textContent = 'text'
    const {
      updateActions: [setTextAction],
    } = root
    expect(setTextAction[0]).toBe(ACTION_TYPE_SET_TEXT)
    expect(setTextAction[1]).toBe(1)
    expect(setTextAction[2]).toBe('text')

    root.updateActions.length = 0
    root.removeChild(viewElem)
    const {
      updateActions: [removeChildAction],
    } = root
    expect(removeChildAction[0]).toBe(ACTION_TYPE_REMOVE)
    expect(removeChildAction[1]).toBe(1)

    root.updateActions.length = 0
    const textNode = createTextNode('hello')
    root.appendChild(textNode)
    const {
      updateActions: [addTextNodeAction],
    } = root
    expect(addTextNodeAction[0]).toBe(ACTION_TYPE_INSERT)
    expect(addTextNodeAction[1]).toBe(2)
    expect(addTextNodeAction[2]).toBe(0)
fxy060608's avatar
fxy060608 已提交
90
    expect(addTextNodeAction[3]).toBe(-1)
fxy060608's avatar
fxy060608 已提交
91 92 93 94 95 96 97 98 99 100

    root.updateActions.length = 0
    const clickFn = () => {}
    textNode.addEventListener('click', clickFn)
    const {
      updateActions: [addEventListenerAction],
    } = root
    expect(addEventListenerAction[0]).toBe(ACTION_TYPE_SET_ATTRIBUTE)
    expect(addEventListenerAction[1]).toBe(2)
    expect(addEventListenerAction[2]).toBe('.e0')
fxy060608's avatar
fxy060608 已提交
101
    expect(addEventListenerAction[3]).toBe(0)
fxy060608's avatar
fxy060608 已提交
102 103 104 105 106 107 108 109 110

    root.updateActions.length = 0
    textNode.removeEventListener('click', clickFn)
    const {
      updateActions: [removeEventListenerAction],
    } = root
    expect(removeEventListenerAction[0]).toBe(ACTION_TYPE_REMOVE_ATTRIBUTE)
    expect(removeEventListenerAction[1]).toBe(2)
    expect(removeEventListenerAction[2]).toBe('.e0')
fxy060608's avatar
fxy060608 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

    root.updateActions.length = 0
    const clickFn1 = withModifiers(() => {}, [
      'stop',
      'prevent',
    ]) as unknown as UniEventListener
    textNode.addEventListener('click', clickFn1, { capture: true })
    const {
      updateActions: [addEventListenerAction1],
    } = root
    expect(addEventListenerAction1[0]).toBe(ACTION_TYPE_SET_ATTRIBUTE)
    expect(addEventListenerAction1[1]).toBe(2)
    expect(addEventListenerAction1[2]).toBe('.e00')
    const flag = addEventListenerAction1[3] as number
    expect(flag & EventModifierFlags.stop).toBeTruthy()
    expect(flag & EventModifierFlags.prevent).toBeTruthy()
    expect(flag & EventModifierFlags.self).toBeFalsy()
fxy060608's avatar
fxy060608 已提交
128 129
  })
})