blob: 2d17e768ba505a83cc643fe158766fc5c761997c [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
Ed Tanous665235c2024-02-27 12:11:59 -080019 // VueX requires that all modules be present, even if they aren't used
20 // in the test, so invent a Fake auth module and install it.
21 const modules = {
22 authentication: {
23 namespaced: true,
24 },
25 };
26
27 const store = new Vuex.Store({ actions, modules });
Derick Montaguead2ceb62020-04-24 18:11:04 -050028 const wrapper = mount(AppHeader, {
Derick Montague68592032020-04-04 14:02:34 -050029 store,
30 localVue,
Sukanya Pandey98059c92020-03-26 17:10:32 +053031 mocks: {
Derick Montague602e98a2020-10-21 16:20:00 -050032 $t: (key) => key,
33 },
Sukanya Pandey98059c92020-03-26 17:10:32 +053034 });
35
Derick Montaguead2ceb62020-04-24 18:11:04 -050036 // Reset dispatch between tests so that multiple
37 // actions are not dispatched for each test
Sukanya Pandey98059c92020-03-26 17:10:32 +053038 beforeEach(() => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050039 store.dispatch = jest.fn();
Sukanya Pandey98059c92020-03-26 17:10:32 +053040 });
41
Derick Montaguead2ceb62020-04-24 18:11:04 -050042 it('should exist', () => {
43 expect(wrapper.exists()).toBe(true);
Sukanya Pandey98059c92020-03-26 17:10:32 +053044 });
45
Derick Montaguead2ceb62020-04-24 18:11:04 -050046 it('should render correctly', () => {
47 expect(wrapper.element).toMatchSnapshot();
48 });
49
50 it('refresh button click should emit refresh event', async () => {
51 wrapper.get('#app-header-refresh').trigger('click');
52 await wrapper.vm.$nextTick();
53 expect(wrapper.emitted('refresh')).toBeTruthy();
54 });
55
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053056 it('nav-trigger button click should emit toggle-navigation event', async () => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050057 const rootWrapper = createWrapper(wrapper.vm.$root);
58 wrapper.get('#app-header-trigger').trigger('click');
59 await wrapper.vm.$nextTick();
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053060 expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
Derick Montaguead2ceb62020-04-24 18:11:04 -050061 });
62
63 it('logout button should dispatch authentication/logout', async () => {
Derick Montague58a7a032020-09-21 12:21:48 -050064 wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
Derick Montaguead2ceb62020-04-24 18:11:04 -050065 await wrapper.vm.$nextTick();
66 expect(store.dispatch).toHaveBeenCalledTimes(1);
67 });
68
69 it('change:isNavigationOpen event should set isNavigationOpen prop to false', async () => {
70 const rootWrapper = createWrapper(wrapper.vm.$root);
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053071 rootWrapper.vm.$emit('change-is-navigation-open', false);
Derick Montaguead2ceb62020-04-24 18:11:04 -050072 await rootWrapper.vm.$nextTick();
73 expect(wrapper.vm.isNavigationOpen).toEqual(false);
74 });
75
76 describe('Created lifecycle hook', () => {
Sukanya Pandey4dd7eab2021-08-16 18:59:14 +053077 it('getSystemInfo should dispatch global/getSystemInfo', () => {
78 wrapper.vm.getSystemInfo();
Derick Montaguead2ceb62020-04-24 18:11:04 -050079 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053080 });
81
Derick Montague68592032020-04-04 14:02:34 -050082 it('getEvents should dispatch eventLog/getEventLogData', () => {
Sukanya Pandey98059c92020-03-26 17:10:32 +053083 wrapper.vm.getEvents();
Derick Montaguead2ceb62020-04-24 18:11:04 -050084 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053085 });
86 });
87});