Security News
CISA Brings KEV Data to GitHub
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
test-drive-react
Advanced tools
Opinionated library for Test-Driven Development of React components, extending Test Drive and providing its DOM parts lookup, presence/absence matchers and event triggering layout matchers.
In addition, it reexports React simulate testing utility and integrated renderer
ClientRenderer
provides a utility for rendering React components in consistent
and convenient way. It creates the holding container, if necessary, with uniform positioning,
automatically binds to it all important Test Drive helper functions, and proivides clean-up
mechanism.
For a typical use, see the end-to-end test.
The renderer is created simply by invoking new ClientRenderer()
.
render(element, container?)
Renders the element
React component. If container
is not specified, a new one is created.
Returns RenderingContext
with following fields:
container
result
- rendered root component (either DOM Element or React component instance)select
- DOM selector
pre-bound to the containercleanup()
Unmounts the root component and removes any container that had been created by the renderer.
Test Drive React provides additional layer of abstraction over the basic assertion tools. In order to keep the intended behavior of a component separated from the actual implementation (DOM structure, particular DOM events, etc.), every component should provide it's "driver". Component driver translates meaningful actions and getters into specific DOM details.
All drivers should extend the DriverBase
class. The basic (and recommended) way of creating drivers is to use
.withDriver(DriverClass)
method which is part of the RenderingContext
interface.
For example, consider a TestComponent
. There should be always
relevant implementation of its driver, e.g.:
export class TestComponentDriver extends DriverBase<HTMLElement> {
static ComponentClass = TestComponent;
get samplePart() {
return this.ensuredSelect('SAMPLE_PART') as HTMLDivElement;
}
doAction() {
this.root.click();
}
}
The ComponentClass
points to the component for which is the driver relevant. It is used by the
ClientRenderer
for validation of a component/driver match during the rendering. (This validation
is intended to prevent using a wrong driver on top of a DOM generated by a component.)
The getter .samplePart
provides access (via data-automation-id
) to specific parts of
the component's shadow DOM, while keeping this detail encapsulated. Similarly,
the .doAction()
method represents specific methods, while keeping the technicalities (event type)
private.
The driver then should be instantiated and used through ClientRenderer
, e.g.:
const { driver } = clientRenderer.render(<TestComponent />).withDriver(TestComponentDriver);
expect(driver.samplePart).to.be.present();
Note that the .withDriver()
function returns RenderingContextWithDriver
, which has following
members:
driver
- instance of the component driverIn the case of composite components, the drivers should mirror their structure as well. .samplePart
in
the above example should, therefore, reference another (relevant) component driver, rather than plain DOM Element,
if it corresponds to custom component.
MIT
FAQs
Opinionated library for Test-Driven Development of React Components.
The npm package test-drive-react receives a total of 154 weekly downloads. As such, test-drive-react popularity was classified as not popular.
We found that test-drive-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.