Other testing libraries like react-testing-library have popularized testing react-components by locating them by specific html-attributes that enforce best pactices for accessibility. This module brings the convenience of those selectors to enzyme β¨.
Install it by running:
yarn add -D enzyme-selectors
Or if using npm instead of yarn:
npm i -D enzyme-selectors
After installation, you should configure the selectors. If you already have enzyme configured this will be easy (if you haven't configured enzyme yet, follow their installation guide before continuing). In the setup file where you configure Enzyme you can add the following lines:
const Enzyme = require("enzyme");
const Adapter = require("enzyme-adapter-react-16");
+ const { addSelectors } = require("enzyme-selectors");
Enzyme.configure({
adapter: new Adapter()
});
+ addSelectors();
This module also includes it's own types, but since the original enzyme types are modified, you have to let typescript know how to add these types.
You can do this by by either putting the following line in a test file:
+ import 'enzyme-selectors';
or by putting:
+ /// <reference types="enzyme-selectors" />
in a global declaration file that you already use.
That's it, You are now ready to start using the selectors in your tests π.
All selectors apply to
mount
as well asshallow
After installing and configuring the following selectors will be added to enzyme:
Find components by their data-testid
value
id
: string
const component = mount(
<div>
<div data-testid="foo">foo</div>
</div>
);
component.findByTestId("foo");
Find components by their aria-label
value
label
: string
const component = mount(
<div>
<button aria-label="Close">×</button>
</div>
);
component.findByAriaLabel("Close");
Find components by their placeholder
value
text
: string
const component = mount(
<div>
<input type="text" placeholder="Name" />
</div>
);
component.findByPlaceholderText("Name");
Find components by their alt
value
text
: string
const component = mount(
<div>
<img alt="foo" />
</div>
);
component.findByAltText("foo");
Find components by their title
value
title
: string
const component = mount(
<div>
<a href="https://..." title="foo">
Go
</a>
</div>
);
component.findByTitle("foo"); // returns <a href="https://..." title="foo">Go</a>
Find components by their role
value
role
: string
const component = mount(
<div>
<button role="Close">×</button>
</div>
);
component.findByRole("Close"); // returns <button role="Close">×</button>
All utilities apply to
mount
as well asshallow
This module also adds a couple of useful utilities that match the added selectors.
Debug components with an alt
text
query
: string
Debug components with an alt
text
query
: string
Debug components by attribute (values)
attribute
: string
value
: string
Debug components with a placeholder
query
: string
Debug components with a role
attribute
query
: string
Debug components with a data-testid
attribute
query
: string
Debug components with a title
attribute
query
: string