Skip to content

Commit

Permalink
fix(unit): test
Browse files Browse the repository at this point in the history
  • Loading branch information
aliraza556 committed Oct 27, 2024
1 parent 18bc670 commit 4b4ebff
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const GraphDetailsStep: FC<Props> = ({ onSubmit, error }) => {

return (
<Flex>
<Flex align="center" direction="column" justify="center">
<Flex direction="column" justify="space-between">
<StyledText>Welcome to SecondBrain</StyledText>
<StyledSubText>Set a name and short description for your graph.</StyledSubText>
</Flex>
Expand Down Expand Up @@ -101,7 +101,6 @@ const StyledText = styled(Text)`
font-size: 22px;
font-weight: 600;
font-family: 'Barlow';
text-align: center;
margin-bottom: 10px;
`

Expand Down
120 changes: 120 additions & 0 deletions src/components/ModalsContainer/OnboardingFlow/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import '@testing-library/jest-dom'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import React from 'react'
import { postAboutData } from '~/network/fetchSourcesData'
import { useModal } from '~/stores/useModalStore'
import { OnboardingModal } from '../index'

jest.mock('~/network/fetchSourcesData', () => ({
postAboutData: jest.fn(),
}))

jest.mock('~/stores/useModalStore', () => ({
useModal: jest.fn(),
}))

const useModalMock = useModal as jest.MockedFunction<typeof useModal>
const postAboutDataMock = postAboutData as jest.MockedFunction<typeof postAboutData>

describe('OnboardingModal Component', () => {
beforeEach(() => {
jest.clearAllMocks()

useModalMock.mockReturnValue({
close: jest.fn(),
visible: true,
})
})

test('renders the onboarding modal', () => {
render(<OnboardingModal />)
expect(screen.getByText('Welcome to SecondBrain')).toBeInTheDocument()
expect(screen.getByText('Set a name and short description for your graph.')).toBeInTheDocument()
})

test('submits form successfully', async () => {
postAboutDataMock.mockResolvedValue({ status: 'success' })

render(<OnboardingModal />)

fireEvent.change(screen.getByPlaceholderText('Type graph title here...'), { target: { value: 'Test Title' } })

fireEvent.change(screen.getByPlaceholderText('Type graph description here...'), {
target: { value: 'Test Description' },
})

fireEvent.click(screen.getByText('Confirm'))

await waitFor(() => {
expect(postAboutDataMock).toHaveBeenCalledWith({
title: 'Test Title',
description: 'Test Description',
})
})
})

test('displays error on form submission failure', async () => {
postAboutDataMock.mockRejectedValue({ status: 400, json: async () => ({ errorCode: 'Error occurred' }) })

render(<OnboardingModal />)

fireEvent.change(screen.getByPlaceholderText('Type graph title here...'), { target: { value: 'Test Title' } })

fireEvent.change(screen.getByPlaceholderText('Type graph description here...'), {
target: { value: 'Test Description' },
})

fireEvent.click(screen.getByText('Confirm'))

await waitFor(() => {
expect(screen.getByText('Error occurred')).toBeInTheDocument()
})
})

test('closes modal on successful submission', async () => {
const closeMock = jest.fn()

useModalMock.mockReturnValue({
close: closeMock,
visible: true,
})

postAboutDataMock.mockResolvedValue({ status: 'success' })

render(<OnboardingModal />)

fireEvent.change(screen.getByPlaceholderText('Type graph title here...'), { target: { value: 'Test Title' } })

fireEvent.change(screen.getByPlaceholderText('Type graph description here...'), {
target: { value: 'Test Description' },
})

fireEvent.click(screen.getByText('Confirm'))

await waitFor(() => {
expect(closeMock).toHaveBeenCalled()
})
})

test('resets form and error on modal close', async () => {
const { rerender } = render(<OnboardingModal />)

fireEvent.change(screen.getByPlaceholderText('Type graph title here...'), { target: { value: 'Test Title' } })

fireEvent.change(screen.getByPlaceholderText('Type graph description here...'), {
target: { value: 'Test Description' },
})

useModalMock.mockReturnValue({
close: jest.fn(),
visible: false,
})

rerender(<OnboardingModal />)

waitFor(() => {
expect(screen.getByPlaceholderText('Type graph title here...')).toHaveValue('')
expect(screen.getByPlaceholderText('Type graph description here...')).toHaveValue('')
})
})
})

0 comments on commit 4b4ebff

Please sign in to comment.