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