https://github.com/mjancarik/shallow-with-context
The module is temporary workaround for passing context in shallow rendering mode
https://github.com/mjancarik/shallow-with-context
context enzyme react shallow
Last synced: about 2 months ago
JSON representation
The module is temporary workaround for passing context in shallow rendering mode
- Host: GitHub
- URL: https://github.com/mjancarik/shallow-with-context
- Owner: mjancarik
- License: mit
- Created: 2019-12-08T17:32:01.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-10-03T18:03:57.000Z (over 1 year ago)
- Last Synced: 2025-02-07T19:11:12.125Z (4 months ago)
- Topics: context, enzyme, react, shallow
- Language: JavaScript
- Homepage:
- Size: 29.3 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# shallow-with-context
[](https://travis-ci.com/mjancarik/shallow-with-context)
[](https://coveralls.io/github/mjancarik/shallow-with-context?branch=master)
[](https://www.npmjs.com/package/shallow-with-context)
[](https://github.com/prettier/prettier)The module is temporary workaround for passing context in shallow rendering mode. The new React Context API is not supported in shallow rendering mode yet.
It use under hood Legacy Context API and [to-aop](https://www.npmjs.com/package/to-aop) module.
## Installation
```bash
npm i shallow-with-context --save-dev
```## Usage
### Context is object
You don't have to use `createContext` method from `shallow-with-context` module for context as object.
``` jsx
import { shallow } from 'enzyme';
import { withContext } from 'shallow-with-context';
import React from 'react';const MyContext = React.createContext({ text: 'default' });
class Component extends React.Component {
static contextType = MyContext;
render() {
return{this.context.text}
}
}describe('your description', () => {
it('your spec', () => {
const context = { text: 'new value' };
const ComponentWithContext = withContext(Component, context);const wrapper = shallow(, { context });
expect(wrapper).toMatchInlineSnapshot('
new value');
});
});
```### Context is primitive value
You have to use `createContext` method from `shallow-with-context` module for context as primitive value because Legacy Context API don't support primitive value.
``` jsx
import { shallow } from 'enzyme';
import { withContext, createContext } from 'shallow-with-context';
import React from 'react';const MyContext = React.createContext('default');
class Component extends React.Component {
static contextType = MyContext;
render() {
return{this.context}
}
}describe('your description', () => {
it('your spec', () => {
const context = createContext('new value');
const ComponentWithContext = withContext(Component, context);const wrapper = shallow(, { context });
expect(wrapper).toMatchInlineSnapshot('
new value');
});
});
```## API
### withContext
#### Parameters- `Component` (React.Component|React.PureComponent|function|ReactObject)
- `context` Object### createContext
#### Parameters- `value` (*)