Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bobsilverberg committed May 3, 2018
1 parent 05526b2 commit 8b0d81c
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 18 deletions.
21 changes: 14 additions & 7 deletions src/amo/reducers/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ export const LOAD_RECOMMENDATIONS: 'LOAD_RECOMMENDATIONS'
= 'LOAD_RECOMMENDATIONS';

export type Recommendations = {|
addons: Array<ExternalAddonType>,
fallbackReason: string,
addons: Array<ExternalAddonType> | null,
fallbackReason: string | null,
loading: boolean,
outcome: string,
outcome: string | null,
|};

export type RecommendationsState = {
export type RecommendationsState = {|
byGuid: {
[guid: string]: Recommendations,
},
};
|};

export const initialState: RecommendationsState = {
byGuid: {},
Expand All @@ -40,6 +40,7 @@ type AbortFetchRecommendationsAction = {|
export const abortFetchRecommendations = ({
guid,
}: AbortFetchRecommendationsParams = {}): AbortFetchRecommendationsAction => {
invariant(guid, 'guid is required');
return {
type: ABORT_FETCH_RECOMMENDATIONS,
payload: { guid },
Expand Down Expand Up @@ -110,15 +111,15 @@ export const getRecommendationsByGuid = (
{ guid, state }: GetRecommendationsByGuidParams
): Recommendations | null => {
invariant(guid, 'guid is required');
invariant(state, 'state is required');

return state.byGuid[guid] || null;
};

type Action =
| AbortFetchRecommendationsAction
| FetchRecommendationsAction
| LoadRecommendationsAction
;
| LoadRecommendationsAction;

const reducer = (
state: RecommendationsState = initialState,
Expand All @@ -131,7 +132,10 @@ const reducer = (
byGuid: {
...state.byGuid,
[action.payload.guid]: {
addons: null,
fallbackReason: null,
loading: false,
outcome: null,
},
},
};
Expand All @@ -142,7 +146,10 @@ const reducer = (
byGuid: {
...state.byGuid,
[action.payload.guid]: {
addons: null,
fallbackReason: null,
loading: true,
outcome: null,
},
},
};
Expand Down
7 changes: 4 additions & 3 deletions src/amo/sagas/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export function* fetchRecommendations({
api: state.api, guid, recommended,
};
const recommendations = yield call(api.getRecommendations, params);
const { outcome } = recommendations;
const { fallback_reason: fallbackReason, outcome, results: addons }
= recommendations;

yield put(loadRecommendations({
addons: recommendations.results,
fallbackReason: recommendations.fallback_reason,
addons,
fallbackReason,
guid,
outcome,
}));
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/amo/api/test_recommendations.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as api from 'core/api';
import { getRecommendations } from 'amo/api/recommendations';
import { createApiResponse } from 'tests/unit/helpers';
import {
dispatchClientMetadata,
} from 'tests/unit/amo/helpers';
import { dispatchClientMetadata } from 'tests/unit/amo/helpers';


describe(__filename, () => {
Expand Down
4 changes: 1 addition & 3 deletions tests/unit/amo/reducers/test_recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ describe(__filename, () => {
}));

expect(state.byGuid[guid].loading).toEqual(true);
expect(state.byGuid[guid].addons).toEqual(undefined);
expect(state.byGuid[guid].addons).toEqual(null);
});


it('loads recommendations', () => {
const addons = [fakeAddon, fakeAddon];
const fallbackReason = 'timeout';
Expand All @@ -50,7 +49,6 @@ describe(__filename, () => {

const loadedRecommendations = getRecommendationsByGuid({ guid, state });

expect(loadedRecommendations).not.toEqual(null);
expect(loadedRecommendations).toEqual({
addons: expectedAddons,
fallbackReason,
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/amo/sagas/test_recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ import {


describe(__filename, () => {
let clientData;
let errorHandler;
let mockApi;
let sagaTester;

beforeEach(() => {
const clientData = dispatchClientMetadata();
errorHandler = createStubErrorHandler();
mockApi = sinon.mock(recommendationsApi);
clientData = dispatchClientMetadata();
sagaTester = new SagaTester({
initialState: clientData.state,
reducers: {
Expand Down

0 comments on commit 8b0d81c

Please sign in to comment.