How to test ngrx pipe select in Rxjs 6
Previously I could test the below store select
this.store.select(fromRoot.someselector).pipe(map(r => this.store.dispatch(new Action())));
This was in my test
{provide: Store, useValue: jasmine.createSpyObj('store', ['select']);
store.select.and.returnValue(of());
But now it has changed to pipes
this.store.pipe(select(fromRoot.someselector));
this.store.pipe(
select(fromRoot.someselector),
filter(result => !!result),
map(r =>
if (result === ' hello')
this.store.dispatch(new Action());
));
How to test this especially when you have map after select and within it you are dispatching an action and you want verify action was called.
angular typescript jasmine ngrx
add a comment |
Previously I could test the below store select
this.store.select(fromRoot.someselector).pipe(map(r => this.store.dispatch(new Action())));
This was in my test
{provide: Store, useValue: jasmine.createSpyObj('store', ['select']);
store.select.and.returnValue(of());
But now it has changed to pipes
this.store.pipe(select(fromRoot.someselector));
this.store.pipe(
select(fromRoot.someselector),
filter(result => !!result),
map(r =>
if (result === ' hello')
this.store.dispatch(new Action());
));
How to test this especially when you have map after select and within it you are dispatching an action and you want verify action was called.
angular typescript jasmine ngrx
add a comment |
Previously I could test the below store select
this.store.select(fromRoot.someselector).pipe(map(r => this.store.dispatch(new Action())));
This was in my test
{provide: Store, useValue: jasmine.createSpyObj('store', ['select']);
store.select.and.returnValue(of());
But now it has changed to pipes
this.store.pipe(select(fromRoot.someselector));
this.store.pipe(
select(fromRoot.someselector),
filter(result => !!result),
map(r =>
if (result === ' hello')
this.store.dispatch(new Action());
));
How to test this especially when you have map after select and within it you are dispatching an action and you want verify action was called.
angular typescript jasmine ngrx
Previously I could test the below store select
this.store.select(fromRoot.someselector).pipe(map(r => this.store.dispatch(new Action())));
This was in my test
{provide: Store, useValue: jasmine.createSpyObj('store', ['select']);
store.select.and.returnValue(of());
But now it has changed to pipes
this.store.pipe(select(fromRoot.someselector));
this.store.pipe(
select(fromRoot.someselector),
filter(result => !!result),
map(r =>
if (result === ' hello')
this.store.dispatch(new Action());
));
How to test this especially when you have map after select and within it you are dispatching an action and you want verify action was called.
angular typescript jasmine ngrx
angular typescript jasmine ngrx
edited Nov 15 '18 at 14:21
Angad
asked Nov 15 '18 at 8:23
AngadAngad
2741620
2741620
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Skip the operators and test directly the result of the stream :
store
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))
To explain further :
- create a mock of your store
- create a mock of your value
- return this mocked value in your mocked store
- expect your component variable to return the mocked value
This gives :
const mockedValue = id: 1 ;
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore =
pipe: () => storeSubjectMock.asObservable(),
;
// provide: Sotre, useValue: mockedStore ; in your testbed
it('should return an unaltered value', () =>
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(mockedValue))
);
Now the good thing about that is that you can test all operators like that. Say your component variable is
storeValue$ = this.store.pipe(
select('selector'),
map(value => ( ...value, name: 'customName' ))
)
Then your tests just changes to
it('should return an altered value with a name property set to customName', () => {
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toEqual( ...mockedValue, name: 'customName'))
);
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
|
show 2 more comments
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%2f53315111%2fhow-to-test-ngrx-pipe-select-in-rxjs-6%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
Skip the operators and test directly the result of the stream :
store
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))
To explain further :
- create a mock of your store
- create a mock of your value
- return this mocked value in your mocked store
- expect your component variable to return the mocked value
This gives :
const mockedValue = id: 1 ;
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore =
pipe: () => storeSubjectMock.asObservable(),
;
// provide: Sotre, useValue: mockedStore ; in your testbed
it('should return an unaltered value', () =>
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(mockedValue))
);
Now the good thing about that is that you can test all operators like that. Say your component variable is
storeValue$ = this.store.pipe(
select('selector'),
map(value => ( ...value, name: 'customName' ))
)
Then your tests just changes to
it('should return an altered value with a name property set to customName', () => {
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toEqual( ...mockedValue, name: 'customName'))
);
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
|
show 2 more comments
Skip the operators and test directly the result of the stream :
store
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))
To explain further :
- create a mock of your store
- create a mock of your value
- return this mocked value in your mocked store
- expect your component variable to return the mocked value
This gives :
const mockedValue = id: 1 ;
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore =
pipe: () => storeSubjectMock.asObservable(),
;
// provide: Sotre, useValue: mockedStore ; in your testbed
it('should return an unaltered value', () =>
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(mockedValue))
);
Now the good thing about that is that you can test all operators like that. Say your component variable is
storeValue$ = this.store.pipe(
select('selector'),
map(value => ( ...value, name: 'customName' ))
)
Then your tests just changes to
it('should return an altered value with a name property set to customName', () => {
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toEqual( ...mockedValue, name: 'customName'))
);
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
|
show 2 more comments
Skip the operators and test directly the result of the stream :
store
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))
To explain further :
- create a mock of your store
- create a mock of your value
- return this mocked value in your mocked store
- expect your component variable to return the mocked value
This gives :
const mockedValue = id: 1 ;
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore =
pipe: () => storeSubjectMock.asObservable(),
;
// provide: Sotre, useValue: mockedStore ; in your testbed
it('should return an unaltered value', () =>
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(mockedValue))
);
Now the good thing about that is that you can test all operators like that. Say your component variable is
storeValue$ = this.store.pipe(
select('selector'),
map(value => ( ...value, name: 'customName' ))
)
Then your tests just changes to
it('should return an altered value with a name property set to customName', () => {
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toEqual( ...mockedValue, name: 'customName'))
);
Skip the operators and test directly the result of the stream :
store
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(theMockedValSentToTheSpy))
To explain further :
- create a mock of your store
- create a mock of your value
- return this mocked value in your mocked store
- expect your component variable to return the mocked value
This gives :
const mockedValue = id: 1 ;
const storeSubjectMock = new BehaviorSubject(mockedValue);
const mockedStore =
pipe: () => storeSubjectMock.asObservable(),
;
// provide: Sotre, useValue: mockedStore ; in your testbed
it('should return an unaltered value', () =>
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toBe(mockedValue))
);
Now the good thing about that is that you can test all operators like that. Say your component variable is
storeValue$ = this.store.pipe(
select('selector'),
map(value => ( ...value, name: 'customName' ))
)
Then your tests just changes to
it('should return an altered value with a name property set to customName', () => {
component.variableReferencingStore
.pipe(select('selector'))
.subscribe(val => expect(val).toEqual( ...mockedValue, name: 'customName'))
);
edited Nov 15 '18 at 8:55
answered Nov 15 '18 at 8:39
trichetrichetrichetriche
28k42359
28k42359
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
|
show 2 more comments
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
Can you elaborate? I have to provide store in my test and spy on pipe so question is what would i returnValue of pipe ...store.pipe.and.returnValue(of()); Also, there is some logic going on in my pipe as shown in the example e.g. map() ..I want to test that also
– Angad
Nov 15 '18 at 8:46
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
@Angad I have updated my answer.
– trichetriche
Nov 15 '18 at 8:55
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
No sure if I fully understand it. How would this be test this.store.pipe(select(fromRoot.someselector), filter(result => !!result), map(r => if (result === ' hello') this.store.dispatch(new Action()); ));
– Angad
Nov 15 '18 at 14:20
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
I have edited the store statement with a bit complex one to get an idea how that would be tested. Can you please tell me how to go about this one?
– Angad
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
If you don't know how to test your code, I suggest you make another question. I'm not answering you on that through comments ...
– trichetriche
Nov 15 '18 at 14:22
|
show 2 more comments
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%2f53315111%2fhow-to-test-ngrx-pipe-select-in-rxjs-6%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