dom.spec.ts 4.6 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { encodeTag, 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
import {
  InsertAction,
  ACTION_TYPE_INSERT,
  SetAttributeAction,
  ACTION_TYPE_SET_ATTRIBUTE,
  ACTION_TYPE_REMOVE_ATTRIBUTE,
  ACTION_TYPE_SET_TEXT,
  ACTION_TYPE_REMOVE,
fxy060608's avatar
fxy060608 已提交
16 17
  CreateAction,
  ACTION_TYPE_CREATE,
fxy060608's avatar
fxy060608 已提交
18
} from '../../../src/PageAction'
fxy060608's avatar
fxy060608 已提交
19 20

import { EventModifierFlags } from '@dcloudio/uni-shared'
fxy060608's avatar
fxy060608 已提交
21 22 23
describe('dom', () => {
  const pageId = 1
  const root = createPageNode(pageId, {
fxy060608's avatar
fxy060608 已提交
24
    css: true,
fxy060608's avatar
fxy060608 已提交
25
    route: 'pages/index/index',
fxy060608's avatar
fxy060608 已提交
26 27
    version: 1,
    locale: 'zh_CN',
fxy060608's avatar
fxy060608 已提交
28 29 30
    platform: 'ios',
    pixelRatio: 1,
    windowWidth: 375,
fxy060608's avatar
fxy060608 已提交
31 32 33 34 35 36 37 38 39
    disableScroll: false,
    onPageScroll: false,
    onPageReachBottom: false,
    onReachBottomDistance: 50,
    statusbarHeight: 24,
    windowTop: 0,
    windowBottom: 0,
  })
  test('proxyNode', () => {
fxy060608's avatar
fxy060608 已提交
40
    const viewElem = createElement('view', { pageNode: root })
fxy060608's avatar
fxy060608 已提交
41 42 43 44
    viewElem.setAttribute('id', 'view')
    root.appendChild(viewElem)
    viewElem.setAttribute('hidden', true)
    const { updateActions } = root
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51
    const createElementAction = updateActions[0] as CreateAction
    expect(createElementAction[0]).toBe(ACTION_TYPE_CREATE)
    expect(createElementAction[1]).toBe(1)
    expect(createElementAction[2]).toBe(encodeTag('VIEW'))
    expect(createElementAction[3]).toBe(0)
    expect(createElementAction[4]!.a!.id).toBe('view')
    const addElementAction = updateActions[1] as InsertAction
fxy060608's avatar
fxy060608 已提交
52 53 54
    expect(addElementAction[0]).toBe(ACTION_TYPE_INSERT)
    expect(addElementAction[1]).toBe(1) // nodeId
    expect(addElementAction[2]).toBe(0) // parentNodeId
fxy060608's avatar
fxy060608 已提交
55
    expect(addElementAction[3]).toBe(-1) // index
fxy060608's avatar
fxy060608 已提交
56

fxy060608's avatar
fxy060608 已提交
57
    const setAttributeAction = updateActions[2] as SetAttributeAction
fxy060608's avatar
fxy060608 已提交
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
    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
fxy060608's avatar
fxy060608 已提交
90
    const textNode = createTextNode('hello', { pageNode: root })
fxy060608's avatar
fxy060608 已提交
91 92
    root.appendChild(textNode)
    const {
fxy060608's avatar
fxy060608 已提交
93
      updateActions: [, addTextNodeAction],
fxy060608's avatar
fxy060608 已提交
94 95 96 97
    } = root
    expect(addTextNodeAction[0]).toBe(ACTION_TYPE_INSERT)
    expect(addTextNodeAction[1]).toBe(2)
    expect(addTextNodeAction[2]).toBe(0)
fxy060608's avatar
fxy060608 已提交
98
    expect(addTextNodeAction[3]).toBe(-1)
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106 107 108

    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 已提交
109
    expect(addEventListenerAction[3]).toBe(0)
fxy060608's avatar
fxy060608 已提交
110 111 112 113 114 115 116 117 118

    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 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

    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 已提交
136 137
  })
})