-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement testing TODO: investigate failing Image test #1075
- Loading branch information
1 parent
157ed0f
commit 2b0ae3f
Showing
5 changed files
with
400 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
import { fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import userEvent, { UserEvent } from '@testing-library/user-event'; | ||
import { http } from 'msw'; | ||
import { MockInstance } from 'vitest'; | ||
import { storageApi } from '../../api/api'; | ||
import handleIMS_APIError from '../../handleIMS_APIError'; | ||
import ImagesJSON from '../../mocks/Images.json'; | ||
import { server } from '../../mocks/server'; | ||
import { renderComponentWithRouterProvider } from '../../testUtils'; | ||
import EditImageDialog, { ImageDialogProps } from './editImageDialog.component'; | ||
|
||
vi.mock('../../handleIMS_APIError'); | ||
|
||
describe('Edit image dialog', () => { | ||
const onClose = vi.fn(); | ||
let props: ImageDialogProps; | ||
let user: UserEvent; | ||
const createView = () => { | ||
return renderComponentWithRouterProvider(<EditImageDialog {...props} />); | ||
}; | ||
|
||
beforeEach(() => { | ||
props = { | ||
open: true, | ||
onClose: onClose, | ||
}; | ||
user = userEvent.setup(); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
const modifyImageValues = (values: { | ||
file_name?: string; | ||
title?: string; | ||
description?: string; | ||
}) => { | ||
if (values.file_name !== undefined) | ||
fireEvent.change(screen.getByLabelText('File Name *'), { | ||
target: { value: values.file_name }, | ||
}); | ||
|
||
if (values.title !== undefined) | ||
fireEvent.change(screen.getByLabelText('Title'), { | ||
target: { value: values.title }, | ||
}); | ||
|
||
if (values.description !== undefined) | ||
fireEvent.change(screen.getByLabelText('Description'), { | ||
target: { value: values.description }, | ||
}); | ||
}; | ||
|
||
describe('Edit an image', () => { | ||
let axiosPatchSpy: MockInstance; | ||
beforeEach(() => { | ||
props = { | ||
...props, | ||
selectedImage: ImagesJSON[0], | ||
}; | ||
|
||
axiosPatchSpy = vi.spyOn(storageApi, 'patch'); | ||
}); | ||
|
||
it('disables save button and shows circular progress indicator when request is pending', async () => { | ||
server.use( | ||
http.patch('/images/:id', () => { | ||
return new Promise(() => {}); | ||
}) | ||
); | ||
|
||
createView(); | ||
|
||
modifyImageValues({ | ||
file_name: 'Image A', | ||
}); | ||
|
||
const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
await user.click(saveButton); | ||
|
||
expect(saveButton).toBeDisabled(); | ||
expect(await screen.findByRole('progressbar')).toBeInTheDocument(); | ||
}); | ||
|
||
it('Edits an image correctly', async () => { | ||
createView(); | ||
modifyImageValues({ | ||
file_name: 'test_file_name.jpeg', | ||
title: 'Test Title', | ||
description: 'Test Description', | ||
}); | ||
|
||
const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
|
||
await user.click(saveButton); | ||
|
||
expect(axiosPatchSpy).toHaveBeenCalledWith('/images/1', { | ||
file_name: 'test_file_name.jpeg', | ||
title: 'Test Title', | ||
description: 'Test Description', | ||
}); | ||
|
||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it('No values changed shows correct error message', async () => { | ||
createView(); | ||
|
||
const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
|
||
await user.click(saveButton); | ||
|
||
expect( | ||
screen.getByText( | ||
"There have been no changes made. Please change a field's value or press Cancel to exit." | ||
) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('Required fields show error if they are whitespace or current value just removed', async () => { | ||
createView(); | ||
modifyImageValues({ | ||
file_name: '', | ||
}); | ||
const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
|
||
await user.click(saveButton); | ||
|
||
expect(screen.getByText('Please enter a file name.')).toBeInTheDocument(); | ||
expect(onClose).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('CatchAllError request works correctly and displays refresh page message', async () => { | ||
createView(); | ||
modifyImageValues({ | ||
file_name: 'Error 500', | ||
}); | ||
const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
|
||
await user.click(saveButton); | ||
|
||
expect(handleIMS_APIError).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls onClose when Close button is clicked', async () => { | ||
createView(); | ||
const cancelButton = screen.getByRole('button', { name: 'Cancel' }); | ||
await user.click(cancelButton); | ||
|
||
await waitFor(() => { | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.