Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update state in mockstore in ngrx unit testing?

I have a object defined as:

obj:{
  name:"string",
  system:"string"
}

however I am trying to update the value in mockstore as:

store.setState({
  name:"Rose",
  system:"Updated Dummy"
});
store.refreshState();
fixture.detectChanges();

but the value is not updating. How can I update the value and then verify that it has been updated as a part of unit testing?

like image 611
RIni Avatar asked Oct 22 '25 07:10

RIni


1 Answers

store.setState updates the whole state. Therefore you need to use its feature name too to repeat the real store structure.

const featureSelector = createFeatureSelector('FEATURE_NAME_IS_HERE');

store.setState({
  FEATURE_NAME_IS_HERE: { // <- adding the feature state.
    name:"Rose",
    system:"Updated Dummy"
  },
});
store.refreshState();
fixture.detectChanges();

If you use nested reducers - then you need to nest the object to de desired child.

like image 66
satanTime Avatar answered Oct 23 '25 20:10

satanTime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!