Mocking a getter in Vuex + Jest | Toucan Toco
文章推薦指數: 80 %
You don't want to use the “real” getter, because that would not be a unit test ... Pizza.vue'; const localVue = createLocalVue(); localVue.use(Vuex); const ... HOME BLOG MockingagetterinVuex+Jest MockingagetterinVuex+Jest|ToucanToco NinoFiliu TableofContents Let’ssayyouwanttounittestacomponentwhichbehaviordependsonaVuexgetter. Let’susetheimaginaryexampleofaPizzacomponentwithaisVegancomputedpropertythatdependsontherecipeVuexgetter:amargheritapizzaisnotvegan,butazucchinipizzais. Youdon’twanttousethe“real”getter,becausethatwouldnotbeaunittestanymore,soyoumockit,andallgoeswellatfirst: import{createLocalVue,shallowMount}from'@vue/test-utils'; importVuexfrom'vuex'; importPizzafrom'./Pizza.vue'; constlocalVue=createLocalVue(); localVue.use(Vuex); constrecipeMock=jest.fn(); recipeMock.mockReturnValue('margherita'); conststore=newVuex.Store({ getters:{ recipe:recipeMock, }, }); constwrapper=shallowMount(Pizza,{ localVue, store, }); expect(wrapper.vm.isVegan).toBeFalsy();//pass Youmoveontotestingwhathappenswhenthegetterupdates,andwithhorror,youobservethatthetestfails: recipeMock.mockReturnValue('zucchini'); expect(wrapper.vm.isVegan).toBeTruthy();//fail Tryingtounderstandwhatishappening,youaddlogsinbetweenyourlines: console.log( wrapper.vm.$store.getters.recipe,//margherita wrapper.vm.isVegan,//false ) recipeMock.mockReturnValue('zucchini'); console.log( wrapper.vm.$store.getters.recipe,//margherita wrapper.vm.isVegan,//false ) expect(wrapper.vm.isVegan).toBeTruthy();//fail Argh,thegetterisnotupdating,eventhoughweclearlyaskthemocktoreturnsomethingelse! Why? BecauseVueisreactive:itdoesn’tupdateanythingunlessittrackedaneventthatisexplicitlychangingthestateofsomething.Sometimes,it’shiddenforconvenience,likewhenweupdatedataproperties,butthereisnosuchthinghappeninghereandVuecan’tmakethelinkbetweenthemockupdatingitsreturnvalueandthegettervalueupdating. Theonlywaytoupdatethegetteristocommitamutationthatwillimpactastatepropertythatisusedincomputingthegetter.Thatwouldlooksomethinglikethis: constlocalVue=createLocalVue(); localVue.use(Vuex); conststore=newVuex.Store({ state:{ testRecipe:'margherita', }, mutations:{ setTestRecipe(state,value){ state.testRecipe=value; }, }, getters:{ recipe(state){ returnstate.testRecipe; }, }, }); Yes,it’snotperfectbecausewe’reintroductingstatepropertiesandmutationsthatdon’tactuallyexist,butgiventherightprefixorcommenttosignalthat,it’sanacceptabletradeoff. Anditworks! constwrapper=shallowMount(Pizza,{ localVue, store, }); expect(wrapper.vm.isVegan).toBeFalsy();//pass store.commit('setTestRecipe','zucchini'); expect(wrapper.vm.isVegan).toBeTruthy();//pass TableofContents readalso 6ReasonstojoinourTechTeam readmore TalendspeaksToucanToco readmore Learnhowtoautomateyourproduct’sreleasenoteslikeabadass readmore
延伸文章資訊
- 1vuex 進階教學:Module、Getter
接續上回〈vuex 教學:vue 的狀態管理工具〉教學後,這次要延伸筆記Module、Getter 的使用,也包含較進階的用法,如果已經學完上一版的教學, ...
- 2Getter | Vuex
Vuex 允许我们在store 中定义“getter”(可以认为是store 的计算属性)。 注意. 从Vue 3.0 开始,getter 的结果不再像计算属性一样会被缓存起来。这是一个 ...
- 3那些關於Vue 的小細節- Computed 中getter 和setter 觸發的 ...
另外,在getter 中,要記得搭配使用return 來把值返回出來。 基本的寫法如下:. 預設只有getter 的computed. new Vue({ ...
- 4[Vue.js] Vuex 學習筆記(5) - getters 的核心概念- iT 邦幫忙
Getter 為什麼需要使用getters ? 在某些時候我們需要computed store` 中的state ,例如在to do list 內取得完成的數量。 computed: { do...
- 5Mocking a getter in Vuex + Jest | Toucan Toco
You don't want to use the “real” getter, because that would not be a unit test ... Pizza.vue'; co...