blob: ed78f03387224ce2df4b541358108a91d9ec119f [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 Montague71114fe2021-05-06 18:17:34 -050013 'global/getServerStatus': 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(),
wangqi02df8cf6a2022-06-15 10:06:52 +080016 'global/getSystemInfo': jest.fn(),
Derick Montague68592032020-04-04 14:02:34 -050017 };
Sukanya Pandey98059c92020-03-26 17:10:32 +053018
Derick Montague68592032020-04-04 14:02:34 -050019 const store = new Vuex.Store({ actions });
Derick Montaguead2ceb62020-04-24 18:11:04 -050020 const wrapper = mount(AppHeader, {
Derick Montague68592032020-04-04 14:02:34 -050021 store,
22 localVue,
Sukanya Pandey98059c92020-03-26 17:10:32 +053023 mocks: {
Derick Montague602e98a2020-10-21 16:20:00 -050024 $t: (key) => key,
25 },
Sukanya Pandey98059c92020-03-26 17:10:32 +053026 });
27
Derick Montaguead2ceb62020-04-24 18:11:04 -050028 // Reset dispatch between tests so that multiple
29 // actions are not dispatched for each test
Sukanya Pandey98059c92020-03-26 17:10:32 +053030 beforeEach(() => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050031 store.dispatch = jest.fn();
Sukanya Pandey98059c92020-03-26 17:10:32 +053032 });
33
Derick Montaguead2ceb62020-04-24 18:11:04 -050034 it('should exist', () => {
35 expect(wrapper.exists()).toBe(true);
Sukanya Pandey98059c92020-03-26 17:10:32 +053036 });
37
Derick Montaguead2ceb62020-04-24 18:11:04 -050038 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
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053048 it('nav-trigger button click should emit toggle-navigation event', async () => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050049 const rootWrapper = createWrapper(wrapper.vm.$root);
50 wrapper.get('#app-header-trigger').trigger('click');
51 await wrapper.vm.$nextTick();
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053052 expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
Derick Montaguead2ceb62020-04-24 18:11:04 -050053 });
54
55 it('logout button should dispatch authentication/logout', async () => {
Derick Montague58a7a032020-09-21 12:21:48 -050056 wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
Derick Montaguead2ceb62020-04-24 18:11:04 -050057 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);
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053063 rootWrapper.vm.$emit('change-is-navigation-open', false);
Derick Montaguead2ceb62020-04-24 18:11:04 -050064 await rootWrapper.vm.$nextTick();
65 expect(wrapper.vm.isNavigationOpen).toEqual(false);
66 });
67
68 describe('Created lifecycle hook', () => {
Sukanya Pandey4dd7eab2021-08-16 18:59:14 +053069 it('getSystemInfo should dispatch global/getSystemInfo', () => {
70 wrapper.vm.getSystemInfo();
Derick Montaguead2ceb62020-04-24 18:11:04 -050071 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053072 });
73
Derick Montague68592032020-04-04 14:02:34 -050074 it('getEvents should dispatch eventLog/getEventLogData', () => {
Sukanya Pandey98059c92020-03-26 17:10:32 +053075 wrapper.vm.getEvents();
Derick Montaguead2ceb62020-04-24 18:11:04 -050076 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053077 });
78 });
79});