Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
760 views
in Technique[技术] by (71.8m points)

angular - NgRX provideMockStore createSelector state undefined

When using the provide mockStore, my feature/selector functions only see state as undefined.

Due to this, I am only able to mock the selectors and cannot therefore perform a more fully integrated test.

Are selectors not supposed to see state when it's provided by the mock store or should this work?

Creating the mock store

     TestBed.configureTestingModule({
        providers: [
            AuthGuardGuard,
            provideMockStore({
                initialState: { ...initialState },
            }),
        ],
    });

Then logging the state that the selector should see, which is undefined.

export const currentUser = createSelector(
    authRootFeature,
    (state: fromAuthRoot.State) => {
        console.log(state); // Undefined
        return state.auth.currentUser;
    }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

MockStore should be setting the state, I'll share with you two implementations that work and you can choose which one you prefer, there's missing info in your question so it's hard to point out why this is not working for you.

Here's one:

My selector:

export const selectFeature = createFeatureSelector<State, FeatureState>('feature');

export const getFeatureProperty = createSelector(
  selectFeature,
  (state) => state.featureProperty
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...