{"id":15416745,"url":"https://github.com/mjancarik/shallow-with-context","last_synced_at":"2025-04-19T14:34:09.025Z","repository":{"id":57357875,"uuid":"226705397","full_name":"mjancarik/shallow-with-context","owner":"mjancarik","description":"The module is temporary workaround for passing context in shallow rendering mode","archived":false,"fork":false,"pushed_at":"2023-10-03T18:03:57.000Z","size":30,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-07T19:11:12.125Z","etag":null,"topics":["context","enzyme","react","shallow"],"latest_commit_sha":null,"homepage":"","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/mjancarik.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-08T17:32:01.000Z","updated_at":"2022-11-08T14:25:43.000Z","dependencies_parsed_at":"2024-06-18T22:59:57.193Z","dependency_job_id":"ee030a1e-4dd4-4072-9f6f-aa6694fb0a1b","html_url":"https://github.com/mjancarik/shallow-with-context","commit_stats":{"total_commits":49,"total_committers":2,"mean_commits":24.5,"dds":0.04081632653061229,"last_synced_commit":"df90fa45bd1226ab03cadca4ed86b2a4581d4879"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjancarik%2Fshallow-with-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjancarik%2Fshallow-with-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjancarik%2Fshallow-with-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mjancarik%2Fshallow-with-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mjancarik","download_url":"https://codeload.github.com/mjancarik/shallow-with-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240035944,"owners_count":19737613,"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":["context","enzyme","react","shallow"],"created_at":"2024-10-01T17:13:43.509Z","updated_at":"2025-03-01T23:30:54.046Z","avatar_url":"https://github.com/mjancarik.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# shallow-with-context\n\n[![Build Status](https://travis-ci.com/mjancarik/shallow-with-context.svg?branch=master)](https://travis-ci.com/mjancarik/shallow-with-context)\n[![Coverage Status](https://coveralls.io/repos/github/mjancarik/shallow-with-context/badge.svg?branch=master)](https://coveralls.io/github/mjancarik/shallow-with-context?branch=master)\n[![NPM package version](https://img.shields.io/npm/v/shallow-with-context/latest.svg)](https://www.npmjs.com/package/shallow-with-context)\n[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)\n\nThe module is temporary workaround for passing context in shallow rendering mode. The new React Context API is not supported in shallow rendering mode yet.\n\nIt use under hood Legacy Context API and [to-aop](https://www.npmjs.com/package/to-aop) module.\n\n## Installation\n\n```bash\nnpm i shallow-with-context --save-dev\n```\n\n## Usage\n\n### Context is object\n\nYou don't have to use `createContext` method from `shallow-with-context` module for context as object.  \n\n``` jsx\nimport { shallow } from 'enzyme';\nimport { withContext } from 'shallow-with-context';\nimport React from 'react';\n\nconst MyContext = React.createContext({ text: 'default' });\nclass Component extends React.Component {\n  static contextType = MyContext;\n  render() {\n    return \u003cdiv\u003e{this.context.text}\u003c/div\u003e\n  }\n}\n\ndescribe('your description', () =\u003e {\n  it('your spec', () =\u003e {\n    const context = { text: 'new value' };\n    const ComponentWithContext = withContext(Component, context);\n\n    const wrapper = shallow(\u003cComponentWithContext /\u003e, { context });\n\n    expect(wrapper).toMatchInlineSnapshot('\u003cdiv\u003enew value\u003c/div\u003e');\n  });\n});\n```\n\n### Context is primitive value\n\nYou have to use `createContext` method from `shallow-with-context` module for context as primitive value because Legacy Context API don't support primitive value. \n\n``` jsx\nimport { shallow } from 'enzyme';\nimport { withContext, createContext } from 'shallow-with-context';\nimport React from 'react';\n\nconst MyContext = React.createContext('default');\nclass Component extends React.Component {\n  static contextType = MyContext;\n  render() {\n    return \u003cdiv\u003e{this.context}\u003c/div\u003e\n  }\n}\n\ndescribe('your description', () =\u003e {\n  it('your spec', () =\u003e {\n    const context = createContext('new value');\n    const ComponentWithContext = withContext(Component, context);\n\n    const wrapper = shallow(\u003cComponentWithContext /\u003e, { context });\n\n    expect(wrapper).toMatchInlineSnapshot('\u003cdiv\u003enew value\u003c/div\u003e');\n  });\n});\n```\n\n## API\n### withContext\n#### Parameters\n\n-   `Component` (React.Component|React.PureComponent|function|ReactObject)\n-   `context` Object\u003cstring, *\u003e\n\n### createContext\n#### Parameters\n\n-   `value` (*)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjancarik%2Fshallow-with-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmjancarik%2Fshallow-with-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmjancarik%2Fshallow-with-context/lists"}