{"id":13726008,"url":"https://github.com/kentcdodds/react-test-context-provider","last_synced_at":"2025-04-15T01:14:40.533Z","repository":{"id":57346246,"uuid":"68149755","full_name":"kentcdodds/react-test-context-provider","owner":"kentcdodds","description":"A function that allows you to specify context to pass to a child component (intended for testing only).","archived":false,"fork":false,"pushed_at":"2021-01-25T23:17:24.000Z","size":4,"stargazers_count":48,"open_issues_count":0,"forks_count":9,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-28T13:11:50.246Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/react-test-context-provider","language":"JavaScript","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/kentcdodds.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":"2016-09-13T21:39:45.000Z","updated_at":"2023-11-16T17:13:36.000Z","dependencies_parsed_at":"2022-09-17T23:21:09.126Z","dependency_job_id":null,"html_url":"https://github.com/kentcdodds/react-test-context-provider","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Freact-test-context-provider","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Freact-test-context-provider/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Freact-test-context-provider/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kentcdodds%2Freact-test-context-provider/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kentcdodds","download_url":"https://codeload.github.com/kentcdodds/react-test-context-provider/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248964329,"owners_count":21190510,"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":[],"created_at":"2024-08-03T01:02:47.104Z","updated_at":"2025-04-15T01:14:40.511Z","avatar_url":"https://github.com/kentcdodds.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# react-test-context-provider\n\nA function that allows you to specify context to pass to a child component (intended for testing only).\n\n## Installation\n\nThis module is distributed via [npm][npm] which is bundled with [node][node] and should\nbe installed as one of your project's `devDependencies`:\n\n```\nnpm install --save-dev react-test-context-provider\n```\n\n## Usage\n\n```javascript\nvar getContextProvider = require('react-test-context-provider')\nvar contextObject = {color: 'blue'} // the context you want to provide to the children components\nvar reactElement = getElementWithContext(contextObject, \u003cComponentThatNeedsContext /\u003e) // returns the react element as rendered with the given context\n```\n\nIn this example, I'm using the [Jest](http://facebook.github.io/jest/) testing framework.\n\n**Button.js**\n\n```javascript\nimport React, {PropTypes} from 'react'\nexport default function Button({children}, {color}) {\n  // function components receive context as the second argument\n  return \u003cbutton style={{background: color}}\u003e{children}\u003c/button\u003e\n}\nButton.propTypes = {children: PropTypes.any.isRequired}\nButton.contextTypes = {color: PropTypes.string}\n```\n\n**Button.test.js**\n\n```javascript\nimport React from 'react'\nimport renderer from 'react-test-renderer'\nimport getElementWithContext from 'react-test-context-provider'\nimport Button from './Button'\n\ntest('styles the button with a background of the context color', () =\u003e {\n  const element = getElementWithContext({color: 'blue'}, \u003cButton\u003eClick Me\u003c/Button\u003e)\n  const component = renderer.create(element)\n  expect(component).toMatchSnapshot()\n\n  // NOTE: This API is also curried, so you can provide the context first and the children later:\n  // import getContextProvider from 'react-test-context-provider'\n  // const getElementWithBlueColorContext = getContextProvider({color: 'blue'})\n  // const element = getElementWithBlueColorContext(\u003cButton\u003eClick Me\u003c/Button\u003e)\n})\n```\n\n## LICENSE\n\nMIT\n\n[npm]: https://www.npmjs.com/\n[node]: https://nodejs.org\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkentcdodds%2Freact-test-context-provider","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkentcdodds%2Freact-test-context-provider","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkentcdodds%2Freact-test-context-provider/lists"}