Lewis Morgans
Lewis Morgans

Reputation: 261

NGRX selector projector function returning undefined in jasmine unit test, angular 10

I am trying to project state and props into the selector. When console logging inside the selector running the test, the variables exist and work as expected. But my expect line throws errors saying expected undefined to equal ...

    fit('should return the data based on ID passed in', () => {
        const props = {
            id: '67891'
        }
        const state = {
            data: [
                { id: '12345', startDate: '2018-01-01', endDate: '2019-01-01' },
                { id: '67891', startDate: '2018-01-01', endDate: '2019-01-01' },
                { id: '14567', startDate: '2018-01-01', endDate: '2019-01-01' }
            ]
        };
        const result = state.data[1]

        expect(selectors.getDataByID.projector(state, props)).toEqual(jasmine.objectContaining(result));
    });

export const getDataByID = createSelector(
    getState,
    (state, props) => {
        const data = Object.values(state.data);
        data.find((element: any) => props.id === element.id)
    }
  );

I'm not sure what's going on, as reading through the docs on the NGRX site seems to support what I am doing here, so does previous examples across the web. The variable data is available in the selector, but in the test it says undefined?

Any help appreciated, fairly new to NGRX. Thanks.

Upvotes: 2

Views: 2756

Answers (1)

timdeschryver
timdeschryver

Reputation: 15505

Try returning something, the rest seems fine.

return data.find((element: any) => props.id === element.id)

Upvotes: 2

Related Questions