MODULE 17 · LESSON 17.2
Test what users can perceive and operate instead of component internals.
Use the concept at the correct boundary
The difficult part of React Component and Accessibility Tests is deciding where the responsibility belongs and how you will know it works. This lesson defines an application trust boundary, where an explicit contract is safer than framework convention or an undocumented assumption.
Here, that decision supports a specific checkpoint: Create a CI-ready test suite for CourseFlow's enrollment path. A reviewable result should include a repeatable request, automated test, query result and failure response rather than a claim that the feature simply works.
React Component and Accessibility Tests workflow
- 1Queries By Role
- 2User Events
- 3Async UI
- 4Automated A11y Checks
A practical model for react component and accessibility tests
Test what users can perceive and operate instead of component internals. The useful unit of understanding is the boundary: who owns the decision, which input crosses it, what result is visible and how a failure is reported.
- Queries By Role: Implement one behavior that another learner can reproduce without reading your mind.
- User Events: Compare the simplest correct approach with one credible alternative.
- Async UI: State the assumption this concept relies on and show how the system behaves when it is false.
- Automated A11y Checks: Connect this concept to the module checkpoint and identify the evidence a reviewer should expect.
What the example proves
Use the sample to answer one question: does the implementation make queries by role easier to verify or merely harder to see?
expect(screen.getByRole('heading', { name: /course catalog/i })).toBeVisible();
await user.click(screen.getByRole('button', { name: /save course/i }));Change one input connected to queries by role, predict the result, then run the successful path and one failure path.
Implement and verify one behavior
- 1Queries By Role
Compare expected and actual output before editing; the difference tells you where to investigate.
- 2User Events
Keep names tied to the product rule so a reviewer can follow the change without decoding abbreviations.
- 3Async UI
Add a regression check close to the boundary where this behavior can fail.
- 4Automated A11y Checks
Describe the behavior in one sentence, then choose the smallest input that can prove it.
Common design traps
- Treating queries by role as vocabulary instead of defining the behavior it must produce.
- Testing the expected path while ignoring an empty, invalid, repeated or unauthorized case around user events.
- Allowing async UI to cross a boundary without an explicit contract or useful error.
- Changing several layers before capturing the first piece of evidence, which makes the original cause harder to see.
Diagnose before changing code
- Reduce the problem to the smallest failing React Component and Accessibility Tests case.
- Capture the actual input and output at the queries by role boundary.
- Read the first relevant error, request, trace or query rather than the loudest downstream symptom.
- Test one explanation for the failure in user events; avoid changing two variables together.
- Keep a regression check that would expose the same defect if it returned.
Security decision
Validate external input, authorize the requested action, use parameterized data access, and keep credentials out of responses, source control and logs.
Performance decision
Bound queries and collections, inspect the actual request or query plan, and optimize only the slow boundary confirmed by evidence.
PRACTICE
Build something you can inspect
Test search, save and error recovery using roles and visible names.
Stretch challenge
Replace one happy-path assumption about user events with explicit validation and show the before-and-after behavior.
Definition of done
- The behavior around queries by role works with realistic input.
- A failure involving user events is handled clearly and without leaking sensitive detail.
- The implementation remains keyboard-usable when it produces an interface.
- Your evidence directly supports the claim made in the exercise.
- The README records the important trade-off without pretending the solution is universal.
Check your reasoning
Why are role-based queries more resilient than CSS selectors?
Answer by naming the expected queries by role behavior, the layer responsible for it and the evidence that would confirm your explanation.
Where would you investigate the first failure?
Start where user events crosses a boundary. Compare the actual input and output there before following downstream symptoms.
What would make this work reviewable?
Show the focused change, repeatable steps, the result of your check and one honest trade-off connected to async UI.
What to carry into the next lesson
- Test what users can perceive and operate instead of component internals.
- Keep queries by role visible at the boundary where it can be tested.
- Use evidence from user events before widening the implementation.
References and related reading
Progress is stored only in this browser.
Share this page
Share this page with the people who will use it next.
Discussion
No comments yet. Add the first useful question or observation.
You must log in to post a comment.