Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudchakk committed Nov 4, 2024
1 parent 616eedc commit bdb2232
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/containers/offer-page/create-offer/CreateOffer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const CreateOffer: FC<CreateOfferProps> = ({ closeDrawer, updateOffer }) => {
OfferService.createOffer({ ...data, FAQ: findFullObjects(data.FAQ) }),
[]
)
console.log(7)

return (
<CreateOrEditOffer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const CreateOrEditOffer: FC<CreateOrUpdateOfferProps> = ({
const navigate = useNavigate()
const { hash } = useLocation()

console.log(1)

const offerAction = existingOffer
? OfferActionsEnum.Edit
: OfferActionsEnum.Create
Expand All @@ -70,6 +72,7 @@ const CreateOrEditOffer: FC<CreateOrUpdateOfferProps> = ({
)
}
const onResponse = (response: Offer | null) => {
console.log(2)
if (hash == '#offer') {
dispatch(
openAlert({
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/use-axios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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(
<CreateOrEditOffer existingOffer={null} closeDrawer={mockCloseDrawer} service={mockService} />
<CreateOffer closeDrawer={mockCloseDrawer} />, {store}
)
})

Expand All @@ -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'
Expand All @@ -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')
// })
// })
})

0 comments on commit bdb2232

Please sign in to comment.