iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 5
1
Modern Web

Vue 怎麼寫測試系列 第 5

Day 5. 冬至吃一碗湯圓,配一晚測試

  • 分享至 

  • twitterImage
  •  

我的 IDE 改用 webstorm 了
因為 phpstorm 還不支援 jest

上次說到有個 bug, 如果是空字串,還是可以爽爽的加入 todo list
這次就來補這個洞。

首先,先寫好測試。

it ('never add empty item', () => {
    wrapper.setData({
      newItem: '',
      items: []
    })

    const button = wrapper.find('button')
    button.trigger('click')

    expect(wrapper.vm.items).toBe([]);
})

然後確認測試會爆掉

https://ithelp.ithome.com.tw/upload/images/20171223/20104476dYmr16PH0n.png

然後修一下洞,直到測試過

addItemToList() {
    if( this.newItem === '' )
        return;

    this.items.push( this.newItem );
    this.newItem = '';
}

之後,看一下測試的 code,處理重複的地方

it ('never add empty item', () => {
    wrapper.setData({
      newItem: '',
      items: []
    })

    // 這邊
    const button = wrapper.find('button')
    button.trigger('click')

    expect(wrapper.vm.items).toEqual([]);
})

it ('should add item to list on click', () => {

    // 和這邊重複了
    wrapper.vm.newItem = 'mos'
    const button = wrapper.find('button')
    button.trigger('click')

    expect(wrapper.vm.items).toContain('mos')
})

換成這種

const addItem = (item) => {
    wrapper.vm.newItem = item
    const button = wrapper.find('button')
    button.trigger('click')
}

it ('never add empty item', () => {
    wrapper.setData({
      newItem: '',
      items: []
    })

    addItem('');
    expect(wrapper.vm.items).toEqual([]);
})

it ('should add item to list on click', () => {
    addItem("mos");
    expect(wrapper.vm.items).toContain('mos')
})

打完收工。


上一篇
Day 4. 只要你目標明確,關注細節,執行力強,那你就得加班了
下一篇
Day 6. 是條狀的都行,管它香蕉還芭蕉
系列文
Vue 怎麼寫測試30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言