NGXS: Testing dispatched action is not working with ofActionDispatched












1















I'm trying to test action dispatching, but it just doesn't work in my tests...
Component behaves fine, action is dispatched, but actions$.pipe does not catch the action, so it can't be checked in the test.



Code:



describe('ResponsiveService', () => {
let store, actions$;
const observableMediaMock = {
subscribe : (callback: (change: MediaChange) => void) => {
this.callback = callback
},
triggerMediaChange: (change: MediaChange) => this.callback(change)
};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
FlexLayoutModule,
NgxsModule.forRoot([MediaState])
],
providers: [ResponsiveService, { provide: ObservableMedia, useValue: observableMediaMock }]
});
store = TestBed.get(Store);
actions$ = TestBed.get(Actions);
});

it('should be created', inject([ResponsiveService], (service: ResponsiveService) => {
expect(service).toBeTruthy();
}));

it("should send get request", function(done) {
inject([ResponsiveService], (service: ResponsiveService) => {
const mediaQueryValue = 'mediaQuery',
mqAliasValue = 'mqAlias',
suffixValue = 'suffix';

actions$.pipe(ofActionDispatched([MediaChanged]))
.subscribe(({payload}) => {
expect(payload).toBeTruthy();
expect(payload.mediaQuery).toEqual(mediaQueryValue);
done();
});
service.init();

observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
})(); // function returned by 'inject' has to be invoked
});
});


Thanks!










