How to wait for Redux Persist to rehydrate before ending test











up vote
14
down vote

favorite
1












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?










share|improve this question




















  • 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















up vote
14
down vote

favorite
1












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?










share|improve this question




















  • 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













up vote
14
down vote

favorite
1









up vote
14
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer




























    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.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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

























      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.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 22 at 16:14









          Inus Saha

          1,625614




          1,625614
























              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.






              share|improve this answer



























                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.






                share|improve this answer

























                  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.






                  share|improve this answer














                  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.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Jun 22 at 13:51

























                  answered Jun 22 at 13:16









                  Jordan Enev

                  5,4201639




                  5,4201639






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents