Skip to content

Commit

Permalink
test: improve test coverage for ToastManager
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton committed Nov 8, 2024
1 parent 76abe05 commit e7e14aa
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions packages/react/src/toast/__tests__/ToastManager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ describe('ToastManager', () => {
});
expect(updateSuccess).toBe(true);
}, [toast]);
const handleClickUpdateInvalidToast = useCallback(() => {
const updateSuccess = toast.update(null, {});
expect(updateSuccess).toBe(false);
}, [toast]);

return (
<>
Expand All @@ -525,6 +529,9 @@ describe('ToastManager', () => {
<Button onClick={handleClickUpdateToast}>
Update Toast
</Button>
<Button onClick={handleClickUpdateInvalidToast}>
Update Invalid Toast
</Button>
</>
);
};
Expand All @@ -540,9 +547,75 @@ describe('ToastManager', () => {

// Update the toast
await user.click(screen.getByText('Update Toast'));
await user.click(screen.getByText('Update Invalid Toast'));

// Check if the content has been updated
const toastElement = screen.getByTestId(toastId);
expect(toastElement).toHaveTextContent(updatedMessage);
});

it('should not create a toast and return false for invalid placement', async () => {
const user = userEvent.setup();
const toastId = 'toast-id';
const placement = 'center'; // "center" is not a supported placement
const message = 'This is a toast message';

// Spy on console.error to capture and check the error message
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

const WrapperComponent = (props) => (
<ToastManager {...props} />
);

const TestComponent = () => {
const toast = useToastManager();
const handleClick = useCallback(() => {
const result = toast(({ onClose }) => (
<Toast
appearance="error"
isClosable
onClose={onClose}
data-testid={toastId}
>
{message}
</Toast>
), { placement });
expect(result).toBe(false);
}, [toast]);

return (
<Button onClick={handleClick}>
Add Toast
</Button>
);
};

render(
<WrapperComponent>
<TestComponent />
</WrapperComponent>
);

const button = await screen.findByText('Add Toast');
await user.click(button);

// Check that console.error was called with the expected error message
const placements = [
'bottom',
'bottom-right',
'bottom-left',
'top',
'top-left',
'top-right',
];
const expectedErrorMessage = `[ToastManager] Error: Invalid toast placement "${placement}". Please provide a valid placement from the following options: ${placements.join(', ')}.`;
expect(consoleErrorSpy).toHaveBeenCalledWith(expectedErrorMessage);

// Assert that no toast element with the invalid placement was created
const toastElement = screen.queryByTestId(toastId);
expect(toastElement).not.toBeInTheDocument();

// Restore console.error to its original implementation
consoleErrorSpy.mockRestore();
});
});

0 comments on commit e7e14aa

Please sign in to comment.