blob: 80460b90ad93028a58c0e23db21c9e1664a1e931 [file] [log] [blame]
Ed Tanous7d6b44c2024-03-23 14:56:34 -07001import { mount, createWrapper } from '@vue/test-utils';
2import { createStore } from 'vuex';
Sukanya Pandey98059c92020-03-26 17:10:32 +05303import AppHeader from '@/components/AppHeader';
Derick Montague68592032020-04-04 14:02:34 -05004
Sukanya Pandey98059c92020-03-26 17:10:32 +05305describe('AppHeader.vue', () => {
Derick Montague68592032020-04-04 14:02:34 -05006 const actions = {
Derick Montague71114fe2021-05-06 18:17:34 -05007 'global/getServerStatus': jest.fn(),
Derick Montague602e98a2020-10-21 16:20:00 -05008 'eventLog/getEventLogData': jest.fn(),
Derick Montaguef833c7e2020-10-27 08:21:20 -05009 'authentication/resetStoreState': jest.fn(),
wangqi02df8cf6a2022-06-15 10:06:52 +080010 'global/getSystemInfo': jest.fn(),
Derick Montague68592032020-04-04 14:02:34 -050011 };
Sukanya Pandey98059c92020-03-26 17:10:32 +053012
Ed Tanous665235c2024-02-27 12:11:59 -080013 // 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
Ed Tanous7d6b44c2024-03-23 14:56:34 -070021 const store = createStore({ actions, modules });
Derick Montaguead2ceb62020-04-24 18:11:04 -050022 const wrapper = mount(AppHeader, {
Derick Montague68592032020-04-04 14:02:34 -050023 store,
Sukanya Pandey98059c92020-03-26 17:10:32 +053024 mocks: {
Derick Montague602e98a2020-10-21 16:20:00 -050025 $t: (key) => key,
26 },
Sukanya Pandey98059c92020-03-26 17:10:32 +053027 });
28
Derick Montaguead2ceb62020-04-24 18:11:04 -050029 // Reset dispatch between tests so that multiple
30 // actions are not dispatched for each test
Sukanya Pandey98059c92020-03-26 17:10:32 +053031 beforeEach(() => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050032 store.dispatch = jest.fn();
Sukanya Pandey98059c92020-03-26 17:10:32 +053033 });
34
Derick Montaguead2ceb62020-04-24 18:11:04 -050035 it('should exist', () => {
36 expect(wrapper.exists()).toBe(true);
Sukanya Pandey98059c92020-03-26 17:10:32 +053037 });
38
Derick Montaguead2ceb62020-04-24 18:11:04 -050039 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
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053049 it('nav-trigger button click should emit toggle-navigation event', async () => {
Derick Montaguead2ceb62020-04-24 18:11:04 -050050 const rootWrapper = createWrapper(wrapper.vm.$root);
51 wrapper.get('#app-header-trigger').trigger('click');
52 await wrapper.vm.$nextTick();
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053053 expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy();
Derick Montaguead2ceb62020-04-24 18:11:04 -050054 });
55
56 it('logout button should dispatch authentication/logout', async () => {
Derick Montague58a7a032020-09-21 12:21:48 -050057 wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click');
Derick Montaguead2ceb62020-04-24 18:11:04 -050058 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);
Sukanya Pandeyedb8a772020-10-29 11:33:42 +053064 rootWrapper.vm.$emit('change-is-navigation-open', false);
Derick Montaguead2ceb62020-04-24 18:11:04 -050065 await rootWrapper.vm.$nextTick();
66 expect(wrapper.vm.isNavigationOpen).toEqual(false);
67 });
68
69 describe('Created lifecycle hook', () => {
Sukanya Pandey4dd7eab2021-08-16 18:59:14 +053070 it('getSystemInfo should dispatch global/getSystemInfo', () => {
71 wrapper.vm.getSystemInfo();
Derick Montaguead2ceb62020-04-24 18:11:04 -050072 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053073 });
74
Derick Montague68592032020-04-04 14:02:34 -050075 it('getEvents should dispatch eventLog/getEventLogData', () => {
Sukanya Pandey98059c92020-03-26 17:10:32 +053076 wrapper.vm.getEvents();
Derick Montaguead2ceb62020-04-24 18:11:04 -050077 expect(store.dispatch).toHaveBeenCalledTimes(1);
Sukanya Pandey98059c92020-03-26 17:10:32 +053078 });
79 });
80});