import { earningsReducer } from './earnings.reducer'; import { onNextPage, onPreviousPage, selectAggregation, setAggregations, setBenchmarkMode, setTceCurrentPage, setTceKeyValues, setTceNumberOfPages, } from './earnings.actions'; import { BENCHMARK_MODE_SPOT, BENCHMARK_MODE_YEAR, SET_NUMBER_PER_PAGE } from './earnings.constants'; import { TceAggregation, TceKeyValues } from './earnings.types'; describe('Earnings reducer tests', () => { const initialState = { tce: { aggregations: [], keyValues: { current: 0, versusGlobalSpot: 0, versusOneYearTc: 0, versusPreviousAggregationPeriod: 0, versusSamePeriodLastYear: 0, }, pagination: { currentPage: 1, numberOfPages: 1, numberPerPage: 12, }, selectedAggregation: null, benchmarkMode: BENCHMARK_MODE_SPOT, }, vessels: { collection: [], poolAverage: null, filteredAverage: null, }, }; it('returns the initial state', () => { expect(earningsReducer(undefined, { type: null })).toEqual(initialState); }); it('returns state with given new tce key values for setPoolFilter action', () => { const newKeyValues = { current: 1, versusGlobalSpot: 2, versusOneYearTc: 4, versusPreviousAggregationPeriod: 8, versusSamePeriodLastYear: 15, }; const expectedState = { ...initialState, tce: { ...initialState.tce, keyValues: newKeyValues, }, }; const action = setTceKeyValues(newKeyValues); expect(earningsReducer(initialState, action)).toEqual(expectedState); }); it('returns state with updated tce key values when object doesn\'t have all props defined.', () => { const newKeyValues = { versusGlobalSpot: 2, } as TceKeyValues; const action = setTceKeyValues(newKeyValues); const reducedState = earningsReducer(initialState, action); expect(reducedState.tce.keyValues.current).toEqual(initialState.tce.keyValues.current); expect(reducedState.tce.keyValues.versusGlobalSpot).toEqual(newKeyValues.versusGlobalSpot); expect(reducedState.tce.keyValues.versusOneYearTc).toEqual(initialState.tce.keyValues.versusOneYearTc); expect(reducedState.tce.keyValues.versusPreviousAggregationPeriod).toEqual(initialState.tce.keyValues.versusPreviousAggregationPeriod); expect(reducedState.tce.keyValues.versusSamePeriodLastYear).toEqual(initialState.tce.keyValues.versusSamePeriodLastYear); }); it('returns state with new aggregations for set aggregations action.', () => { const newAggregations: TceAggregation[] = [ { date: new Date().toString(), commercial: 345, benchmark: { date: new Date().toString(), spot: 5435, year: 1234, }, id: 0, }, { date: new Date().toString(), commercial: 125, benchmark: { date: new Date().toString(), spot: 534, year: 1234, }, id: 1, }, { date: new Date().toString(), commercial: 100, benchmark: { date: new Date().toString(), spot: 43, year: 1234, }, id: 2, }, ]; const expectedState = { ...initialState, tce: { ...initialState.tce, aggregations: newAggregations, }, }; const action = setAggregations(newAggregations); expect(earningsReducer(initialState, action)).toEqual(expectedState); }); it('set new benchmark mode when set benchmark is fired', () => { const expectedState = { ...initialState, tce: { ...initialState.tce, benchmarkMode: BENCHMARK_MODE_YEAR, }, }; const action = setBenchmarkMode(BENCHMARK_MODE_YEAR); expect(earningsReducer(initialState, action)).toEqual(expectedState); }); it('sets only selected aggregation on selectAggregationAction', () => { const expectedState = { ...initialState, tce: { ...initialState.tce, selectedAggregation: 2, }, }; const action = selectAggregation(2); expect(earningsReducer(initialState, action)).toEqual(expectedState); }); it('sets current page on onNextPage action', () => { const currentState = { ...initialState, tce: { ...initialState.tce, pagination: { ...initialState.tce.pagination, numberOfPages: 3, }, }, }; const expectedState = { ...currentState, tce: { ...currentState.tce, pagination: { ...currentState.tce.pagination, currentPage: 2, }, }, }; const action = onNextPage(); expect(earningsReducer(currentState, action)).toEqual(expectedState); }); it('sets current page on onPrevPage action', () => { const currentState = { ...initialState, tce: { ...initialState.tce, pagination: { ...initialState.tce.pagination, numberOfPages: 10, currentPage: 4, }, }, }; const expectedState = { ...currentState, tce: { ...currentState.tce, pagination: { ...currentState.tce.pagination, currentPage: 3, }, }, }; const action = onPreviousPage(); expect(earningsReducer(currentState, action)).toEqual(expectedState); }); it('changes only number of pages on setTceNumberOfPages action', () => { const currentState = { ...initialState, tce: { ...initialState.tce, pagination: { ...initialState.tce.pagination, numberOfPages: 3, numberPerPage: 12, }, }, }; const expectedState = { ...currentState, tce: { ...currentState.tce, pagination: { ...currentState.tce.pagination, numberOfPages: 9, numberPerPage: 12, }, }, }; const action = setTceNumberOfPages(20); expect(earningsReducer(initialState, action)).toEqual(expectedState); }); it('sets current page number on setCurrentPage action', () => { const currentState = { ...initialState, tce: { ...initialState.tce, pagination: { ...initialState.tce.pagination, numberOfPages: 12, }, }, }; const expectedState = { ...currentState, tce: { ...currentState.tce, pagination: { ...currentState.tce.pagination, currentPage: 9, numberOfPages: 12, }, }, }; const action = setTceCurrentPage(9); expect(earningsReducer(currentState, action)).toEqual(expectedState); }); it('sets number per page when action when set number per page action is fired', () => { const expectedState = { ...initialState, tce: { ...initialState.tce, pagination: { ...initialState.tce.pagination, numberPerPage: 4, }, }, }; expect(earningsReducer(initialState, { type: SET_NUMBER_PER_PAGE, payload: 4 })).toEqual(expectedState); }); });