1import { mount, createLocalVue, createWrapper } from '@vue/test-utils';
2import Vue from 'vue';
3import Vuex from 'vuex';
4import AppHeader from '@/components/AppHeader';
5
6// Silencing warnings about undefined Bootsrap-vue components
7Vue.config.silent = true;
8const localVue = createLocalVue();
9localVue.use(Vuex);
10
11describe('AppHeader.vue', () => {
12  const actions = {
13    'global/getHostStatus': jest.fn(),
14    'eventLog/getEventLogData': jest.fn()
15  };
16
17  const store = new Vuex.Store({ actions });
18  const wrapper = mount(AppHeader, {
19    store,
20    localVue,
21    mocks: {
22      $t: key => key
23    }
24  });
25
26  // Reset dispatch between tests so that multiple
27  // actions are not dispatched for each test
28  beforeEach(() => {
29    store.dispatch = jest.fn();
30  });
31
32  it('should exist', () => {
33    expect(wrapper.exists()).toBe(true);
34  });
35
36  it('should render correctly', () => {
37    expect(wrapper.element).toMatchSnapshot();
38  });
39
40  it('refresh button click should emit refresh event', async () => {
41    wrapper.get('#app-header-refresh').trigger('click');
42    await wrapper.vm.$nextTick();
43    expect(wrapper.emitted('refresh')).toBeTruthy();
44  });
45
46  it('nav-trigger button click should emit toggle:navigation event', async () => {
47    const rootWrapper = createWrapper(wrapper.vm.$root);
48    wrapper.get('#app-header-trigger').trigger('click');
49    await wrapper.vm.$nextTick();
50    expect(rootWrapper.emitted('toggle:navigation')).toBeTruthy();
51  });
52
53  it('logout button should dispatch authentication/logout', async () => {
54    wrapper.get('#app-header-logout').trigger('click');
55    await wrapper.vm.$nextTick();
56    expect(store.dispatch).toHaveBeenCalledTimes(1);
57  });
58
59  it('change:isNavigationOpen event should set isNavigationOpen prop to false', async () => {
60    const rootWrapper = createWrapper(wrapper.vm.$root);
61    rootWrapper.vm.$emit('change:isNavigationOpen', false);
62    await rootWrapper.vm.$nextTick();
63    expect(wrapper.vm.isNavigationOpen).toEqual(false);
64  });
65
66  describe('Created lifecycle hook', () => {
67    it('getHostInfo should dispatch global/getHostStatus', () => {
68      wrapper.vm.getHostInfo();
69      expect(store.dispatch).toHaveBeenCalledTimes(1);
70    });
71
72    it('getEvents should dispatch eventLog/getEventLogData', () => {
73      wrapper.vm.getEvents();
74      expect(store.dispatch).toHaveBeenCalledTimes(1);
75    });
76  });
77});
78