NGXS: Testing dispatched action is not working with ofActionDispatched
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
add a comment |
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
add a comment |
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
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
angular unit-testing jasmine store ngxs
asked Nov 27 '18 at 15:31
AttGalAttGal
84
84
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
Awesome, thanks so much! Also for the async hint, thanks again!
– AttGal
Dec 3 '18 at 15:23
add a comment |
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
});
}
});
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%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
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.
Awesome, thanks so much! Also for the async hint, thanks again!
– AttGal
Dec 3 '18 at 15:23
add a comment |
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.
Awesome, thanks so much! Also for the async hint, thanks again!
– AttGal
Dec 3 '18 at 15:23
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
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%2f53502991%2fngxs-testing-dispatched-action-is-not-working-with-ofactiondispatched%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