|
| 1 | +import { render, waitFor } from '@testing-library/vue'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import { createLocalVue } from '@vue/test-utils'; |
| 5 | +import { PiniaVuePlugin } from 'pinia'; |
| 6 | +import { createTestingPinia } from '@pinia/testing'; |
| 7 | +import { ref } from '@nuxtjs/composition-api'; |
| 8 | +import { useUser } from '~/modules/customer/composables/useUser'; |
| 9 | +import { useUiState } from '~/composables'; |
| 10 | +import { useUiStateMock, useUserMock } from '~/test-utils'; |
| 11 | + |
| 12 | +import { useCategoryStore } from '~/modules/catalog/category/stores/category'; |
| 13 | +import BottomNavigation from '../BottomNavigation.vue'; |
| 14 | + |
| 15 | +jest.mock('~/composables/useUiState', () => ({ |
| 16 | + useUiState: jest.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +jest.mock('~/modules/customer/composables/useUser', () => ({ |
| 20 | + useUser: jest.fn(), |
| 21 | +})); |
| 22 | + |
| 23 | +(useUser as jest.Mock).mockReturnValue(useUserMock()); |
| 24 | +(useUiState as jest.Mock).mockReturnValue(useUiStateMock()); |
| 25 | + |
| 26 | +const localVue = createLocalVue(); |
| 27 | +localVue.use(PiniaVuePlugin); |
| 28 | + |
| 29 | +describe('BottomNavigation', () => { |
| 30 | + it('handles "Home" button click', async () => { |
| 31 | + const routerPushMock = jest.fn(); |
| 32 | + |
| 33 | + const { getByTestId } = render( |
| 34 | + BottomNavigation, |
| 35 | + { |
| 36 | + mocks: { |
| 37 | + $router: { |
| 38 | + push: routerPushMock, |
| 39 | + }, |
| 40 | + $route: { |
| 41 | + name: 'home', |
| 42 | + }, |
| 43 | + }, |
| 44 | + localVue, |
| 45 | + pinia: createTestingPinia(), |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + const homeButton = getByTestId('bottom-navigation-home'); |
| 50 | + userEvent.click(homeButton); |
| 51 | + await waitFor(() => { |
| 52 | + expect(routerPushMock).toHaveBeenCalledWith({ name: 'home' }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + it('handles "Menu" (categories) button click', async () => { |
| 56 | + const { getByTestId } = render( |
| 57 | + BottomNavigation, |
| 58 | + { |
| 59 | + mocks: { |
| 60 | + $route: { |
| 61 | + name: 'home', |
| 62 | + }, |
| 63 | + }, |
| 64 | + localVue, |
| 65 | + pinia: createTestingPinia(), |
| 66 | + }, |
| 67 | + ); |
| 68 | + |
| 69 | + const categoryStore = useCategoryStore(); |
| 70 | + |
| 71 | + const menuButton = getByTestId('bottom-navigation-menu'); |
| 72 | + userEvent.click(menuButton); |
| 73 | + await waitFor(() => { |
| 74 | + expect(categoryStore.load).toHaveBeenCalled(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + it('handles "Wishlist" button click', async () => { |
| 78 | + const useUiStateMockInstance = useUiStateMock(); |
| 79 | + (useUiState as jest.Mock).mockReturnValueOnce(useUiStateMockInstance); |
| 80 | + const useUserMockInstance = useUserMock({ isAuthenticated: ref(true) }); |
| 81 | + (useUser as jest.Mock).mockReturnValueOnce(useUserMockInstance); |
| 82 | + |
| 83 | + const { getByTestId } = render( |
| 84 | + BottomNavigation, |
| 85 | + { |
| 86 | + mocks: { |
| 87 | + $route: { |
| 88 | + name: 'home', |
| 89 | + }, |
| 90 | + }, |
| 91 | + localVue, |
| 92 | + pinia: createTestingPinia(), |
| 93 | + }, |
| 94 | + ); |
| 95 | + |
| 96 | + const wishlistButton = getByTestId('bottom-navigation-wishlist'); |
| 97 | + userEvent.click(wishlistButton); |
| 98 | + await waitFor(() => { |
| 99 | + expect(useUiStateMockInstance.toggleWishlistSidebar).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + it('handles "Account" button click', async () => { |
| 103 | + const useUiStateMockInstance = useUiStateMock(); |
| 104 | + (useUiState as jest.Mock).mockReturnValueOnce(useUiStateMockInstance); |
| 105 | + const useUserMockInstance = useUserMock({ isAuthenticated: ref(true) }); |
| 106 | + (useUser as jest.Mock).mockReturnValueOnce(useUserMockInstance); |
| 107 | + |
| 108 | + const routerPushMock = jest.fn(); |
| 109 | + |
| 110 | + const { getByTestId } = render( |
| 111 | + BottomNavigation, |
| 112 | + { |
| 113 | + mocks: { |
| 114 | + $router: { |
| 115 | + push: routerPushMock, |
| 116 | + }, |
| 117 | + $route: { |
| 118 | + name: 'home', |
| 119 | + }, |
| 120 | + }, |
| 121 | + localVue, |
| 122 | + pinia: createTestingPinia(), |
| 123 | + }, |
| 124 | + ); |
| 125 | + |
| 126 | + const accountButton = getByTestId('bottom-navigation-account'); |
| 127 | + userEvent.click(accountButton); |
| 128 | + await waitFor(() => { |
| 129 | + expect(routerPushMock).toHaveBeenCalledWith({ name: 'customer' }); |
| 130 | + }); |
| 131 | + }); |
| 132 | + it('handles "Cart" button click', async () => { |
| 133 | + const useUiStateMockInstance = useUiStateMock(); |
| 134 | + (useUiState as jest.Mock).mockReturnValueOnce(useUiStateMockInstance); |
| 135 | + const useUserMockInstance = useUserMock({ isAuthenticated: ref(true) }); |
| 136 | + (useUser as jest.Mock).mockReturnValueOnce(useUserMockInstance); |
| 137 | + |
| 138 | + const { getByTestId } = render( |
| 139 | + BottomNavigation, |
| 140 | + { |
| 141 | + mocks: { |
| 142 | + $route: { |
| 143 | + name: 'home', |
| 144 | + }, |
| 145 | + }, |
| 146 | + localVue, |
| 147 | + pinia: createTestingPinia(), |
| 148 | + }, |
| 149 | + ); |
| 150 | + |
| 151 | + const cartButton = getByTestId('bottom-navigation-cart'); |
| 152 | + userEvent.click(cartButton); |
| 153 | + await waitFor(() => { |
| 154 | + expect(useUiStateMockInstance.toggleCartSidebar).toHaveBeenCalled(); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments