Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 1 | import { mount, createLocalVue, createWrapper } from '@vue/test-utils'; |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 2 | import Vue from 'vue'; |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 3 | import Vuex from 'vuex'; |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 4 | import AppHeader from '@/components/AppHeader'; |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 5 | |
| 6 | // Silencing warnings about undefined Bootsrap-vue components |
| 7 | Vue.config.silent = true; |
| 8 | const localVue = createLocalVue(); |
| 9 | localVue.use(Vuex); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 10 | |
| 11 | describe('AppHeader.vue', () => { |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 12 | const actions = { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 13 | 'global/getServerStatus': jest.fn(), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 14 | 'eventLog/getEventLogData': jest.fn(), |
Derick Montague | f833c7e | 2020-10-27 08:21:20 -0500 | [diff] [blame] | 15 | 'authentication/resetStoreState': jest.fn(), |
wangqi02 | df8cf6a | 2022-06-15 10:06:52 +0800 | [diff] [blame] | 16 | 'global/getSystemInfo': jest.fn(), |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 17 | }; |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 18 | |
Ed Tanous | 665235c | 2024-02-27 12:11:59 -0800 | [diff] [blame] | 19 | // 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 Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 28 | const wrapper = mount(AppHeader, { |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 29 | store, |
| 30 | localVue, |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 31 | mocks: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 32 | $t: (key) => key, |
| 33 | }, |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 34 | }); |
| 35 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 36 | // Reset dispatch between tests so that multiple |
| 37 | // actions are not dispatched for each test |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 38 | beforeEach(() => { |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 39 | store.dispatch = jest.fn(); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 40 | }); |
| 41 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 42 | it('should exist', () => { |
| 43 | expect(wrapper.exists()).toBe(true); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 44 | }); |
| 45 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 46 | 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 Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 56 | it('nav-trigger button click should emit toggle-navigation event', async () => { |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 57 | const rootWrapper = createWrapper(wrapper.vm.$root); |
| 58 | wrapper.get('#app-header-trigger').trigger('click'); |
| 59 | await wrapper.vm.$nextTick(); |
Sukanya Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 60 | expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 61 | }); |
| 62 | |
| 63 | it('logout button should dispatch authentication/logout', async () => { |
Derick Montague | 58a7a03 | 2020-09-21 12:21:48 -0500 | [diff] [blame] | 64 | wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click'); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 65 | 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 Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 71 | rootWrapper.vm.$emit('change-is-navigation-open', false); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 72 | await rootWrapper.vm.$nextTick(); |
| 73 | expect(wrapper.vm.isNavigationOpen).toEqual(false); |
| 74 | }); |
| 75 | |
| 76 | describe('Created lifecycle hook', () => { |
Sukanya Pandey | 4dd7eab | 2021-08-16 18:59:14 +0530 | [diff] [blame] | 77 | it('getSystemInfo should dispatch global/getSystemInfo', () => { |
| 78 | wrapper.vm.getSystemInfo(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 79 | expect(store.dispatch).toHaveBeenCalledTimes(1); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 80 | }); |
| 81 | |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 82 | it('getEvents should dispatch eventLog/getEventLogData', () => { |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 83 | wrapper.vm.getEvents(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 84 | expect(store.dispatch).toHaveBeenCalledTimes(1); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 85 | }); |
| 86 | }); |
| 87 | }); |