Skip to content

Commit

Permalink
Improved Code Coverage in src/components/Venues/VenueModal.tsx (#3203)
Browse files Browse the repository at this point in the history
* Improved Code Coverage in src/components/Venues/VenueModal.tsx

* removed the ignore statements from VenueModal.tsx
  • Loading branch information
harshk-89 authored Jan 7, 2025
1 parent de5330b commit bcfb41f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 4 deletions.
85 changes: 85 additions & 0 deletions src/components/Venues/VenueModal.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,88 @@ describe('VenueModal', () => {
);
});
});

describe('VenueModal with error scenarios', () => {
test('displays error toast when creating a venue fails', async () => {
const errorMocks = [
{
request: {
query: CREATE_VENUE_MUTATION,
variables: {
name: 'Error Venue',
description: 'This should fail',
capacity: 50,
organizationId: 'orgId',
file: '',
},
},
error: new Error('Failed to create venue'),
},
];

const errorLink = new StaticMockLink(errorMocks, true);
renderVenueModal(props[0], errorLink);

const nameInput = screen.getByPlaceholderText('Enter Venue Name');
fireEvent.change(nameInput, { target: { value: 'Error Venue' } });

const descriptionInput = screen.getByPlaceholderText(
'Enter Venue Description',
);
fireEvent.change(descriptionInput, {
target: { value: 'This should fail' },
});

const capacityInput = screen.getByPlaceholderText('Enter Venue Capacity');
fireEvent.change(capacityInput, { target: { value: 50 } });

const submitButton = screen.getByTestId('createVenueBtn');
fireEvent.click(submitButton);

await wait();

expect(toast.error).toHaveBeenCalledWith('Failed to create venue');
});

test('displays error toast when updating a venue fails', async () => {
const errorMocks = [
{
request: {
query: UPDATE_VENUE_MUTATION,
variables: {
capacity: 150,
description: 'Failed update description',
file: 'image1',
id: 'venue1',
name: 'Failed Update Venue',
organizationId: 'orgId',
},
},
error: new Error('Failed to update venue'),
},
];

const errorLink = new StaticMockLink(errorMocks, true);
renderVenueModal(props[1], errorLink);

const nameInput = screen.getByDisplayValue('Venue 1');
fireEvent.change(nameInput, { target: { value: 'Failed Update Venue' } });

const descriptionInput = screen.getByDisplayValue(
'Updated description for venue 1',
);
fireEvent.change(descriptionInput, {
target: { value: 'Failed update description' },
});

const capacityInput = screen.getByDisplayValue('100');
fireEvent.change(capacityInput, { target: { value: 150 } });

const submitButton = screen.getByTestId('updateVenueBtn');
fireEvent.click(submitButton);

await wait();

expect(toast.error).toHaveBeenCalledWith('Failed to update venue');
});
});
4 changes: 0 additions & 4 deletions src/components/Venues/VenueModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ const VenueModal = ({
...(edit && { id: venueData?._id }),
},
});
/* istanbul ignore next */
if (data) {
toast.success(
edit ? (t('venueUpdated') as string) : (t('venueAdded') as string),
Expand All @@ -114,7 +113,6 @@ const VenueModal = ({
setVenueImage(false);
}
} catch (error) {
/* istanbul ignore next */
errorHandler(t, error);
}
}, [
Expand All @@ -136,7 +134,6 @@ const VenueModal = ({
const clearImageInput = useCallback(() => {
setFormState((prevState) => ({ ...prevState, imageURL: '' }));
setVenueImage(false);
/* istanbul ignore next */
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
Expand Down Expand Up @@ -236,7 +233,6 @@ const VenueModal = ({
}));
setVenueImage(true);
const file = e.target.files?.[0];
/* istanbul ignore next */
if (file) {
setFormState({
...formState,
Expand Down

0 comments on commit bcfb41f

Please sign in to comment.