How to use react-testing-library - 10 common examples

To help you get started, we’ve selected a few react-testing-library examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github kentcdodds / stop-runaway-react-effects / src / __tests__ / index.js View on Github external
} else {
          // something else is up
          throw e
        }
      }
    }, deps)
  }

  hijackEffects({callCount: 2})

  let rendered = false

  // it's super weird, but somehow the error is not try/catchable here, but
  // it still fails the test. It's really odd. So we do some weird stuff to make
  // sure we wait for it to be thrown.
  await wait(
    () => {
      if (!rendered) {
        rendered = true
        render(ui)
      }
      expect(console.warn).toHaveBeenCalledTimes(1)
    },
    {timeout: 500},
  )
}
github timc1 / time-capsule / src / components / questionnaire / __tests__ / steps / interests.js View on Github external
test(`User should be able to add a new checkbox item.`, async () => {
    const { getByText, getByTestId, getAllByTestId } = render()
    const modalToggle = getByTestId('modal-toggle')
    // Open menu.
    const menuToggle = getByText(/More/i)
    fireEvent.click(menuToggle)

    // Get input field.
    const input = getByTestId('add-more-input')
    const submitButton = getByTestId('add-more-submit-button')

    // Assert that input and submit button are present.
    expect(input).toBeInTheDocument()
    expect(submitButton).toHaveAttribute('disabled')

    // Input new checkbox item.
    fireEvent.change(input, {
      target: {
        value: 'New Checkbox Item!',
      },
    })
github mlaursen / react-md / packages / wia-aria / src_old / hooks / __tests__ / useSearchEventHandler.tsx View on Github external
it("should call the onKeyboardFocus with the correct values when there is a match", () => {
      const { container } = render();
      const items = container.querySelectorAll("[role='menuitem']");
      fireEvent.keyDown(items[0], { key: "I" });

      expect(onKeyboardFocus).toBeCalledWith(
        expect.objectContaining({
          element: items[1],
          elementIndex: 1,
          // using items causes a memeory leak for some reason
          focusableElements: expect.any(Array),
        }),
        expect.any(Object)
      );

      fireEvent.keyDown(items[1], { key: "t" });
      expect(onKeyboardFocus).toBeCalledWith(
        expect.objectContaining({
          element: items[2],
          elementIndex: 2,
          focusableElements: expect.any(Array),
        }),
        expect.any(Object)
      );
    });
  });
github byte-foundry / prototypo / app / scripts / components / __tests__ / register.js View on Github external
const {getByText, getByTestId, getByLabelText} = render(
			
				
					
				
			,
		);

		// waiting for the loading to finish
		const form = await waitForElement(() => getByTestId('register-form'));

		// Trying to register without entering credentials
		fireEvent.submit(form);

		await waitForElement(() => getByText('Fields with a * are required'));

		fireEvent.change(getByLabelText('First name', {exact: false}), {
			target: {value: 'Jean-Michel'},
		});
		fireEvent.change(getByLabelText('Email', {exact: false}), {
			target: {value: 'test@test.test'},
		});
		fireEvent.change(getByLabelText('Password', {exact: false}), {
			target: {value: 'password'},
		});
		// Until event submission is supported by JSDOM
		fireEvent.submit(form, {
			target: {
				firstname: {value: 'Jean-Michel'},
github flow-typed / flow-typed / definitions / npm / react-testing-library_v5.x.x / flow_v0.67.1- / test_react-testing-library_v5.x.x.js View on Github external
it('should throw on invalid arguments', () => {
    // $ExpectError
    fireEvent(1);
    // $ExpectError
    fireEvent(htmlEl, 1);
  });
github flow-typed / flow-typed / definitions / npm / react-testing-library_v5.x.x / flow_v0.67.1- / test_react-testing-library_v5.x.x.js View on Github external
it('should throw on invalid arguments', () => {
    // $ExpectError
    fireEvent(1);
    // $ExpectError
    fireEvent(htmlEl, 1);
  });
github flow-typed / flow-typed / definitions / npm / react-testing-library_v2.0.x / flow_v0.58.0- / test_react-testing-library_v2.0.x.js View on Github external
it('should fail on invalid inputs', () => {
    // $ExpectError
    wait(1);
    // $ExpectError
    wait(() => {}, 1);
  });
github flow-typed / flow-typed / definitions / npm / react-testing-library_v5.x.x / flow_v0.67.1- / test_react-testing-library_v5.x.x.js View on Github external
it('should fail on invalid inputs', () => {
    // $ExpectError
    wait(1);
    // $ExpectError
    wait(() => {}, 1);
  });
github flow-typed / flow-typed / definitions / npm / react-testing-library_v5.x.x / flow_v0.67.1- / test_react-testing-library_v5.x.x.js View on Github external
it('should fail on invalid inputs', () => {
    // $ExpectError
    wait(1);
    // $ExpectError
    wait(() => {}, 1);
  });
github flow-typed / flow-typed / definitions / npm / react-testing-library_v2.0.x / flow_v0.58.0- / test_react-testing-library_v2.0.x.js View on Github external
it('should fail on invalid inputs', () => {
    // $ExpectError
    wait(1);
    // $ExpectError
    wait(() => {}, 1);
  });