xref: /openbmc/webui-vue/tests/unit/AppHeader.spec.js (revision 7d6b44cb263da09e575c7cb28cab88c1eb339c7b)
1import { mount, createWrapper } from '@vue/test-utils';
2import { createStore } from 'vuex';
3import AppHeader from '@/components/AppHeader';
4
5describe('AppHeader.vue', () => {
6  const actions = {
7    'global/getServerStatus': jest.fn(),
8    'eventLog/getEventLogData': jest.fn(),
9    'authentication/resetStoreState': jest.fn(),
10    'global/getSystemInfo': jest.fn(),
11  };
12
13  // VueX requires that all modules be present, even if they aren't used
14  // in the test, so invent a Fake auth module and install it.
15  const modules = {
16    authentication: {
17      namespaced: true,
18    },
19  };
20
21  const store = createStore({ actions, modules });
22  const wrapper = mount(AppHeader, {
23    store,
24    mocks: {
25      $t: (key) => key,
26    },
27  });
28
29  // Reset dispatch between tests so that multiple
30  // actions are not dispatched for each test
31  beforeEach(() => {
32    store.dispatch = jest.fn();
33  });
34
35  it('should exist', () => {
36    expect(wrapper.exists()).toBe(true);
37  });
38
39  it('should render correctly', () => {
40    expect(wrapper.element).toMatchSnapshot();
41  });
42
43  it('refresh button click should emit refresh event', async () => {
44    wrapper.get('#app-header-refresh').trigger('click');
45    await wrapper.vm.$nextTick();
46    expect(wrapper.emitted('refresh')).toBeTruthy();
47  });
48
49  it('nav-trigger button click should emit toggle-navigation event', async () => {
50    const rootWrapper = createWrapper(wrapper.vm.$root);
51    wrapper.get('#app-header-trigger').trigger('click');
52    await wrapper.vm.$nextTick();
53    expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
54  });
55
56  it('logout button should dispatch authentication/logout', async () => {
57    wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
58    await wrapper.vm.$nextTick();
59    expect(store.dispatch).toHaveBeenCalledTimes(1);
60  });
61
62  it('change:isNavigationOpen event should set isNavigationOpen prop to false', async () => {
63    const rootWrapper = createWrapper(wrapper.vm.$root);
64    rootWrapper.vm.$emit('change-is-navigation-open', false);
65    await rootWrapper.vm.$nextTick();
66    expect(wrapper.vm.isNavigationOpen).toEqual(false);
67  });
68
69  describe('Created lifecycle hook', () => {
70    it('getSystemInfo should dispatch global/getSystemInfo', () => {
71      wrapper.vm.getSystemInfo();
72      expect(store.dispatch).toHaveBeenCalledTimes(1);
73    });
74
75    it('getEvents should dispatch eventLog/getEventLogData', () => {
76      wrapper.vm.getEvents();
77      expect(store.dispatch).toHaveBeenCalledTimes(1);
78    });
79  });
80});
81