checkbox.test.js 3.5 KB
Newer Older
1 2 3 4 5 6 7 8
function getData(key = '') {
  return new Promise(async (resolve, reject) => {
    const data = await page.data()
    resolve(key ? data[key] : data)
  })
}

let page
9 10
let originEventCallbackNum

11 12
beforeAll(async () => {
  page = await program.reLaunch('/pages/component/checkbox/checkbox')
13
  await page.waitFor(2000)
14 15 16
})

describe('Checkbox.uvue', () => {
雪洛's avatar
雪洛 已提交
17
  const isMP = process.env.uniTestPlatformInfo.startsWith('mp')
18 19 20 21
  it('change', async () => {
    expect(await getData('value')).toEqual([])
    const cb1 = await page.$('.cb1')
    await cb1.tap()
雪洛's avatar
雪洛 已提交
22
    await page.waitFor(100)
23 24 25
    expect(await getData('value')).toEqual(['cb', 'cb1'])
    const cb = await page.$('.cb')
    await cb.tap()
雪洛's avatar
雪洛 已提交
26
    await page.waitFor(100)
27 28 29
    expect(await getData('value')).toEqual(['cb1'])
    const cb2 = await page.$('.cb2')
    await cb2.tap()
雪洛's avatar
雪洛 已提交
30
    await page.waitFor(100)
31 32
    expect(await getData('value')).toEqual(['cb1'])
    await cb1.tap()
雪洛's avatar
雪洛 已提交
33
    await page.waitFor(100)
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    expect(await getData('value')).toEqual([])
  })
  it('length', async () => {
    const checkboxGroupElements = await page.$$('.checkbox-group')
    expect(checkboxGroupElements.length).toBe(3)
    const checkboxElements = await page.$$('.checkbox')
    expect(checkboxElements.length).toBe(12)
  })
  it('text', async () => {
    const cb = await page.$('.cb1')
    expect(await cb.text()).toEqual('未选中')
    await page.setData({
      text: 'not selected',
    })
    expect(await cb.text()).toEqual('not selected')
  })
雪洛's avatar
雪洛 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
  if(isMP) {
    it('disabled', async () => {
      const cb = await page.$('.cb2')
      const disabled1 = await cb.property('disabled')
      expect(disabled1).toBe(true)
      await page.setData({
        disabled: false,
      })
      const disabled2 = await cb.property('disabled')
      expect(disabled2).toBe(false)
    })
  } else {
    it('disabled', async () => {
      const cb = await page.$('.cb2')
      const disabled1 = await cb.attribute('disabled')
      expect(disabled1).toBe(true + '')
      await page.setData({
        disabled: false,
      })
      const disabled2 = await cb.attribute('disabled')
      expect(disabled2).toBe(false + '')
    })
  }
73 74
  it('checked', async () => {
    const cb = await page.$('.cb')
H
hdx 已提交
75 76 77
    // TODO
    const newValue1 = await cb.property('checked')
    expect(newValue1.toString()).toBe(true + '')
78 79 80
    await page.setData({
      checked: false,
    })
H
hdx 已提交
81 82 83
    // TODO
    const newValue2 = await cb.property('checked')
    expect(newValue2.toString()).toBe(false + '')
84
  })
雪洛's avatar
雪洛 已提交
85 86 87 88 89 90 91 92
  if(!isMP) {
    it('color', async () => {
      const cb = await page.$('.cb')
      expect(await cb.attribute('color')).toBe('#007aff')
      await page.setData({
        color: '#63acfc',
      })
      expect(await cb.attribute('color')).toBe('#63acfc')
93
    })
94

雪洛's avatar
雪洛 已提交
95 96 97 98 99 100 101
    it('icon color', async () => {
      const cb = await page.$('.cb')
      expect(await cb.attribute('iconColor')).toBe('#211cfe')
      await page.setData({
        iconColor: '#63acfc',
      })
      expect(await cb.attribute('iconColor')).toBe('#63acfc')
102
    })
雪洛's avatar
雪洛 已提交
103 104 105 106 107 108 109
    it('foreColor', async () => {
      const cb = await page.$('.cb')
      expect(await cb.attribute('foreColor')).toBe('#ff0000')
      await page.setData({
        foreColor: '#63acfe',
      })
      expect(await cb.attribute('foreColor')).toBe('#63acfe')
110
    })
雪洛's avatar
雪洛 已提交
111 112 113 114 115 116
    it('trigger UniCheckboxGroupChangeEvent', async () => {
      const element = await page.$('.checkbox-item-0')
      await element.tap()
      await page.waitFor(1000)
      const { testEvent } = await page.data()
      expect(testEvent).toBe(true)
117
    })
雪洛's avatar
雪洛 已提交
118
  }
119
})