diff --git a/src/containers/offer-page/create-offer/CreateOffer.tsx b/src/containers/offer-page/create-offer/CreateOffer.tsx index b1090f634..6d10f03c5 100644 --- a/src/containers/offer-page/create-offer/CreateOffer.tsx +++ b/src/containers/offer-page/create-offer/CreateOffer.tsx @@ -17,6 +17,7 @@ const CreateOffer: FC = ({ closeDrawer, updateOffer }) => { OfferService.createOffer({ ...data, FAQ: findFullObjects(data.FAQ) }), [] ) + console.log(7) return ( = ({ const navigate = useNavigate() const { hash } = useLocation() + console.log(1) + const offerAction = existingOffer ? OfferActionsEnum.Edit : OfferActionsEnum.Create @@ -70,6 +72,7 @@ const CreateOrEditOffer: FC = ({ ) } const onResponse = (response: Offer | null) => { + console.log(2) if (hash == '#offer') { dispatch( openAlert({ diff --git a/src/hooks/use-axios.tsx b/src/hooks/use-axios.tsx index 26f7edfaa..4f2667f87 100644 --- a/src/hooks/use-axios.tsx +++ b/src/hooks/use-axios.tsx @@ -51,7 +51,9 @@ const useAxios = < try { setLoading(true) + console.log(3) const res = await service(params) + console.log(4) const responseData = transform ? transform(res.data) : res.data setResponse(responseData as TransformedResponse) setError(null) diff --git a/tests/unit/containers/offer-page/create-or-edit-offer/CreateOrEditOffer.spec.jsx b/tests/unit/containers/offer-page/create-or-edit-offer/CreateOrEditOffer.spec.jsx index 31e39013a..d37a18815 100644 --- a/tests/unit/containers/offer-page/create-or-edit-offer/CreateOrEditOffer.spec.jsx +++ b/tests/unit/containers/offer-page/create-or-edit-offer/CreateOrEditOffer.spec.jsx @@ -1,15 +1,28 @@ import { screen, fireEvent, waitFor } from '@testing-library/react' +import { configureStore } from '@reduxjs/toolkit' + import { renderWithProviders } from '~tests/test-utils' import CreateOrEditOffer from '~/containers/offer-page/create-or-edit-offer/CreateOrEditOffer' -import { openAlert } from '~/redux/features/snackbarSlice' +import CreateOffer from '~/containers/offer-page/create-offer/CreateOffer' +import snackbarReducer, { openAlert } from '~/redux/features/snackbarSlice' import { snackbarVariants } from '~/constants' import { expect } from 'vitest' +import reducer from '~/redux/reducer' +import { OfferService } from '~/services/offer-service' const mockDispatch = vi.fn() const mockCloseDrawer = vi.fn() -const mockService = vi.fn() +const mockService = vi.fn(async () => ({ data: {} })) const mockNavigate = vi.fn() +vi.mock('~/hooks/use-axios', async () => { + const actual = await vi.importActual('~/hooks/use-axios') + return { + ...actual, + useAxios: vi.fn() + } +}) + vi.mock('~/redux/features/snackbarSlice', async () => { const actual = await vi.importActual('~/redux/features/snackbarSlice') return { @@ -27,16 +40,33 @@ vi.mock('~/hooks/use-redux', async () => { } }) +vi.mock('~/services/offer-service', async () => { + const actual = await vi.importActual('~/services/offer-service') + const mockCreateOffer = vi.fn() + return { + ...actual, + OfferService: { + createOffer: mockCreateOffer + } + } +}) + vi.mock('react-router-dom', async () => ({ ...(await vi.importActual('react-router-dom')), useNavigate: () => mockNavigate })) +const store = configureStore({ + reducer: { + appMain: reducer, + snackbar: snackbarReducer + } +}) describe('CreateOrEditOffer', () => { beforeEach(() => { renderWithProviders( - + , {store} ) }) @@ -45,11 +75,16 @@ describe('CreateOrEditOffer', () => { mockDispatch.mockReset() }) - it('should call a dispatch and navigate on successful response', () => { + it('should call a dispatch and navigate on successful response', async () => { + const { OfferService } = await import( + '~/services/offer-service' + ) + OfferService.createOffer.mockResolvedValue({}) + const saveButton = screen.getByRole('button', {name: /offerPage.createOffer.buttonTitles.tutor/i}) fireEvent.click(saveButton) - waitFor(() => { + await waitFor(() => { expect(mockDispatch).toHaveBeenCalledWith(openAlert({ severity: snackbarVariants.success, message: 'offerPage.createOffer.successMessage' @@ -60,26 +95,26 @@ describe('CreateOrEditOffer', () => { }) }) - it('should call different dispatch and navigate with #offer on successful response', () => { - vi.mock('react-router-dom', async () => ({ - ...(await vi.importActual('react-router-dom')), - useLocation: () => ({hash: '#offer'}) - })) + // it('should call different dispatch and navigate with #offer on successful response', () => { + // vi.mock('react-router-dom', async () => ({ + // ...(await vi.importActual('react-router-dom')), + // useLocation: () => ({hash: '#offer'}) + // })) - const saveButton = screen.getByRole('button', {name: /offerPage.createOffer.buttonTitles.tutor/i}) - fireEvent.click(saveButton) + // const saveButton = screen.getByRole('button', {name: /offerPage.createOffer.buttonTitles.tutor/i}) + // fireEvent.click(saveButton) - waitFor(() => { - expect(mockDispatch).toHaveBeenCalledWith(openAlert({ - severity: snackbarVariants.success, - message: 'offerPage.createOffer.extendedSuccessMessage.tutor', - duration: 10000, - isExtended: true, - route: '/my-offers' - } - )) - expect(mockCloseDrawer).toHaveBeenCalled() - expect(mockNavigate).toHaveBeenCalledWith('/my-profile#complete') - }) - }) + // waitFor(() => { + // expect(mockDispatch).toHaveBeenCalledWith(openAlert({ + // severity: snackbarVariants.success, + // message: 'offerPage.createOffer.extendedSuccessMessage.tutor', + // duration: 10000, + // isExtended: true, + // route: '/my-offers' + // } + // )) + // expect(mockCloseDrawer).toHaveBeenCalled() + // expect(mockNavigate).toHaveBeenCalledWith('/my-profile#complete') + // }) + // }) })