checkbox.test.js 2.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 14 15 16 17 18
  await page.waitFor(2000)
  originEventCallbackNum = await page.callMethod('getEventCallbackNum')
})

beforeEach(async () => {
  await page.callMethod('setEventCallbackNum', 0)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
})

describe('Checkbox.uvue', () => {
  it('change', async () => {
    expect(await getData('value')).toEqual([])
    const cb1 = await page.$('.cb1')
    await cb1.tap()
    expect(await getData('value')).toEqual(['cb', 'cb1'])
    const cb = await page.$('.cb')
    await cb.tap()
    expect(await getData('value')).toEqual(['cb1'])
    const cb2 = await page.$('.cb2')
    await cb2.tap()
    expect(await getData('value')).toEqual(['cb1'])
    await cb1.tap()
    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')
  })
  it('checked', async () => {
    const cb = await page.$('.cb')
H
hdx 已提交
52 53 54
    // TODO
    const newValue1 = await cb.property('checked')
    expect(newValue1.toString()).toBe(true + '')
55 56 57
    await page.setData({
      checked: false,
    })
H
hdx 已提交
58 59 60
    // TODO
    const newValue2 = await cb.property('checked')
    expect(newValue2.toString()).toBe(false + '')
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  })
  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')
  })
  it('disabled', async () => {
    const cb = await page.$('.cb2')
    expect(await cb.attribute('disabled')).toBe(true + '')
    await page.setData({
      disabled: false,
    })
    expect(await cb.attribute('disabled')).toBe(false + '')
  })
78 79 80 81 82 83
  it('trigger UniCheckboxGroupChangeEvent', async () => {
    const element = await page.$('.checkbox-item-0')
    await element.tap()
    const eventCallbackNum = await page.callMethod('getEventCallbackNum')
    expect(eventCallbackNum - originEventCallbackNum).toBe(3)
  })
84
})