This repository has been archived by the owner on Nov 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Store.get for better testing
- Loading branch information
Showing
10 changed files
with
95 additions
and
64 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,13 @@ store.set({ data: { user: { login: { email: '[email protected]' } } } }); | |
// and this would affect ONLY the email if data, user and login | ||
// would have been nodes instead of objects. | ||
``` | ||
If you want to access the data directly e.g. outside any component, you can | ||
do the following: | ||
```js | ||
const state = Store.get(); | ||
``` | ||
- ATTENTION: Modifications to the returned state can effect your application in | ||
an unexpected way. Even if you know what you're doing, I do not recommend it. | ||
|
||
## Known issues | ||
First of all I want to mention, that the production functionality is not | ||
|
@@ -158,24 +165,11 @@ related mostly to `flow` issues, which were not even solved for `react-redux`. | |
|
||
- It is necessary for wired **class components** to define the props as exact | ||
type. But maybe this is even good, since you should try to always use exact | ||
prop types. | ||
prop types. `flow` itself now tries to establish a new default, that makes all | ||
objects rather exact than inexact. | ||
```js | ||
class MyComponent extends Component<{| myProp: string |}> { ... } | ||
``` | ||
- It is necessary for optional props to provide the key for this prop | ||
either in returned object from `mapStateToProps` or wherever you use | ||
the component. | ||
```js | ||
class MyComponent extends Component<{| myProp?: string, other: string |}> { ... } | ||
|
||
// Option 1: | ||
const MyWiredComponent = Store.wire<SP, OP>(MyComponent, state => ({ myProp: undefined, other: state.foo })) | ||
const rendered = <JSX><MyWiredComponent /></JSX>; | ||
|
||
// Option 2: | ||
const MyWiredComponent = Store.wire<SP, OP>(MyComponent, state => ({ other: state.foo })) | ||
const rendered = <JSX><MyWiredComponent myProp={undefined} /></JSX>; | ||
``` | ||
- Currently **default props** are not correctly recognized on wired class | ||
components. They are ignored since `React$ElementConfig` which contains | ||
the logic to calculate the props could not be connected without using | ||
|
@@ -192,7 +186,7 @@ class MyComponent extends Component<{| myProp: string |}> { | |
(see `jest` option "setupTestFrameworkScriptFile") | ||
```js | ||
Store.set = (Component, mapStateToProps) => { | ||
const result = props => <Component {...mapStateToProps(Store.data)} {...props} />; | ||
const result = props => <Component {...mapStateToProps(Store.get())} {...props} />; | ||
result.displayName = `Wired(${Component.name})`; | ||
return result; | ||
``` | ||
|
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 |
---|---|---|
@@ -1,17 +1,10 @@ | ||
// @flow | ||
|
||
import { Spy } from 'spy4js'; | ||
import 'spy4js'; | ||
import Enzyme from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import { createSerializer } from 'enzyme-to-json'; | ||
|
||
expect.addSnapshotSerializer(createSerializer({ mode: 'deep' })); | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
const oldDescribe = describe; | ||
window.describe = (string, func) => | ||
oldDescribe(string, () => { | ||
afterEach(Spy.restoreAll); | ||
return func(); | ||
}); |
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,4 @@ | ||
// @flow | ||
|
||
// necessary to test the fallback | ||
Symbol[('for': any)] = undefined; |
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,40 @@ | ||
// @flow | ||
|
||
import './wired-store-symbol-fallback'; | ||
import { Wired } from '../react-rewired'; | ||
|
||
describe('WiredStore - without Symbols', () => { | ||
const getDummyStore = () => | ||
Wired.store( | ||
({ | ||
num: 12, | ||
some: (undefined: boolean | void), | ||
bool: true, | ||
obj: { | ||
str: 'here', | ||
}, | ||
array: ['there', 12], | ||
node: Wired.node({ | ||
not: 'visible', | ||
as: 'node', | ||
}), | ||
}: any) // we need to manipulate flow, because else we can do illegal things in the tests | ||
); | ||
|
||
it('initializes the store', () => { | ||
const store = getDummyStore(); | ||
expect(store.get()).toEqual({ | ||
num: 12, | ||
bool: true, | ||
obj: { | ||
str: 'here', | ||
}, | ||
array: ['there', 12], | ||
node: { | ||
__WIRED_NODE__: true, | ||
not: 'visible', | ||
as: 'node', | ||
}, | ||
}); | ||
}); | ||
}); |
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