Skip to content

Mocking NGRX Factory Selectors #3279

Discussion options

You must be logged in to vote

Try this:

// customer.selectors.ts
export const selectCustomer = (id: string) =>
  createSelector(selectCustomers, (customers) => customers[id]);

// spec.ts
import * as customerSelectors from './customer.selectors';

it('should mock factory selector', (done) => {
  const customerMock = { id: '1', name: 'c1' };
  spyOn(customerSelectors, 'selectCustomer').and.returnValue(() => customerMock);
  
  const store = TestBed.inject(MockStore);
  store.select(customerSelectors.selectCustomer('1')).subscribe((customer) => {
    expect(customer).toBe(customerMock);
    done();
  });
});

Note: If you're using Jasmine, set "module": "commonjs" in tsconfig.spec.json.

"compilerOptions": {
  "module": "…

Replies: 24 comments 8 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by timdeschryver
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@david-shortman
Comment options

@david-shortman
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@david-shortman
Comment options

@silver-mx
Comment options

@david-shortman
Comment options

@david-shortman
Comment options

@david-shortman
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Converted from issue

This discussion was converted from issue #3107 on January 06, 2022 17:31.