-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Raice Hannay
committed
Jul 9, 2021
1 parent
35862e7
commit 3440a70
Showing
9 changed files
with
221 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters