Skip to content

Commit

Permalink
fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Raice Hannay committed Jul 9, 2021
1 parent 35862e7 commit 3440a70
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ logs
coverage
dist
lib
enzyme/
react-testing-library/
/enzyme/
/react-testing-library/

#node
node_modules
Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ describe("when testing a scenario", () => {
Package contents
----------------

- [`Wrapper`](./docs/Wrapper.md)
- [`WrapperWithIntl`](./docs/WrapperWithIntl.md)
- [`WrapperWithRedux`](./docs/WrapperWithRedux.md)
- [Custom `react-testing-library` queries](./docs/react-testing-library/queries.md)
## For `enzyme`
- [`Wrapper`](/docs/enzyme/Wrapper.md)
- [`WrapperWithIntl`](/docs/enzyme/WrapperWithIntl.md)
- [`WrapperWithRedux`](/docs/enzyme/WrapperWithRedux.md)

## For `react-testing-library`
- [`Wrapper`](/docs/react-testing-library/Wrapper.md)
- [`WrapperWithIntl`](/docs/react-testing-library/WrapperWithIntl.md)
- [`WrapperWithRedux`](/docs/react-testing-library/WrapperWithRedux.md)
- [Custom `react-testing-library` queries](/docs/react-testing-library/queries.md)
22 changes: 1 addition & 21 deletions docs/Wrapper.md → docs/enzyme/Wrapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Protected abstract methods to define when extending
### `beforeMount`
This method is called before `mount`, `render` or `shallow` are called. The intention is to use this
when defining any instance properties that need to be passed as props in your `WrappingComponent`,
such as the Redux store instance (see [`WrapperWithRedux`](./WrapperWithRedux.md) for example).
such as the Redux store instance (see [`WrapperWithRedux`](WrapperWithRedux.md) for example).


Public read-only properties
Expand Down Expand Up @@ -62,7 +62,6 @@ There's a `WrappingComponent` property on the `Wrapper` class that will automati
This is very useful when testing components that require some form of context provider component to
exist in the React tree.

### For `enzyme`
```typescript jsx
import * as React from "react";
import { Wrapper as BaseWrapper } from "react-test-wrapper/enzyme";
Expand All @@ -80,22 +79,3 @@ export class WrapperWithCustomStuff<
// Add custom properties and methods here
}
```

### For `react-testing-library`
```typescript jsx
import * as React from "react";
import { Wrapper as BaseWrapper } from "react-test-wrapper/react-testing-library";

export class WrapperWithCustomStuff<
C extends React.ComponentType<any>,
P extends React.ComponentProps<C> = React.ComponentProps<C>
> extends BaseWrapper<C, P> {
protected WrappingComponent: React.FC = ({ children }) => (
<SomeProviderComponent>
{children}
</SomeProviderComponent>
);

// Add custom properties and methods here
}
```
4 changes: 2 additions & 2 deletions docs/WrapperWithIntl.md → docs/enzyme/WrapperWithIntl.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To use this class, you have to extend it and define your own `intlProviderProps`

Public methods
--------------
The public methods are identical to the base [`Wrapper`](./Wrapper.md) class.
The public methods are identical to the base [`Wrapper`](Wrapper.md) class.


How to extend for use in your project
Expand All @@ -19,7 +19,7 @@ How to extend for use in your project
```typescript jsx
import * as React from "react";
import { IntlConfig } from "react-intl";
import { WrapperWithIntl as BaseWrapper } from "react-test-wrapper";
import { WrapperWithIntl as BaseWrapper } from "react-test-wrapper/enzyme";

import messages from "./locales/en-NZ";

Expand Down
5 changes: 3 additions & 2 deletions docs/WrapperWithRedux.md → docs/enzyme/WrapperWithRedux.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dispatched as expected during interactions or in the component lifecycle.
Public methods
--------------

In addition to the public methods provided by the base [`Wrapper`](./Wrapper.md) class, the following
In addition to the public methods provided by the base [`Wrapper`](Wrapper.md) class, the following
methods are available.

Note:
Expand All @@ -76,12 +76,13 @@ Sets the scenario-specific Redux store state to be used - cleared after `mount`,
Mounts the component with the Enzyme `mount` function, using the currently-set data.
Returns a `ReactWrapper` instance, which also includes a `store` property.


How to extend for use in your project
-------------------------------------

```typescript jsx
import * as React from "react";
import { WrapperWithRedux as BaseWrapper } from "react-test-wrapper";
import { WrapperWithRedux as BaseWrapper } from "react-test-wrapper/enzyme";

import { createStore, TStoreState } from "../store";

Expand Down
74 changes: 74 additions & 0 deletions docs/react-testing-library/Wrapper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
`Wrapper`
=========

This is the base class which is used for basic presentational components. The other classes provided
extend this one, so all methods defined on this class are also available on the others.

As with all of these classes, you can extend it to implement your own additional functionality.


Protected abstract methods to define when extending
---------------------------------------------------

### `beforeRender`
This method is called before `render` is called. The intention is to use this
when defining any instance properties that need to be passed as props in your `WrappingComponent`,
such as the Redux store instance (see [`WrapperWithRedux`](./WrapperWithRedux.md) for example).


Public read-only properties
---------------------------

### `props`
The props the component was mounted with. Useful for avoiding needing to declare local variables
for later assertion reference - especially mock function instances.


Public methods
--------------

### `withDefaultChildren`
Sets the default children to be used for the wrapper instance.

### `withDefaultProps`
Sets the default props to be used for the wrapper instance.

### `withChildren`
Sets the scenario-specific children to be used - cleared afte `render` is called.

### `withProps`
Sets the scenario-specific props to be used - cleared after `render` is called.

### `render`
Mounts the component with the `react-testing-library` `render` function, using the currently-set data.
Returns the `RenderResult` returned by the `render` function.


How to extend
-------------

To extend this class to implement your own setup functionality, you can do so as shown in the
example below.

There's a `WrappingComponent` property on the `Wrapper` class that will automatically be used by
`render` when it's defined, to wrap the component being tested.
This is very useful when testing components that require some form of context provider component to
exist in the React tree.

```typescript jsx
import * as React from "react";
import { Wrapper as BaseWrapper } from "react-test-wrapper/react-testing-library";

export class WrapperWithCustomStuff<
C extends React.ComponentType<any>,
P extends React.ComponentProps<C> = React.ComponentProps<C>
> extends BaseWrapper<C, P> {
protected WrappingComponent: React.FC = ({ children }) => (
<SomeProviderComponent>
{children}
</SomeProviderComponent>
);

// Add custom properties and methods here
}
```
34 changes: 34 additions & 0 deletions docs/react-testing-library/WrapperWithIntl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
`WrapperWithIntl`
=================

This abstract class has a `WrappingComponent` defined which wraps the component in an `IntlProvider`,
passing in `intlProviderProps`.

To use this class, you have to extend it and define your own `intlProviderProps` to set your own
`messages` etc.


Public methods
--------------
The public methods are identical to the base [`Wrapper`](Wrapper.md) class.


How to extend for use in your project
-------------------------------------

```typescript jsx
import * as React from "react";
import { IntlConfig } from "react-intl";
import { WrapperWithIntl as BaseWrapper } from "react-test-wrapper/react-testing-library";

import messages from "./locales/en-NZ";

export class WrapperWithIntl<
C extends React.ComponentType<any>,
P extends React.ComponentProps<C> = React.ComponentProps<C>
> extends BaseWrapper<C, P> {
protected intlProviderProps: Partial<IntlConfig> = {
messages
};
}
```
94 changes: 94 additions & 0 deletions docs/react-testing-library/WrapperWithRedux.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
`WrapperWithRedux`
=================

This abstract class has a `WrappingComponent` defined which wraps the component in a Redux `Provider`,
passing in the return value from the `createStore` method.

To use this class, you have to extend it, pass in your store state type in the class declaration and
define your own `createStore` method to return an instance of your Redux store, using the
`initialState` and `middlewares` that this class provides - it is important to ensure that your
store uses these, because if it disregards them, none of the methods this class provides will function.

Extensions to the returned `ReduxWrapper`
-----------------------------------------

The return type of the `mount` method extends Enzyme's `ReactWrapper` by adding a store property to
it, so you can access the store instance that the component was mounted with. This is useful for
some edge cases where you may want to test how your component reacts to actions being dispatched
outside of the component's scope.

For example:
```typescript jsx
const component = new WrapperWithRedux(SomeComponent);

describe("when testing a scenario", () => {
const wrapper = component
.withReduxState({
test: {
value: "Scenario value 1"
}
})
.mount();

it("renders the initial value", () => {
expect(wrapper.find(".SomeComponent--value").text()).toBe("Initial value");
});

it("dispatches actions.setValue", () => {
wrapper.store.dispatch(actions.setValue("New value"));
});

it("renders the updated value", () => {
expect(wrapper.find(".SomeComponent--value").text()).toBe("New value");
});
});
```


Public read-only properties
---------------------------

### `reduxHistory`
An array of actions that have been dispatched, used when asserting that actions have been
dispatched as expected during interactions or in the component lifecycle.


Public methods
--------------

In addition to the public methods provided by the base [`Wrapper`](Wrapper.md) class, the following
methods are available.

### `withDefaultReduxState`
Sets the default Redux store state to be used for the wrapper instance.

### `withReduxState`
Sets the scenario-specific Redux store state to be used - cleared after `render` is called.

### `render`
Mounts the component with the `react-testing-library` `render` function, using the currently-set data.
Returns a `RenderResult` instance, which also includes a `store` property.


How to extend for use in your project
-------------------------------------

```typescript jsx
import * as React from "react";
import { WrapperWithRedux as BaseWrapper } from "react-test-wrapper/react-testing-library";

import { createStore, TStoreState } from "../store";

class Wrapper<
C extends React.ComponentType<any>,
S extends {} = TStoreState,
P extends React.ComponentProps<C> = React.ComponentProps<C>
> extends BaseWrapper<C, S, P> {
protected createStore(
initialState: DeepPartial<S>,
middlewares: Middleware[]
) {
return createStore(initialState, middlewares);
}
}
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Raice Hannay <voodoocreation@gmail.com>",
"description": "A set of classes to make setting up React components for unit tests easy.",
"license": "ISC",
"version": "3.0.3",
"version": "3.0.4",
"keywords": [
"component",
"enzyme",
Expand Down

0 comments on commit 3440a70

Please sign in to comment.