-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
18bc670
commit 4b4ebff
Showing
2 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/components/ModalsContainer/OnboardingFlow/__tests__/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('') | ||
}) | ||
}) | ||
}) |