generated from taylorbryant/gatsby-starter-tailwind
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update importfrommtnproj component and add some basic tests #913
- Loading branch information
1 parent
62caa63
commit b60f5da
Showing
4 changed files
with
171 additions
and
94 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
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
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
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,96 @@ | ||
import React from 'react' | ||
import { render, fireEvent, waitFor, screen, act } from '@testing-library/react' | ||
import { MockedProvider } from '@apollo/client/testing' | ||
import ImportFromMtnProj from '../ImportFromMtnProj' | ||
import '@testing-library/jest-dom/extend-expect' | ||
|
||
jest.mock('next-auth/react', () => ({ | ||
useSession: jest.fn(() => ({ status: 'authenticated' })) | ||
})) | ||
|
||
jest.mock('next/router', () => ({ | ||
useRouter: jest.fn(() => ({ replace: jest.fn() })) | ||
})) | ||
|
||
jest.mock('../../../js/graphql/Client', () => ({ | ||
|
||
graphqlClient: jest.fn() | ||
})) | ||
|
||
jest.mock('react-toastify', () => ({ | ||
toast: { | ||
info: jest.fn(), | ||
error: jest.fn() | ||
} | ||
})) | ||
|
||
describe('<ImportFromMtnProj />', () => { | ||
it('renders without crashing', () => { | ||
render( | ||
<MockedProvider mocks={[]} addTypename={false}> | ||
<ImportFromMtnProj isButton username='testuser' /> | ||
</MockedProvider> | ||
) | ||
}) | ||
|
||
it('renders a button when isButton prop is true', () => { | ||
render( | ||
<MockedProvider mocks={[]} addTypename={false}> | ||
<ImportFromMtnProj isButton username='testuser' /> | ||
</MockedProvider> | ||
) | ||
|
||
const button = screen.getByText('Import ticks') | ||
expect(button).toBeInTheDocument() | ||
}) | ||
|
||
it('renders modal on button click', async () => { | ||
render( | ||
<MockedProvider mocks={[]} addTypename={false}> | ||
<ImportFromMtnProj isButton username='testuser' /> | ||
</MockedProvider> | ||
) | ||
|
||
const button = screen.getByText('Import ticks') | ||
await waitFor(() => { | ||
act(() => { | ||
fireEvent.click(button) | ||
}) | ||
}) | ||
|
||
await waitFor(() => { | ||
const modalText = screen.getByText('Input your Mountain Project profile link') | ||
expect(modalText).toBeInTheDocument() | ||
}) | ||
}) | ||
|
||
it('accepts input for the Mountain Project profile link', async () => { | ||
render(<ImportFromMtnProj isButton username='testuser' /> | ||
) | ||
|
||
// Simulate a click to open the modal. | ||
const openModalButton = screen.getByText('Import ticks') | ||
await waitFor(() => { | ||
act(() => { | ||
fireEvent.click(openModalButton) | ||
}) | ||
}) | ||
|
||
// Use findBy to wait for the input field to appear. | ||
const inputField = await screen.findByPlaceholderText('https://www.mountainproject.com/user/123456789/username') | ||
|
||
if (!(inputField instanceof HTMLInputElement)) { | ||
throw new Error('Expected an input field') | ||
} | ||
|
||
// Simulate entering a Mountain Project URL. | ||
|
||
await waitFor(() => { | ||
act(() => { | ||
fireEvent.change(inputField, { target: { value: 'https://www.mountainproject.com/user/123456789/sampleuser' } }) | ||
}) | ||
}) | ||
|
||
expect(inputField.value).toBe('https://www.mountainproject.com/user/123456789/sampleuser') | ||
}) | ||
}) |