@testing-library/react#render TypeScript Examples

The following examples show how to use @testing-library/react#render. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigureOPNavbar.test.tsx    From one-platform with MIT License 6 votes vote down vote up
describe("ConfigureSSI Page", () => {
  it("should render configure SSI page", () => {
    const { getByText } = render(<ConfigureOPNavbar />);
    const headingElement = getByText(/Configure OPC Base/i);
    expect(headingElement).toBeInTheDocument();
  });

  it("should change active theme", async () => {
    const { container } = render(<ConfigureOPNavbar />);
    let currentActiveImage = container.querySelector("img[active-theme]");
    const navOnlyThemeImage = container.querySelector("#nav-only"); // get the nav-only image button
    expect(currentActiveImage?.getAttribute("active-theme")).toEqual(
      "full-ssi"
    );
    fireEvent.click(navOnlyThemeImage!);

    await waitFor(
      () =>
        // getByRole throws an error if it cannot find an element
        container
          .querySelector("img[active-theme]")!
          .getAttribute("active-theme") === "nav-only"
    );
  });
});
Example #2
Source File: App.test.tsx    From react-doc-viewer with Apache License 2.0 6 votes vote down vote up
test("renders component with no file name", async () => {
  let comp = render(
    <DocViewer
      documents={[{ uri: require("../_example-files_/pdf.pdf") }]}
      config={{ header: { disableFileName: true } }}
    />
  );

  act(async () => {
    await comp?.findByTestId("file-name");
  });

  expect(comp?.getByTestId("react-doc-viewer")).toMatchSnapshot();
});
Example #3
Source File: BasicInfo.test.tsx    From abacus with GNU General Public License v2.0 6 votes vote down vote up
test('renders sensible dates as expected', () => {
  MockDate.set('2020-07-21')
  const { container } = render(
    <MockFormik initialValues={{ experiment: { startDatetime: '', endDatetime: '' } }}>
      <BasicInfo completionBag={completionBag} />
    </MockFormik>,
  )
  const startDateInput = getByLabelText(container, /Start date/)
  const endDateInput = getByLabelText(container, /End date/)

  act(() => {
    fireEvent.change(startDateInput, { target: { value: '2020-07-28' } })
    fireEvent.change(endDateInput, { target: { value: '2020-10-28' } })
  })
  expect(container).toMatchSnapshot()
})
Example #4
Source File: filtering.spec.tsx    From react-final-table with MIT License 6 votes vote down vote up
TableWithFilter = <T extends DataType>({
  columns,
  data,
  filter,
}: {
  columns: ColumnType<T>[];
  data: T[];
  filter: (row: RowType<T>[]) => RowType<T>[];
}) => {
  const { headers, rows } = useTable(columns, data, {
    filter,
  });

  return (
    <table>
      <thead>
        <tr>
          {headers.map((header, idx) => (
            <th key={idx}>{header.render()}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        {rows.map((row, idx) => (
          <tr data-testid="table-row" key={idx}>
            {row.cells.map((cell, idx) => (
              <td key={idx}>{cell.render()}</td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
}
Example #5
Source File: App.test.tsx    From anthem with Apache License 2.0 6 votes vote down vote up
describe("WalletPage", () => {
  test("WalletPage component renders without crashing", () => {
    const div = document.createElement("div");
    ReactDOM.render(
      <ReduxProvider store={store}>
        <WalletPage {...mockI18NProps} />
      </ReduxProvider>,
      div,
    );
    ReactDOM.unmountComponentAtNode(div);
  });
});
Example #6
Source File: SettingsPage.test.tsx    From Cromwell with MIT License 6 votes vote down vote up
describe('admin widget', () => {

    it("renders settings", async () => {
        render(<SettingsPage
            pluginName="main-menu"
            pluginSettings={{
                items: [
                    {
                        title: '_test1_',
                        href: '#_test1_',
                    }
                ]
            }}
        />);

        await screen.findByText('_test1_');
    });
})
Example #7
Source File: all.test.tsx    From zustand-store-addons with MIT License 6 votes vote down vote up
it('uses the store with no args', async () => {
  const useStore = create(
    set => ({
      count: 1,
      inc: () => set(state => ({ count: state.count + 1 })),
    }),
    {
      computed: {
        doubleCount() {
          return this.count * 2;
        },
      },
    }
  );

  function Counter() {
    const { count, doubleCount, inc } = useStore();
    React.useEffect(inc, []);
    return (
      <div>
        <p>count: {count}</p>
        <p>doubleCount: {doubleCount}</p>
      </div>
    );
  }

  const { findByText } = render(<Counter />);

  await findByText('count: 2');
  await findByText('doubleCount: 4');
});
Example #8
Source File: AddRows.test.tsx    From react-datasheet-grid with MIT License 6 votes vote down vote up
test('Calls addRows', () => {
  const addRows = jest.fn()
  render(<AddRows addRows={addRows} />)
  const button = screen.getByRole('button')
  const input = screen.getByRole('spinbutton')

  userEvent.click(button)
  expect(addRows).toHaveBeenLastCalledWith(1)

  userEvent.type(input, '{selectall}5')
  userEvent.click(button)
  expect(addRows).toHaveBeenLastCalledWith(5)

  userEvent.type(input, '{selectall}{backspace}{enter}')
  expect(addRows).toHaveBeenLastCalledWith(1)
})