{"id":21919590,"url":"https://github.com/dusanjovanov/react-use-test","last_synced_at":"2025-03-22T10:19:09.199Z","repository":{"id":57688785,"uuid":"490056500","full_name":"dusanjovanov/react-use-test","owner":"dusanjovanov","description":"⚗️ Hook for testing React component state and functions","archived":false,"fork":false,"pushed_at":"2022-05-08T21:16:21.000Z","size":132,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-28T04:59:23.875Z","etag":null,"topics":["hook","react","testing"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dusanjovanov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-05-08T21:15:03.000Z","updated_at":"2022-09-28T09:22:35.000Z","dependencies_parsed_at":"2022-09-10T09:24:07.640Z","dependency_job_id":null,"html_url":"https://github.com/dusanjovanov/react-use-test","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanjovanov%2Freact-use-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanjovanov%2Freact-use-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanjovanov%2Freact-use-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dusanjovanov%2Freact-use-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dusanjovanov","download_url":"https://codeload.github.com/dusanjovanov/react-use-test/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244938226,"owners_count":20535201,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hook","react","testing"],"created_at":"2024-11-28T20:09:25.259Z","updated_at":"2025-03-22T10:19:09.180Z","avatar_url":"https://github.com/dusanjovanov.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![react-use-test logo](https://github.com/dusanjovanov/react-use-test/blob/master/logo.png 'react-use-test logo')\n\n⚗️ Hook for testing React component state and functions\n\u003cbr /\u003e\n\n[![npm](https://img.shields.io/npm/v/react-use-test?color=%231E90FF\u0026label=npm\u0026style=for-the-badge)](https://www.npmjs.com/package/react-use-test)\n\n## **Installation**\n\n```bash\nnpm install react-use-test\n```\n\n```bash\nyarn add react-use-test\n```\n\n\u003e With standard React testing (@testing-library/react) you don't have access to the internals of React components (state, functions, refs etc.). This hook aims to solve that.\n\n\u003e The hook only supports the Jest framework for now.\n\n## **Usage**\n\n```tsx\n// call useTest in your component and pass it the action and observe functions\n\nimport { useTest } from 'react-use-test';\n\nconst Counter = () =\u003e {\n  const [count, setCount] = React.useState(0);\n\n  const increment = () =\u003e setCount(count + 1);\n\n  // useTest only executes it's code in the jest environment\n  useTest(\n    done =\u003e {\n      increment();\n      // call done when you're done preparing for the test\n      done();\n    },\n    () =\u003e {\n      // jest provides expect as a global function\n      expect(count).toBe(1);\n    }\n  );\n\n  return (\n    \u003cdiv\u003e\n      \u003cspan\u003e{count}\u003c/span\u003e\n      \u003cbutton onClick={increment}\u003e+\u003c/button\u003e\n    \u003c/div\u003e\n  );\n};\n\n// then inside your test file\n\nimport { render } from '@testing-library/react';\nimport { Counter } from '../components/Counter';\n\n// if something throws an error inside useTest, the test will fail\nit('Counter', () =\u003e {\n  render(\u003cCounter /\u003e);\n});\n\n// this library also exports a helper for simulating a change event on inputs\n\nimport { fireChangeEvent } from 'react-use-test';\n\nexport const TextField = () =\u003e {\n  const [value, setValue] = React.useState('');\n\n  useTest(\n    done =\u003e {\n      const element = document.getElementById('text');\n      if (element) {\n        fireChangeEvent(element, 'some text');\n        done();\n      }\n    },\n    () =\u003e {\n      expect(value).toBe('some text');\n    }\n  );\n\n  return (\n    \u003cinput\n      id=\"text\"\n      type=\"text\"\n      value={value}\n      onChange={e =\u003e setValue(e.target.value)}\n    /\u003e\n  );\n};\n```\n\n## **Usage with Typescript**\n\nYou need to declare the `expect` global somewhere in your application code like this\n\n```tsx\ndeclare var expect: any;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdusanjovanov%2Freact-use-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdusanjovanov%2Freact-use-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdusanjovanov%2Freact-use-test/lists"}