docs(tests): Add specific guidance to testing standalone - #4611
docs(tests): Add specific guidance to testing standalone#4611Zac-Smucker-Bryan wants to merge 3 commits into
Conversation
For Angular, React, and Vue. Includes new testing page for Vue as a starter.
|
@Zac-Smucker-Bryan is attempting to deploy a commit to the Ionic Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| /> | ||
| </head> | ||
|
|
||
| # Testing Ionic Vue |
There was a problem hiding this comment.
We should be consistent with the other pages and not use the frontmatter title.
| # Testing Ionic Vue |
Clarity on use of componentOnReady, conciseness of test example Co-authored-by: Maria Hutt <thetaPC@users.noreply.github.com>
Slight rephrasing of paragraph explaining componentOnReady helper Add more concise test to Angular using componentOnReady helper Move Angular to last part of Unit Testing Add example test for React, matching Angular Small formatting edits to Vue, add Vue testing page to sidebar
|
Thanks for the feedback @thetaPC. I think I addressed all your comments. Let me know what else might need to be changed. |
| ```tsx | ||
| import { test, expect } from 'vitest'; | ||
| import { render } from '@testing-library/react'; | ||
| import { componentOnReady } from '@ionic/core'; | ||
|
|
||
| import App from './App'; | ||
|
|
||
| test('renders the submit button', async () => { | ||
| const { container } = render(<App />); | ||
|
|
||
| const button = container.querySelector('ion-button'); | ||
|
|
||
| await new Promise<void>((resolve) => componentOnReady(button!, () => resolve())); | ||
|
|
||
| expect(button?.textContent).toContain('Submit'); | ||
| }); | ||
| ``` |
There was a problem hiding this comment.
Wrapped in IonApp to match the first best practice on this page, moved the non-null assertion to the query so the last line reads cleanly, and dropped the vitest import since the other snippets here stay runner agnostic.
| ```tsx | |
| import { test, expect } from 'vitest'; | |
| import { render } from '@testing-library/react'; | |
| import { componentOnReady } from '@ionic/core'; | |
| import App from './App'; | |
| test('renders the submit button', async () => { | |
| const { container } = render(<App />); | |
| const button = container.querySelector('ion-button'); | |
| await new Promise<void>((resolve) => componentOnReady(button!, () => resolve())); | |
| expect(button?.textContent).toContain('Submit'); | |
| }); | |
| ``` | |
| ```tsx | |
| import { IonApp } from '@ionic/react'; | |
| import { render } from '@testing-library/react'; | |
| import { componentOnReady } from '@ionic/core'; | |
| import Example from './Example'; | |
| test('renders the submit button', async () => { | |
| const { container } = render( | |
| <IonApp> | |
| <Example /> | |
| </IonApp> | |
| ); | |
| const button = container.querySelector('ion-button')!; | |
| await new Promise<void>((resolve) => componentOnReady(button, () => resolve())); | |
| expect(button.textContent).toContain('Submit'); | |
| }); |
|
|
||
| ### Waiting for Components | ||
|
|
||
| When you need to wait for an Ionic component to render before asserting against its DOM, use the `componentOnReady` helper exported from `@ionic/core`. Do not call `el.componentOnReady()` directly. `@ionic/vue` uses Stencil's custom elements build, where that method does not exist on the element. The helper waits one animation frame instead, giving the component's inner contents a chance to render. |
There was a problem hiding this comment.
Forgot to mention it in the last review. This should also have an example like the other two frameworks. Consistency is very important for our docs.
Resolves #3808
Description
componentOnReadyfor standalone projects from@ionic/coreas a helper to improve testing when working with Ionic standalone, for Angular, React, and VueChange Type
Rationale / Problems Fixed
Standalone users can't always consistently test what they need without better guidance.
Notes / Comments
There is a companion issue (ionic-team/ionic-framework#31312) and PR (ionic-team/ionic-framework#31313) in ionic-framework that might need to be coordinated for language tweaks.