How to wait for Redux Persist to rehydrate before ending test
up vote
14
down vote
favorite
I have the following code:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
<ConnectedRootComponent />
</PersistGate>
</Provider>
);
};
export default App;
which uses redux-persist to rehydrate state, and before this is complete, it will show what's sitting in the loading property. I have a Jest test (just the default one that comes with react native out of the box):
it('renders without crashing', () => {
const rendered = renderer.create(<App />).toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
});
but although the test passes I see that the actions that persist/PERSIST
and persist/REHYDRATE
are still occurring, and the value printed to the console in the test (the rendered output) is:
{
"type": "Text",
"props": {
"accessible": true,
"allowFontScaling": true,
"ellipsizeMode": "tail"
},
"children": ["Loading!"]
}
What I want to do is wait until redux-persist has completed hydration, and then check the rendered value. How can I do this?
reactjs react-native jestjs redux-persist
add a comment |
up vote
14
down vote
favorite
I have the following code:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
<ConnectedRootComponent />
</PersistGate>
</Provider>
);
};
export default App;
which uses redux-persist to rehydrate state, and before this is complete, it will show what's sitting in the loading property. I have a Jest test (just the default one that comes with react native out of the box):
it('renders without crashing', () => {
const rendered = renderer.create(<App />).toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
});
but although the test passes I see that the actions that persist/PERSIST
and persist/REHYDRATE
are still occurring, and the value printed to the console in the test (the rendered output) is:
{
"type": "Text",
"props": {
"accessible": true,
"allowFontScaling": true,
"ellipsizeMode": "tail"
},
"children": ["Loading!"]
}
What I want to do is wait until redux-persist has completed hydration, and then check the rendered value. How can I do this?
reactjs react-native jestjs redux-persist
1
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31
add a comment |
up vote
14
down vote
favorite
up vote
14
down vote
favorite
I have the following code:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
<ConnectedRootComponent />
</PersistGate>
</Provider>
);
};
export default App;
which uses redux-persist to rehydrate state, and before this is complete, it will show what's sitting in the loading property. I have a Jest test (just the default one that comes with react native out of the box):
it('renders without crashing', () => {
const rendered = renderer.create(<App />).toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
});
but although the test passes I see that the actions that persist/PERSIST
and persist/REHYDRATE
are still occurring, and the value printed to the console in the test (the rendered output) is:
{
"type": "Text",
"props": {
"accessible": true,
"allowFontScaling": true,
"ellipsizeMode": "tail"
},
"children": ["Loading!"]
}
What I want to do is wait until redux-persist has completed hydration, and then check the rendered value. How can I do this?
reactjs react-native jestjs redux-persist
I have the following code:
const App = () => {
return (
<Provider store={store}>
<PersistGate persistor={persistor} loading={<Text>Loading!</Text>}>
<ConnectedRootComponent />
</PersistGate>
</Provider>
);
};
export default App;
which uses redux-persist to rehydrate state, and before this is complete, it will show what's sitting in the loading property. I have a Jest test (just the default one that comes with react native out of the box):
it('renders without crashing', () => {
const rendered = renderer.create(<App />).toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
});
but although the test passes I see that the actions that persist/PERSIST
and persist/REHYDRATE
are still occurring, and the value printed to the console in the test (the rendered output) is:
{
"type": "Text",
"props": {
"accessible": true,
"allowFontScaling": true,
"ellipsizeMode": "tail"
},
"children": ["Loading!"]
}
What I want to do is wait until redux-persist has completed hydration, and then check the rendered value. How can I do this?
reactjs react-native jestjs redux-persist
reactjs react-native jestjs redux-persist
edited Nov 12 at 20:04
skyboyer
2,80511028
2,80511028
asked Jun 13 at 4:42
user4184113
391214
391214
1
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31
add a comment |
1
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31
1
1
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
import React from 'react';
import App, {persistor} from '../App';
import renderer from 'react-test-renderer';
it('renders without crashing', (done) => {
const appRendered = renderer.create(<App />);
persistor.subscribe(function(){
const rendered = appRendered.toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
done();
});
});
you just need to export
the persistor
from App file and then use the above code to test. it works perfectly, i have tested.
You can notice that persistor
object can subscribe with a callback, which gets executed when the redux store has rehydrated.
add a comment |
up vote
0
down vote
You could mock PersistGate
to always return props.children
, then your test can check the rendered value.
Here's how to create the mock:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Credits.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
import React from 'react';
import App, {persistor} from '../App';
import renderer from 'react-test-renderer';
it('renders without crashing', (done) => {
const appRendered = renderer.create(<App />);
persistor.subscribe(function(){
const rendered = appRendered.toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
done();
});
});
you just need to export
the persistor
from App file and then use the above code to test. it works perfectly, i have tested.
You can notice that persistor
object can subscribe with a callback, which gets executed when the redux store has rehydrated.
add a comment |
up vote
2
down vote
import React from 'react';
import App, {persistor} from '../App';
import renderer from 'react-test-renderer';
it('renders without crashing', (done) => {
const appRendered = renderer.create(<App />);
persistor.subscribe(function(){
const rendered = appRendered.toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
done();
});
});
you just need to export
the persistor
from App file and then use the above code to test. it works perfectly, i have tested.
You can notice that persistor
object can subscribe with a callback, which gets executed when the redux store has rehydrated.
add a comment |
up vote
2
down vote
up vote
2
down vote
import React from 'react';
import App, {persistor} from '../App';
import renderer from 'react-test-renderer';
it('renders without crashing', (done) => {
const appRendered = renderer.create(<App />);
persistor.subscribe(function(){
const rendered = appRendered.toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
done();
});
});
you just need to export
the persistor
from App file and then use the above code to test. it works perfectly, i have tested.
You can notice that persistor
object can subscribe with a callback, which gets executed when the redux store has rehydrated.
import React from 'react';
import App, {persistor} from '../App';
import renderer from 'react-test-renderer';
it('renders without crashing', (done) => {
const appRendered = renderer.create(<App />);
persistor.subscribe(function(){
const rendered = appRendered.toJSON();
console.log("Rendering: " + JSON.stringify(rendered));
expect(rendered).toBeTruthy();
done();
});
});
you just need to export
the persistor
from App file and then use the above code to test. it works perfectly, i have tested.
You can notice that persistor
object can subscribe with a callback, which gets executed when the redux store has rehydrated.
answered Jun 22 at 16:14
Inus Saha
1,625614
1,625614
add a comment |
add a comment |
up vote
0
down vote
You could mock PersistGate
to always return props.children
, then your test can check the rendered value.
Here's how to create the mock:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Credits.
add a comment |
up vote
0
down vote
You could mock PersistGate
to always return props.children
, then your test can check the rendered value.
Here's how to create the mock:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Credits.
add a comment |
up vote
0
down vote
up vote
0
down vote
You could mock PersistGate
to always return props.children
, then your test can check the rendered value.
Here's how to create the mock:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Credits.
You could mock PersistGate
to always return props.children
, then your test can check the rendered value.
Here's how to create the mock:
jest.mock('redux-persist/integration/react', () => ({
PersistGate: props => props.children,
}))
Credits.
edited Jun 22 at 13:51
answered Jun 22 at 13:16
Jordan Enev
5,4201639
5,4201639
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50829213%2fhow-to-wait-for-redux-persist-to-rehydrate-before-ending-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can provide a callback to persistStore, which will be called when rehydration is finished. You might have to modify the test case accordingly. Please refer github.com/rt2zz/…
– dhruv soni
Jun 16 at 14:31