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/getServerStatus': jest.fn(),
14    'eventLog/getEventLogData': jest.fn(),
15    'authentication/resetStoreState': jest.fn(),
16    'global/getSystemInfo': jest.fn(),
17  };
18
19  const store = new Vuex.Store({ actions });
20  const wrapper = mount(AppHeader, {
21    store,
22    localVue,
23    mocks: {
24      $t: (key) => key,
25    },
26  });
27
28  // Reset dispatch between tests so that multiple
29  // actions are not dispatched for each test
30  beforeEach(() => {
31    store.dispatch = jest.fn();
32  });
33
34  it('should exist', () => {
35    expect(wrapper.exists()).toBe(true);
36  });
37
38  it('should render correctly', () => {
39    expect(wrapper.element).toMatchSnapshot();
40  });
41
42  it('refresh button click should emit refresh event', async () => {
43    wrapper.get('#app-header-refresh').trigger('click');
44    await wrapper.vm.$nextTick();
45    expect(wrapper.emitted('refresh')).toBeTruthy();
46  });
47
48  it('nav-trigger button click should emit toggle-navigation event', async () => {
49    const rootWrapper = createWrapper(wrapper.vm.$root);
50    wrapper.get('#app-header-trigger').trigger('click');
51    await wrapper.vm.$nextTick();
52    expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
53  });
54
55  it('logout button should dispatch authentication/logout', async () => {
56    wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
57    await wrapper.vm.$nextTick();
58    expect(store.dispatch).toHaveBeenCalledTimes(1);
59  });
60
61  it('change:isNavigationOpen event should set isNavigationOpen prop to false', async () => {
62    const rootWrapper = createWrapper(wrapper.vm.$root);
63    rootWrapper.vm.$emit('change-is-navigation-open', false);
64    await rootWrapper.vm.$nextTick();
65    expect(wrapper.vm.isNavigationOpen).toEqual(false);
66  });
67
68  describe('Created lifecycle hook', () => {
69    it('getSystemInfo should dispatch global/getSystemInfo', () => {
70      wrapper.vm.getSystemInfo();
71      expect(store.dispatch).toHaveBeenCalledTimes(1);
72    });
73
74    it('getEvents should dispatch eventLog/getEventLogData', () => {
75      wrapper.vm.getEvents();
76      expect(store.dispatch).toHaveBeenCalledTimes(1);
77    });
78  });
79});
80