event-bus.test.js 2.7 KB
Newer Older
Q
qiang 已提交
1 2 3 4 5 6
const PAGE_PATH = '/pages/API/event-bus/event-bus'

describe('event-bus', () => {
  let page
  beforeAll(async () => {
    page = await program.reLaunch(PAGE_PATH)
7
    await page.waitFor('view')
Q
qiang 已提交
8 9 10 11 12 13 14 15 16 17 18 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 52
  })

  it('on', async () => {
    await page.callMethod('clear')
    await page.callMethod('on')
    await page.callMethod('emit')
    const l1 = (await page.data()).log.length
    expect(l1).toBe(1)
    await page.callMethod('clear')
    await page.callMethod('emit')
    const l2 = (await page.data()).log.length
    expect(l2).toBe(1)
    await page.callMethod('clear')
    await page.callMethod('on')
    await page.callMethod('emit')
    const l3 = (await page.data()).log.length
    expect(l3).toBe(2)
    await page.callMethod('clear')
    await page.callMethod('off')
    await page.callMethod('emit')
    const l4 = (await page.data()).log.length
    expect(l4).toBe(1)
    await page.callMethod('clear')
    await page.callMethod('off')
    await page.callMethod('emit')
    const l5 = (await page.data()).log.length
    expect(l5).toBe(0)
  })

  it('once', async () => {
    await page.callMethod('clear')
    await page.callMethod('once')
    await page.callMethod('emit')
    const l1 = (await page.data()).log.length
    expect(l1).toBe(1)
    await page.callMethod('clear')
    await page.callMethod('emit')
    const l2 = (await page.data()).log.length
    expect(l2).toBe(0)
    await page.callMethod('clear')
    await page.callMethod('once')
    await page.callMethod('off')
    await page.callMethod('emit')
    const l3 = (await page.data()).log.length
    expect(l3).toBe(0)
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
  })

  it('emit object params', async () => {
    await page.callMethod('onObj')
    await page.callMethod('emitWithObj')
    const objArg = await page.data('objArg')
    expect(objArg.a).toBe(1)
    expect(objArg.b).toBe(2)
  })

  it('off-all', async () => {
    await page.callMethod('clear')
    await page.callMethod('on')
    await page.callMethod('on2')
    await page.callMethod('emit')
    const l1 = (await page.data()).log.length
    expect(l1).toBe(2)

    await page.callMethod('clear')
    const l2 = (await page.data()).log.length
    expect(l2).toBe(0)

    await page.callMethod('offAll')
    await page.callMethod('emit')
    const l3 = (await page.data()).log.length
    expect(l3).toBe(0)
79
  })
80 81 82 83 84 85 86 87 88
  it('test return id', async () => {
    await page.callMethod('clear')
    expect((await page.data()).log.length).toBe(0)
    await page.callMethod('testReturnId')
    const logs = await page.data('log')
    expect(logs.length).toBe(2)
    expect(logs[0]).toBe('触发 test-return-id $on fn')
    expect(logs[1]).toBe('触发 test-return-id $once fn')
  })
Q
qiang 已提交
89
})