create-selector-query.test.js 3.4 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1
const PAGE_PATH = '/pages/API/create-selector-query/create-selector-query'
2 3 4

const RECT_LEFT = 15;
const RECT_WIDTH = 150;
5
const RECT_HEIGHT = 100;
6 7 8 9 10 11 12

describe('nodes-info', () => {
  let page
  beforeAll(async () => {
    page = await program.reLaunch(PAGE_PATH)
    await page.waitFor(500)
  })
H
hdx 已提交
13
  it('get-root-node-info', async () => {
H
hdx 已提交
14 15 16 17 18 19 20 21
    // 测试 class 选择器
    await getRootNode('.page')

    // 测试 id 选择器
    await getRootNode('#page')

    // 测试 标签 选择器
    // await getRootNode('page')
H
hdx 已提交
22
  })
23 24 25 26 27 28
  it('get-node-info', async () => {
    const btnGetNodeInfo = await page.$('.btn-get-node-info')

    await btnGetNodeInfo.tap()
    await page.waitFor(50)

29 30
    const data = await page.data()

31
    // TODO 和浏览器的计算存在差异
32 33 34
    const nodeInfo = data.nodeInfoList[0]
    expect(Math.round(nodeInfo.left)).toBe(RECT_LEFT)
    expect(Math.round(nodeInfo.width)).toBe(RECT_WIDTH)
35
    expect(Math.round(nodeInfo.height)).toBe(RECT_HEIGHT)
36 37 38 39 40 41 42 43 44
  })
  it('get-all-node-info', async () => {
    const btnGetAllNodeInfo = await page.$('.btn-get-all-node-info')

    await btnGetAllNodeInfo.tap()
    await page.waitFor(50)

    const data = await page.data()

45 46 47 48 49 50 51 52 53 54
    const nodeInfo1 = data.nodeInfoList[0]
    expect(Math.round(nodeInfo1.left)).toBe(RECT_LEFT)
    expect(nodeInfo1.top > 220).toBe(true)
    expect(Math.round(nodeInfo1.width)).toBe(RECT_WIDTH)
    expect(Math.round(nodeInfo1.height)).toBe(RECT_HEIGHT)

    const nodeInfo2 = data.nodeInfoList[1]
    expect(nodeInfo2.left > 180).toBe(true)
    expect(nodeInfo2.top > 220).toBe(true)
    expect(Math.round(nodeInfo2.width)).toBe(RECT_WIDTH)
55
    expect(Math.round(nodeInfo2.height)).toBe(RECT_HEIGHT)
56 57 58 59 60 61 62 63 64 65
  })
  if(!isMP) {
    // 小程序端启用了虚拟host,无法获取到子组件
    it('get-node-info-child', async () => {
      const child = await page.$('.node-child')
      const childData = await child.data()
      console.log('get-node-info-child.childData.top', childData.top);
      expect(childData.top > 100).toBe(true)
    })
  }
66 67 68 69 70

  it('multi-child', async () => {
    const pageData = await page.data()
    expect(pageData.selectCount).toBe(1)
    expect(pageData.selectAllCount).toBe(2)
71
  })
72 73 74

  // #ifdef APP
  //检测onResize获取BoundingClientRect信息是否有效
75
  /* it('check_resizeRectValid', async () => {
76 77
    const resizeRectValid = await page.data('resizeRectValid')
    expect(resizeRectValid).toBe(true)
78
  }) */
79 80
  // #endif

81
  it('test filelds', async () => {
雪洛's avatar
雪洛 已提交
82 83 84 85
    if (
      process.env.uniTestPlatformInfo.startsWith('web') ||
      process.env.uniTestPlatformInfo.startsWith('mp')
    ) {
86 87 88 89 90 91 92 93
      expect(true).toBe(true)
    } else {
      const pageData = await page.data()
      expect(pageData.fieldsResultContainNode).toBe(true)
    }
  })

  it('test node', async () => {
雪洛's avatar
雪洛 已提交
94 95 96 97
    if (
      process.env.uniTestPlatformInfo.startsWith('web') ||
      process.env.uniTestPlatformInfo.startsWith('mp')
    ) {
98 99 100 101 102 103
      expect(true).toBe(true)
    } else {
      const pageData = await page.data()
      expect(pageData.nodeResultContainNode).toBe(true)
    }
  })
H
hdx 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
})

async function getRootNode(selector) {
  const page = await program.currentPage()

  await page.setData({
    rootNodeInfo: null,
  })
  await page.waitFor(100)

  await page.callMethod('getRootNodeInfo', selector)
  await page.waitFor(100)

  const data = await page.data()
  expect(data.rootNodeInfo != null).toBe(true)
119
}