blob: 1a4075f4cff6792e85706da606fc00956b593537 [file] [log] [blame]
Derick Montaguead2ceb62020-04-24 18:11:04 -05001import { mount, createLocalVue, createWrapper } from '@vue/test-utils';
Sukanya Pandey98059c92020-03-26 17:10:32 +05302import Vue from 'vue';
Derick Montague68592032020-04-04 14:02:34 -05003import Vuex from 'vuex';
Sukanya Pandey98059c92020-03-26 17:10:32 +05304import AppHeader from '@/components/AppHeader';
Derick Montague68592032020-04-04 14:02:34 -05005
6// Silencing warnings about undefined Bootsrap-vue components
7Vue.config.silent = true;
8const localVue = createLocalVue();
9localVue.use(Vuex);
Sukanya Pandey98059c92020-03-26 17:10:32 +053010
11describe('AppHeader.vue', () => {
Derick Montague68592032020-04-04 14:02:34 -050012 const actions = {
Derick Montaguead2ceb62020-04-24 18:11:04 -050013 'global/getHostStatus': jest.fn(),
Derick Montague602e98a2020-10-21 16:20:00 -050014 'eventLog/getEventLogData': jest.fn(),
Derick Montaguef833c7e2020-10-27 08:21:20 -050015 'authentication/resetStoreState': jest.fn(),
Derick Montague68592032020-04-04 14:02:34 -050016 };
Sukanya Pandey98059c92020-03-26 17:10:32 +053017
Derick Montague68592032020-04-04 14:02:34 -050018 const store = new Vuex.Store({ actions });
Derick Montaguead2ceb62020-04-24 18:11:04 -050019 const wrapper = mount(AppHeader, {
Derick Montague68592032020-04-04 14:02:34 -050020 store,
21 localVue,
Sukanya Pandey98059c92020-03-26 17:10:32 +053022 mocks: {
Derick Montague602e98a2020-10-21 16:20:00 -050023 $t: (key) => key,
24 },
Sukanya Pandey98059c92020-03-26 17:10:32 +053025 });
26
Derick Montaguead2ceb62020-04-24 18:11:04 -050027 // Reset dispatch between tests so that multiple
28 // actions are not dispatched for each test
Sukanya Pandey98059c92020-03-26 17:10:32 +053029 beforeEach(() => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050030 store.dispatch = jest.fn();
Sukanya Pandey98059c92020-03-26 17:10:32 +053031 });
32
Derick Montaguead2ceb62020-04-24 18:11:04 -050033 it('should exist', () => {
34 expect(wrapper.exists()).toBe(true);
Sukanya Pandey98059c92020-03-26 17:10:32 +053035 });
36
Derick Montaguead2ceb62020-04-24 18:11:04 -050037 it('should render correctly', () => {
38 expect(wrapper.element).toMatchSnapshot();
39 });
40
41 it('refresh button click should emit refresh event', async () => {
42 wrapper.get('#app-header-refresh').trigger('click');
43 await wrapper.vm.$nextTick();
44 expect(wrapper.emitted('refresh')).toBeTruthy();
45 });
46
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053047 it('nav-trigger button click should emit toggle-navigation event', async () => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050048 const rootWrapper = createWrapper(wrapper.vm.$root);
49 wrapper.get('#app-header-trigger').trigger('click');
50 await wrapper.vm.$nextTick();
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053051 expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
Derick Montaguead2ceb62020-04-24 18:11:04 -050052 });
53
54 it('logout button should dispatch authentication/logout', async () => {
Derick Montague58a7a032020-09-21 12:21:48 -050055 wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
Derick Montaguead2ceb62020-04-24 18:11:04 -050056 await wrapper.vm.$nextTick();
57 expect(store.dispatch).toHaveBeenCalledTimes(1);
58 });
59
60 it('change:isNavigationOpen event should set isNavigationOpen prop to false', async () => {
61 const rootWrapper = createWrapper(wrapper.vm.$root);
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053062 rootWrapper.vm.$emit('change-is-navigation-open', false);
Derick Montaguead2ceb62020-04-24 18:11:04 -050063 await rootWrapper.vm.$nextTick();
64 expect(wrapper.vm.isNavigationOpen).toEqual(false);
65 });
66
67 describe('Created lifecycle hook', () => {
Derick Montague68592032020-04-04 14:02:34 -050068 it('getHostInfo should dispatch global/getHostStatus', () => {
Sukanya Pandey98059c92020-03-26 17:10:32 +053069 wrapper.vm.getHostInfo();
Derick Montaguead2ceb62020-04-24 18:11:04 -050070 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053071 });
72
Derick Montague68592032020-04-04 14:02:34 -050073 it('getEvents should dispatch eventLog/getEventLogData', () => {
Sukanya Pandey98059c92020-03-26 17:10:32 +053074 wrapper.vm.getEvents();
Derick Montaguead2ceb62020-04-24 18:11:04 -050075 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053076 });
77 });
78});