Ed Tanous | 7d6b44c | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 1 | import { mount, createWrapper } from '@vue/test-utils'; |
| 2 | import { createStore } from 'vuex'; |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 3 | import AppHeader from '@/components/AppHeader'; |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 4 | |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 5 | describe('AppHeader.vue', () => { |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 6 | const actions = { |
Derick Montague | 71114fe | 2021-05-06 18:17:34 -0500 | [diff] [blame] | 7 | 'global/getServerStatus': jest.fn(), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 8 | 'eventLog/getEventLogData': jest.fn(), |
Derick Montague | f833c7e | 2020-10-27 08:21:20 -0500 | [diff] [blame] | 9 | 'authentication/resetStoreState': jest.fn(), |
wangqi02 | df8cf6a | 2022-06-15 10:06:52 +0800 | [diff] [blame] | 10 | 'global/getSystemInfo': jest.fn(), |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 11 | }; |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 12 | |
Ed Tanous | 665235c | 2024-02-27 12:11:59 -0800 | [diff] [blame] | 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 | |
Ed Tanous | 7d6b44c | 2024-03-23 14:56:34 -0700 | [diff] [blame] | 21 | const store = createStore({ actions, modules }); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 22 | const wrapper = mount(AppHeader, { |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 23 | store, |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 24 | mocks: { |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame] | 25 | $t: (key) => key, |
| 26 | }, |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 27 | }); |
| 28 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 29 | // Reset dispatch between tests so that multiple |
| 30 | // actions are not dispatched for each test |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 31 | beforeEach(() => { |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 32 | store.dispatch = jest.fn(); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 33 | }); |
| 34 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 35 | it('should exist', () => { |
| 36 | expect(wrapper.exists()).toBe(true); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 37 | }); |
| 38 | |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 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 | |
Sukanya Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 49 | it('nav-trigger button click should emit toggle-navigation event', async () => { |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 50 | const rootWrapper = createWrapper(wrapper.vm.$root); |
| 51 | wrapper.get('#app-header-trigger').trigger('click'); |
| 52 | await wrapper.vm.$nextTick(); |
Sukanya Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 53 | expect(rootWrapper.emitted('toggle-navigation')).toBeTruthy(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 54 | }); |
| 55 | |
| 56 | it('logout button should dispatch authentication/logout', async () => { |
Derick Montague | 58a7a03 | 2020-09-21 12:21:48 -0500 | [diff] [blame] | 57 | wrapper.get('[data-test-id="appHeader-link-logout"]').trigger('click'); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 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); |
Sukanya Pandey | edb8a77 | 2020-10-29 11:33:42 +0530 | [diff] [blame] | 64 | rootWrapper.vm.$emit('change-is-navigation-open', false); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 65 | await rootWrapper.vm.$nextTick(); |
| 66 | expect(wrapper.vm.isNavigationOpen).toEqual(false); |
| 67 | }); |
| 68 | |
| 69 | describe('Created lifecycle hook', () => { |
Sukanya Pandey | 4dd7eab | 2021-08-16 18:59:14 +0530 | [diff] [blame] | 70 | it('getSystemInfo should dispatch global/getSystemInfo', () => { |
| 71 | wrapper.vm.getSystemInfo(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 72 | expect(store.dispatch).toHaveBeenCalledTimes(1); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 73 | }); |
| 74 | |
Derick Montague | 6859203 | 2020-04-04 14:02:34 -0500 | [diff] [blame] | 75 | it('getEvents should dispatch eventLog/getEventLogData', () => { |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 76 | wrapper.vm.getEvents(); |
Derick Montague | ad2ceb6 | 2020-04-24 18:11:04 -0500 | [diff] [blame] | 77 | expect(store.dispatch).toHaveBeenCalledTimes(1); |
Sukanya Pandey | 98059c9 | 2020-03-26 17:10:32 +0530 | [diff] [blame] | 78 | }); |
| 79 | }); |
| 80 | }); |