Part 3: Jest中對Vue組件進(jìn)行結(jié)構(gòu)和樣式測試

Test Styles and Structure of Vue.js Components in Jest

Jest中對Vue組件進(jìn)行結(jié)構(gòu)和樣式測試

vue-test-utils provide us with a set of utilities to assert on Vue.js components.
vue-test-utils給我們提供了一系列對組件進(jìn)行斷言的工具

So far, in the tests we’ve tested via Jest Snapshots. This is great, but sometimes we wanna assert something more specific.
目前為止,在測試用例中我們通過快照進(jìn)行了一些測試蜘腌。效果很棒述吸,但是我們有時候還想要對組件進(jìn)行其他更具體的斷言恩溅。

Although you can access the Vue instance via cmp.vm, you have a set of utilities at your disposal to make it easier. Let’s see what we can do.
還好姥闭,你可以通過cmp.vm斷言Vue實(shí)例褐健,這里有一系列的實(shí)用工具可以隨意使用缸血。讓我們看看它們都能干什么秉剑。

The Wrapper object

Wrapper對象

The Wrapper is the main object of vue-test-utils. It is the type returned by mount, shallow, find and findAll functions.
Wrapper對象是``vue-test-utils的主要對象,它返回自mount, shallow, find 和 findAll方法泛豪。

find and findAll

find 和 findAll方法

They accept a Selector as an argument, which can be both a CSS selector or a Vue Component.
這兩個方法接收一個選擇器作為參數(shù),比如CSS選擇器或者Vue實(shí)例都可以秃症。

So we can do things like:
他們可以幫我們做很多有意思的事情:

  let cmp = mount(MessageList)
  expect(cmp.find('.message').element).toBeInstanceOf(HTMLElement)

  // Or even call it multiple times
  let el = cmp.find('.message').find('span').element

  // Although for the previous example, we could do it in one
  let el = cmp.find('.message span').element

Asserting Structure and Style

對結(jié)構(gòu)和樣式進(jìn)行斷言

Let’s add more tests to MessageList.test.js:
現(xiàn)在在MessageList.test.js里加入以下代碼

it('is a MessageList component', () => {
  expect(cmp.is(MessageList)).toBe(true)

  // Or with CSS selector
  expect(cmp.is('ul')).toBe(true)
})

it('contains a Message component', () => {
  expect(cmp.contains(Message)).toBe(true)

  // Or with CSS selector
  expect(cmp.contains('.message')).toBe(true)
})

Here we’re using is to assert the root component type, and contains to check for sub-components existence.
以上我們測試了根組件的類型候址,還有子組件存在與否。cmp是mount()返回的Wrapper种柑,這里主要是通過將CSS選擇器和Vue實(shí)例作為參數(shù)傳遞到is()岗仑、 contains()兩個方法中進(jìn)行檢測。

We have some utils to assert the Vue instance:
我們還可以對Vue實(shí)例進(jìn)行斷言:

it('MessageList 和 Message 都是Vue實(shí)例', () => {
  expect(cmp.isVueInstance()).toBe(true)
  expect(cmp.find(Message).isVueInstance()).toBe(true)
})

Now we’re going to assert Structure in more detail, exists, isEmpty and hasAttribute comes in very handy for this:
現(xiàn)在我們試著斷言更具體的結(jié)構(gòu)聚请,exists, isEmpty 和 hasAttribute方法對此很擅長荠雕。:

it('Message element exists', () => {
  expect(cmp.find('.message').exists()).toBe(true)
})

it('Message is not empty', () => {
  expect(cmp.find(Message).isEmpty()).toBe(false)
})

it('Message has a class attribute set to "message"', () => {
  // For asserting "class", the `hasClass` method is easier
  expect(cmp.find(Message).hasAttribute('class', 'message')).toBe(true)
})

Then, we have hasClass and hasStyle to assert Styling. Let’s update the Message.vue component with a style, since hasStyle asserts only inline styles:
接下來稳其,我們用hasClass 和 hasStyle來斷言樣式。
我們在Message.vue中添加一些樣式炸卑,注意既鞠,hasStyle只能斷言行內(nèi)樣式。

<li style="padding-top: 10px" class="message">{{message}}</li>

Here the tests:
測試用例可以這么寫:

it('Message component has the .message class', () => {
  expect(cmp.find(Message).hasClass('message')).toBe(true)
})

it('Message component has style padding-top: 10', () => {
  expect(cmp.find(Message).hasStyle('padding-top', '10px')).toBe(true)
})

get methods

get類方法

As you’ve seen, we have some useful utils to assert Vue components. Most use the form of hasX, which is great, but to have a getX brings better testing experience, in terms of flexibility and debugging. So you could rewrite the following example:
我們借助一些有用的工具類方法盖文,對Vue組件進(jìn)行斷言嘱蛋。用的最多是hasX()這類方法,他們雖然很棒五续,但是getX()類方法顯然對測試體驗來說更好一些洒敏。所以你可以重構(gòu)代碼如下:

// `has` form
expect(cmp.find(Message).hasAttribute('class', 'message')).toBe(true)

// `get` form
expect(cmp.find(Message).getAttribute('class')).toBe('message')

This is under discussion and seems like it will be added to the library at some point.
此類方法都還在討論當(dāng)中,不出意外疙驾,以后會將其添加到方法庫中凶伙。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市它碎,隨后出現(xiàn)的幾起案子函荣,更是在濱河造成了極大的恐慌,老刑警劉巖扳肛,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件傻挂,死亡現(xiàn)場離奇詭異,居然都是意外死亡挖息,警方通過查閱死者的電腦和手機(jī)踊谋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來旋讹,“玉大人,你說我怎么就攤上這事轿衔〕良#” “怎么了?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵害驹,是天一觀的道長鞭呕。 經(jīng)常有香客問我,道長宛官,這世上最難降的妖魔是什么葫松? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮底洗,結(jié)果婚禮上腋么,老公的妹妹穿的比我還像新娘。我一直安慰自己亥揖,他們只是感情好珊擂,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布圣勒。 她就那樣靜靜地躺著,像睡著了一般摧扇。 火紅的嫁衣襯著肌膚如雪圣贸。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天扛稽,我揣著相機(jī)與錄音吁峻,去河邊找鬼。 笑死在张,一個胖子當(dāng)著我的面吹牛用含,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瞧掺,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼耕餐,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了辟狈?” 一聲冷哼從身側(cè)響起肠缔,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎哼转,沒想到半個月后明未,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡壹蔓,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年趟妥,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片佣蓉。...
    茶點(diǎn)故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡披摄,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出勇凭,到底是詐尸還是另有隱情疚膊,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布虾标,位于F島的核電站寓盗,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏璧函。R本人自食惡果不足惜傀蚌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望蘸吓。 院中可真熱鬧善炫,春花似錦、人聲如沸库继。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至舅桩,卻和暖如春酱虎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背擂涛。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工读串, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人撒妈。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓恢暖,卻偏偏與公主長得像,于是被迫代替她去往敵國和親狰右。 傳聞我的和親對象是個殘疾皇子杰捂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,334評論 0 10
  • 煩 魂入夢鄉(xiāng)人難走, 蒼蠅亂飛頻出手棋蚌。 最煩還是林深處嫁佳, 知了狂鳴不住口。
    往事如煙胖婆婆閱讀 835評論 12 27