项目作者: Anber

项目描述 :
高级语言: TypeScript
项目地址: git://github.com/Anber/testid.git
创建时间: 2019-08-14T10:17:34Z
项目社区:https://github.com/Anber/testid

开源协议:MIT License

下载


Installation

  1. npm i --save react-test-id
  2. # or
  3. yarn add react-test-id

Configuration

By default the attribute name is data-test-id and the separator is :. The behaviour can be changed by creating new custom component and hooks.

  1. import { custom } from "react-test-id";
  2. const [Role, useRole, useRoles] = custom({
  3. attr: "data-role",
  4. display: /\bwithRoles\b/.test(globalThis.location.search),
  5. separator: ":",
  6. });
  7. export { Role, useRole, useRoles };

Usage

  1. import React from 'react';
  2. import { TestId, useTestId, useTestIds } from "react-test-id"; // if you use predefined configuration
  3. import { TestId, useTestId, useTestIds } from "./custom"; // if you use your own configuration
  4. function Title() {
  5. const id = useTestId(":title");
  6. return <h1 {...id}></h1>;
  7. }
  8. function Actions() {
  9. const [okId, cancelId] = useTestIds([":ok-button", ":cancel-button"]);
  10. return (
  11. <div>
  12. <button {...okId}>Ok</button>
  13. <button {...cancelId}>Cancel</button>
  14. </div>
  15. );
  16. }
  17. function Modal() {
  18. return (
  19. <TestId name="some-modal">
  20. {id => (
  21. <div {...id}>
  22. <Title ></Title>
  23. <Actions ></Actions>
  24. </div>
  25. )}
  26. </TestId>
  27. )
  28. }

Modal component above generates next markup:

  1. <div data-test-id="some-modal">
  2. <h1 data-test-id="some-modal:title"></h1>
  3. <div>
  4. <button data-test-id="some-modal:ok-button"></button>
  5. <button data-test-id="some-modal:cancel-button"></button>
  6. </div>
  7. </div>