|
| 1 | +--- |
| 2 | +sidebar_label: Examples |
| 3 | +title: Ionic React Testing Examples |
| 4 | +description: Learn how to test an Ionic React application. This document provides examples of how to test different types of components. |
| 5 | +--- |
| 6 | + |
| 7 | +# Examples |
| 8 | + |
| 9 | +## Testing a modal presented from a trigger |
| 10 | + |
| 11 | +This example shows how to test a modal that is presented from a trigger. The modal is presented when the user clicks a button. |
| 12 | + |
| 13 | +### Example component |
| 14 | + |
| 15 | +```tsx title="src/Example.tsx" |
| 16 | +import { IonButton, IonModal } from '@ionic/react'; |
| 17 | + |
| 18 | +export default function Example() { |
| 19 | + return ( |
| 20 | + <> |
| 21 | + <IonButton id="open-modal">Open</IonButton> |
| 22 | + <IonModal trigger="open-modal">Modal content</IonModal> |
| 23 | + </> |
| 24 | + ); |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### Testing the modal |
| 29 | + |
| 30 | +```tsx title="src/Example.test.tsx" |
| 31 | +import { IonApp } from '@ionic/react'; |
| 32 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 33 | + |
| 34 | +import Example from './Example'; |
| 35 | + |
| 36 | +test('button presents a modal when clicked', async () => { |
| 37 | + render( |
| 38 | + <IonApp> |
| 39 | + <Example /> |
| 40 | + </IonApp> |
| 41 | + ); |
| 42 | + // Simulate a click on the button |
| 43 | + fireEvent.click(screen.getByText('Open')); |
| 44 | + // Wait for the modal to be presented |
| 45 | + await waitFor(() => { |
| 46 | + // Assert that the modal is present |
| 47 | + expect(screen.getByText('Modal content')).toBeInTheDocument(); |
| 48 | + }); |
| 49 | +}); |
| 50 | +``` |
| 51 | + |
| 52 | +## Testing a modal presented from useIonModal |
| 53 | + |
| 54 | +This example shows how to test a modal that is presented using the `useIonModal` hook. The modal is presented when the user clicks a button. |
| 55 | + |
| 56 | +### Example component |
| 57 | + |
| 58 | +```tsx title="src/Example.tsx" |
| 59 | +import { IonContent, useIonModal, IonHeader, IonToolbar, IonTitle, IonButton, IonPage } from '@ionic/react'; |
| 60 | + |
| 61 | +const ModalContent: React.FC = () => { |
| 62 | + return ( |
| 63 | + <IonContent> |
| 64 | + <div>Modal Content</div> |
| 65 | + </IonContent> |
| 66 | + ); |
| 67 | +}; |
| 68 | + |
| 69 | +const Example: React.FC = () => { |
| 70 | + const [present] = useIonModal(ModalContent); |
| 71 | + return ( |
| 72 | + <IonPage> |
| 73 | + <IonHeader> |
| 74 | + <IonToolbar> |
| 75 | + <IonTitle>Blank</IonTitle> |
| 76 | + </IonToolbar> |
| 77 | + </IonHeader> |
| 78 | + <IonContent fullscreen={true}> |
| 79 | + <IonButton expand="block" className="ion-margin" onClick={() => present()}> |
| 80 | + Open |
| 81 | + </IonButton> |
| 82 | + </IonContent> |
| 83 | + </IonPage> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +export default Example; |
| 88 | +``` |
| 89 | + |
| 90 | +### Testing the modal |
| 91 | + |
| 92 | +```tsx title="src/Example.test.tsx" |
| 93 | +import { IonApp } from '@ionic/react'; |
| 94 | +import { fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 95 | + |
| 96 | +import Example from './Example'; |
| 97 | + |
| 98 | +test('should present ModalContent when button is clicked', async () => { |
| 99 | + render( |
| 100 | + <IonApp> |
| 101 | + <Example /> |
| 102 | + </IonApp> |
| 103 | + ); |
| 104 | + // Simulate a click on the button |
| 105 | + fireEvent.click(screen.getByText('Open')); |
| 106 | + // Wait for the modal to be presented |
| 107 | + await waitFor(() => { |
| 108 | + // Assert that the modal is present |
| 109 | + expect(screen.getByText('Modal Content')).toBeInTheDocument(); |
| 110 | + }); |
| 111 | +}); |
| 112 | +``` |
0 commit comments