github.com/covergates/covergates@v0.2.2-0.20201009050117-42ef8a19fb95/web/src/components/__tests__/AccountButton.spec.ts (about) 1 import Vue from 'vue'; 2 import Vuetify from 'vuetify'; 3 import Vuex, { Store } from 'vuex'; 4 import { createLocalVue, shallowMount, Wrapper } from '@vue/test-utils'; 5 import { cloneDeep } from 'lodash'; 6 import { storeConfig, RootState, Mutations } from '@/store'; 7 import AccountButton from '@/components/AccountButton.vue'; 8 9 Vue.use(Vuetify); 10 11 function findActions<T extends Vue>(wrapper: Wrapper<T>): Wrapper<Vue>[] { 12 const actions = wrapper.findAll('v-list-item-icon-stub'); 13 const icons = []; 14 for (const action of actions.wrappers) { 15 const w = action.find('v-icon-stub'); 16 icons.push(w); 17 } 18 return icons; 19 } 20 21 describe('AccountButton.vue', () => { 22 console.warn = jest.fn(); 23 const localVue = createLocalVue(); 24 let vuetify: typeof Vuetify; 25 let store: Store<RootState>; 26 beforeEach(() => { 27 vuetify = new Vuetify(); 28 store = new Vuex.Store(cloneDeep(storeConfig)); 29 }); 30 localVue.use(Vuetify); 31 localVue.use(Vuex); 32 33 it('show login button when user undefined', () => { 34 const wrapper = shallowMount(AccountButton, { 35 vuetify, store, stubs: ['router-link'] 36 }) as Wrapper<AccountButton & { user?: User }>; 37 expect(wrapper.vm.user).toBeUndefined(); 38 const actions = findActions(wrapper); 39 const icons = actions.map(action => { return action.text(); }); 40 expect(actions.length).toBeGreaterThanOrEqual(1); 41 expect(icons).toContain('mdi-login'); 42 expect(icons).not.toContain('mdi-logout'); 43 }); 44 45 it('show logout button when user defined', async () => { 46 const $router = { 47 push: jest.fn() 48 }; 49 const wrapper = shallowMount(AccountButton, { 50 vuetify, 51 store, 52 stubs: ['router-line'], 53 mocks: { 54 $router 55 } 56 }) as Wrapper<AccountButton & { user?: User }>; 57 globalThis.window = Object.create(window); 58 Object.defineProperty(window, 'location', { 59 value: { 60 href: '' 61 } 62 }); 63 expect(wrapper.vm.user).toBeUndefined(); 64 store.commit(Mutations.UPDATE_USER, {} as User); 65 expect(wrapper.vm.user).toBeDefined(); 66 await wrapper.vm.$nextTick(); 67 const actions = findActions(wrapper); 68 const icons = actions.map(action => { return action.text(); }); 69 expect(icons).toContain('mdi-logout'); 70 const index = Array.from(actions.keys()).find(i => { return icons[i] === 'mdi-logout'; }); 71 if (index !== undefined) { 72 wrapper.findAll('v-list-item-stub').at(index).vm.$emit('click'); 73 } 74 // expect($router.push).toHaveBeenCalledWith('/logoff'); 75 expect(window.location.href).toMatch('/logoff'); 76 }); 77 });