{"id":30127709,"url":"https://github.com/sourceallies/rtl-mock-component","last_synced_at":"2025-08-10T17:10:03.279Z","repository":{"id":41255914,"uuid":"445972790","full_name":"sourceallies/rtl-mock-component","owner":"sourceallies","description":"Library to ease mocking of components for @testing-library/react","archived":false,"fork":false,"pushed_at":"2022-06-30T00:51:13.000Z","size":198,"stargazers_count":3,"open_issues_count":2,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-07-25T06:13:23.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/sourceallies.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-01-09T02:09:16.000Z","updated_at":"2025-02-05T18:30:56.000Z","dependencies_parsed_at":"2022-08-25T21:11:31.760Z","dependency_job_id":null,"html_url":"https://github.com/sourceallies/rtl-mock-component","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/sourceallies/rtl-mock-component","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceallies%2Frtl-mock-component","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceallies%2Frtl-mock-component/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceallies%2Frtl-mock-component/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceallies%2Frtl-mock-component/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sourceallies","download_url":"https://codeload.github.com/sourceallies/rtl-mock-component/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sourceallies%2Frtl-mock-component/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269756394,"owners_count":24470566,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2025-08-10T17:10:01.880Z","updated_at":"2025-08-10T17:10:03.259Z","avatar_url":"https://github.com/sourceallies.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# React Testing Library Mock Component\n\n## The Problem\n\n[React Testing Library](https://testing-library.com/docs/react-testing-library/intro/) is a popular testing library for react applications. It fundamentally changed how react applications are tested by advocating for a \"deep by default\" rendering strategy and asserting and interacting with a component more inline with the way a human does.\n\nWhile I generally agree with Kent C. Dodds that we shouldn't mock every component any more than we should mock every private function in a module, there are times in more complicated applications where rendering complicated child components is impractical and difficult to maintain.\n\n## The Solution\n\nThis library compliments `@testing-library/react` to make it easy to setup and mock components and assert that those components are holding the correct props.\n\n## Setup\n\n1. We assume that your project is already using [@testing-library/react](https://testing-library.com/docs/react-testing-library/intro/) and either [Jest](https://jestjs.io) or [Vitest](https://vitest.dev)\n2. Install this library\n    ```bash\n    npm install --save-dev @sourceallies/rtl-mock-component\n    ```\n\n## Usage\n\n1. First, import this library and the component you are mocking in your test:\n    ```Typescript\n    import {setupMockComponent, getByMockComponent} from '@sourceallies/rtl-mock-component';\n    import MyChildComponent from './MyChildComponent';\n    ```\n2. Next, mock the module just like any other mock:\n    ```Typescript\n    jest.mock('./MyChildComponent');\n    ```\n    or\n    ```Typescript\n    vi.mock('./MyChildComponent');\n    ```\n3. In a `beforeEach` method, call `setupMockComponent` and pass it the component you are mocking. This sets up the mock.\n    ```Typescript\n    beforeEach(() =\u003e {\n        setupMockComponent(MyChildComponent);\n    });\n    ```\n4. Now, when you call `render` with any ancestor component, the implementation will not be called and a stub element will be rendered in its place.\n5. If you want to assert the component is in the dom you can do it as so:\n    ```Typescript\n    expect(getByMockComponent(MyChildComponent)).toBeInTheDocument();\n    ```\n6. `getMockComponent` and the other query functions return a `MockedComponentElement\u003cT\u003e` that extends from `HTMLElement` and adds a `props` property that can be asserted on. For example, to make sure that the component is currently rendered with the open prop set to true:\n    ```Typescript\n    expect(getByMockComponent(MyChildComponent).props.open).toBe(true);\n    ```\n\n    *Note:* this way of testing ensures that the component is **currently** rendered with the provided value. `toHaveBeenCalledWith` would only test that was ever rendered with the expected value.\n\n### Customization\n\n`setupMockComponent` takes an options argument that can change the behavior of the mock.\n\nTo change the element used from a div, pass the element name. This is useful when the mock is used in a place where only certain elements are valid (ex. a child of a `tbody` must be a `tr`)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourceallies%2Frtl-mock-component","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsourceallies%2Frtl-mock-component","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsourceallies%2Frtl-mock-component/lists"}