share|improve this question



























    1















    I'm trying to test action dispatching, but it just doesn't work in my tests...
    Component behaves fine, action is dispatched, but actions$.pipe does not catch the action, so it can't be checked in the test.



    Code:



    describe('ResponsiveService', () => {
    let store, actions$;
    const observableMediaMock = {
    subscribe : (callback: (change: MediaChange) => void) => {
    this.callback = callback
    },
    triggerMediaChange: (change: MediaChange) => this.callback(change)
    };
    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [
    FlexLayoutModule,
    NgxsModule.forRoot([MediaState])
    ],
    providers: [ResponsiveService, { provide: ObservableMedia, useValue: observableMediaMock }]
    });
    store = TestBed.get(Store);
    actions$ = TestBed.get(Actions);
    });

    it('should be created', inject([ResponsiveService], (service: ResponsiveService) => {
    expect(service).toBeTruthy();
    }));

    it("should send get request", function(done) {
    inject([ResponsiveService], (service: ResponsiveService) => {
    const mediaQueryValue = 'mediaQuery',
    mqAliasValue = 'mqAlias',
    suffixValue = 'suffix';

    actions$.pipe(ofActionDispatched([MediaChanged]))
    .subscribe(({payload}) => {
    expect(payload).toBeTruthy();
    expect(payload.mediaQuery).toEqual(mediaQueryValue);
    done();
    });
    service.init();

    observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
    })(); // function returned by 'inject' has to be invoked
    });
    });


    Thanks!










    share|improve this question

























      1












      1








      1








      I'm trying to test action dispatching, but it just doesn't work in my tests...
      Component behaves fine, action is dispatched, but actions$.pipe does not catch the action, so it can't be checked in the test.



      Code:



      describe('ResponsiveService', () => {
      let store, actions$;
      const observableMediaMock = {
      subscribe : (callback: (change: MediaChange) => void) => {
      this.callback = callback
      },
      triggerMediaChange: (change: MediaChange) => this.callback(change)
      };
      beforeEach(() => {
      TestBed.configureTestingModule({
      imports: [
      FlexLayoutModule,
      NgxsModule.forRoot([MediaState])
      ],
      providers: [ResponsiveService, { provide: ObservableMedia, useValue: observableMediaMock }]
      });
      store = TestBed.get(Store);
      actions$ = TestBed.get(Actions);
      });

      it('should be created', inject([ResponsiveService], (service: ResponsiveService) => {
      expect(service).toBeTruthy();
      }));

      it("should send get request", function(done) {
      inject([ResponsiveService], (service: ResponsiveService) => {
      const mediaQueryValue = 'mediaQuery',
      mqAliasValue = 'mqAlias',
      suffixValue = 'suffix';

      actions$.pipe(ofActionDispatched([MediaChanged]))
      .subscribe(({payload}) => {
      expect(payload).toBeTruthy();
      expect(payload.mediaQuery).toEqual(mediaQueryValue);
      done();
      });
      service.init();

      observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
      })(); // function returned by 'inject' has to be invoked
      });
      });


      Thanks!










      share|improve this question














      I'm trying to test action dispatching, but it just doesn't work in my tests...
      Component behaves fine, action is dispatched, but actions$.pipe does not catch the action, so it can't be checked in the test.



      Code:



      describe('ResponsiveService', () => {
      let store, actions$;
      const observableMediaMock = {
      subscribe : (callback: (change: MediaChange) => void) => {
      this.callback = callback
      },
      triggerMediaChange: (change: MediaChange) => this.callback(change)
      };
      beforeEach(() => {
      TestBed.configureTestingModule({
      imports: [
      FlexLayoutModule,
      NgxsModule.forRoot([MediaState])
      ],
      providers: [ResponsiveService, { provide: ObservableMedia, useValue: observableMediaMock }]
      });
      store = TestBed.get(Store);
      actions$ = TestBed.get(Actions);
      });

      it('should be created', inject([ResponsiveService], (service: ResponsiveService) => {
      expect(service).toBeTruthy();
      }));

      it("should send get request", function(done) {
      inject([ResponsiveService], (service: ResponsiveService) => {
      const mediaQueryValue = 'mediaQuery',
      mqAliasValue = 'mqAlias',
      suffixValue = 'suffix';

      actions$.pipe(ofActionDispatched([MediaChanged]))
      .subscribe(({payload}) => {
      expect(payload).toBeTruthy();
      expect(payload.mediaQuery).toEqual(mediaQueryValue);
      done();
      });
      service.init();

      observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
      })(); // function returned by 'inject' has to be invoked
      });
      });


      Thanks!







      angular unit-testing jasmine store ngxs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 15:31









      AttGalAttGal

      84




      84
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Update your test to the following:



          it("should send get request", async( () => {
          inject([ResponsiveService], (service: ResponsiveService) => {
          const mediaQueryValue = 'mediaQuery',
          mqAliasValue = 'mqAlias',
          suffixValue = 'suffix';

          actions$.pipe(ofActionDispatched(MediaChanged))
          .subscribe(({payload}) => {
          expect(payload).toBeTruthy();
          expect(payload.mediaQuery).toEqual(mediaQueryValue);
          });
          service.init();

          observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
          })(); // function returned by 'inject' has to be invoked
          }));


          ofActionDispatched does not take a list as input, but rater takes a list of items eg.: ofActionDispatched(MediaChanged, SomeOtherAction).



          You should also be able to remove the done function and just wrap the test in a normal async block. Hope this helps, not sure if I opened and closed all the brackets correctly.






          share|improve this answer
























          • Awesome, thanks so much! Also for the async hint, thanks again!

            – AttGal
            Dec 3 '18 at 15:23













          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',
          autoActivateHeartbeat: false,
          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%2f53502991%2fngxs-testing-dispatched-action-is-not-working-with-ofactiondispatched%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Update your test to the following:



          it("should send get request", async( () => {
          inject([ResponsiveService], (service: ResponsiveService) => {
          const mediaQueryValue = 'mediaQuery',
          mqAliasValue = 'mqAlias',
          suffixValue = 'suffix';

          actions$.pipe(ofActionDispatched(MediaChanged))
          .subscribe(({payload}) => {
          expect(payload).toBeTruthy();
          expect(payload.mediaQuery).toEqual(mediaQueryValue);
          });
          service.init();

          observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
          })(); // function returned by 'inject' has to be invoked
          }));


          ofActionDispatched does not take a list as input, but rater takes a list of items eg.: ofActionDispatched(MediaChanged, SomeOtherAction).



          You should also be able to remove the done function and just wrap the test in a normal async block. Hope this helps, not sure if I opened and closed all the brackets correctly.






          share|improve this answer
























          • Awesome, thanks so much! Also for the async hint, thanks again!

            – AttGal
            Dec 3 '18 at 15:23


















          0














          Update your test to the following:



          it("should send get request", async( () => {
          inject([ResponsiveService], (service: ResponsiveService) => {
          const mediaQueryValue = 'mediaQuery',
          mqAliasValue = 'mqAlias',
          suffixValue = 'suffix';

          actions$.pipe(ofActionDispatched(MediaChanged))
          .subscribe(({payload}) => {
          expect(payload).toBeTruthy();
          expect(payload.mediaQuery).toEqual(mediaQueryValue);
          });
          service.init();

          observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
          })(); // function returned by 'inject' has to be invoked
          }));


          ofActionDispatched does not take a list as input, but rater takes a list of items eg.: ofActionDispatched(MediaChanged, SomeOtherAction).



          You should also be able to remove the done function and just wrap the test in a normal async block. Hope this helps, not sure if I opened and closed all the brackets correctly.






          share|improve this answer
























          • Awesome, thanks so much! Also for the async hint, thanks again!

            – AttGal
            Dec 3 '18 at 15:23
















          0












          0








          0







          Update your test to the following:



          it("should send get request", async( () => {
          inject([ResponsiveService], (service: ResponsiveService) => {
          const mediaQueryValue = 'mediaQuery',
          mqAliasValue = 'mqAlias',
          suffixValue = 'suffix';

          actions$.pipe(ofActionDispatched(MediaChanged))
          .subscribe(({payload}) => {
          expect(payload).toBeTruthy();
          expect(payload.mediaQuery).toEqual(mediaQueryValue);
          });
          service.init();

          observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
          })(); // function returned by 'inject' has to be invoked
          }));


          ofActionDispatched does not take a list as input, but rater takes a list of items eg.: ofActionDispatched(MediaChanged, SomeOtherAction).



          You should also be able to remove the done function and just wrap the test in a normal async block. Hope this helps, not sure if I opened and closed all the brackets correctly.






          share|improve this answer













          Update your test to the following:



          it("should send get request", async( () => {
          inject([ResponsiveService], (service: ResponsiveService) => {
          const mediaQueryValue = 'mediaQuery',
          mqAliasValue = 'mqAlias',
          suffixValue = 'suffix';

          actions$.pipe(ofActionDispatched(MediaChanged))
          .subscribe(({payload}) => {
          expect(payload).toBeTruthy();
          expect(payload.mediaQuery).toEqual(mediaQueryValue);
          });
          service.init();

          observableMediaMock.triggerMediaChange(new MediaChange(null, mediaQueryValue, mqAliasValue, suffixValue));
          })(); // function returned by 'inject' has to be invoked
          }));


          ofActionDispatched does not take a list as input, but rater takes a list of items eg.: ofActionDispatched(MediaChanged, SomeOtherAction).



          You should also be able to remove the done function and just wrap the test in a normal async block. Hope this helps, not sure if I opened and closed all the brackets correctly.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 30 '18 at 10:06









          salomonvhsalomonvh

          869914




          869914













          • Awesome, thanks so much! Also for the async hint, thanks again!

            – AttGal
            Dec 3 '18 at 15:23





















          • Awesome, thanks so much! Also for the async hint, thanks again!

            – AttGal
            Dec 3 '18 at 15:23



















          Awesome, thanks so much! Also for the async hint, thanks again!

          – AttGal
          Dec 3 '18 at 15:23







          Awesome, thanks so much! Also for the async hint, thanks again!

          – AttGal
          Dec 3 '18 at 15:23






















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53502991%2fngxs-testing-dispatched-action-is-not-working-with-ofactiondispatched%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

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks