Tools, Purpose & Patterns
Frontend Engineering Manager
Certa - No Code workflow management and automation (SAAS)
TS
ReactJS
@rajatvijay
@rajatvijay
test-driven development
Jest is a Javascript test runner
Extending jest asserts to add more DOM specific assertions
test("renders learn react link", () => {
const root = document.createElement("div");
render(<App />, root);
const button = root.querySelector("button")!;
expect(button.disabled).toBe(true);
});
import "@testing-library/jest-dom";
test("renders learn react link", () => {
const root = document.createElement("div");
render(<App />, root);
const button = root.querySelector("button")!;
expect(button).toBeDisabled();
});
Test if a button is disabled
Essentially just a set of functions to query the DOM
Render DOM from a react component
Query that DOM
Use jest assertions to test expected output
fireEvent.change v/s user.type
test("should call the onSubmit2 after re redering", () => {
const onSubmit1 = jest.fn();
const { rerender } = render(<App onSubmit={onSubmit1} />);
const input = screen.getByLabelText("Name");
user.type(input, "Rajat");
const onSubmit2 = jest.fn();
rerender(<App onSubmit={onSubmit2} />);
const button = screen.getByRole("button");
user.click(button);
expect(onSubmit1).toBeCalledTimes(0);
expect(onSubmit2).toBeCalledTimes(1);
});
It's virtually impossible to test accessibility issues in the component.
But jest-axe can help you cover most obvious cases
const renderWithRedux = (
ui,
{
initialState,
store = createStore(
rootReducer,
initialState,
applyMiddleware(thunkMiddlware)
),
} = {}
) => {
return render(<Provider store={store}>{ui}</Provider>)
};
@rajatvijay
@rajatvijay