Skip to content
This repository has been archived by the owner on Apr 5, 2024. It is now read-only.

Commit

Permalink
Fix free-of-charge filter for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Nizar-Rahme authored and rBoost committed Feb 28, 2019
1 parent 8b48ec9 commit 5adf1af
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app/constants/AppConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default {
SHOW_TEST_SITE_MESSAGE: SETTINGS.SHOW_TEST_SITE_MESSAGE,
SUPPORTED_LANGUAGES: ['en', 'fi', 'sv'],
SUPPORTED_SEARCH_FILTERS: {
charge: false,
freeOfCharge: '',
date: '',
distance: '',
duration: 0,
Expand Down
6 changes: 3 additions & 3 deletions app/pages/search/controls/SearchControlsContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UnconnectedSearchControlsContainer extends Component {
hasAdvancedFilters() {
const { filters, position } = this.props;
let hasFilters = Boolean(position);
['charge', 'end', 'distance', 'duration', 'purpose', 'start', 'unit'].forEach((key) => {
['freeOfCharge', 'end', 'distance', 'duration', 'purpose', 'start', 'unit'].forEach((key) => {
if (filters[key]) {
hasFilters = true;
}
Expand Down Expand Up @@ -238,9 +238,9 @@ class UnconnectedSearchControlsContainer extends Component {
id="charge"
label={t('SearchControlsContainer.chargeLabel')}
labelClassName="app-SearchControlsCheckbox__label"
onConfirm={value => this.handleFiltersChange({ charge: value })}
onConfirm={value => this.handleFiltersChange({ freeOfCharge: value })}
toggleClassName="app-SearchControlsCheckbox__toggle"
value={filters.charge}
value={!!filters.freeOfCharge}
/>
</Col>
</Row>
Expand Down
18 changes: 10 additions & 8 deletions app/pages/search/controls/SearchControlsContainer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { UnconnectedSearchControlsContainer as SearchControlsContainer } from '.

describe('pages/search/controls/SearchControlsContainer', () => {
const history = {
push: () => {},
push: () => { },
};

const defaultProps = {
Expand All @@ -32,7 +32,7 @@ describe('pages/search/controls/SearchControlsContainer', () => {
isFetchingPurposes: false,
isFetchingUnits: false,
filters: {
charge: false,
freeOfCharge: '',
date: '2015-10-10',
duration: 0,
end: '',
Expand Down Expand Up @@ -176,12 +176,13 @@ describe('pages/search/controls/SearchControlsContainer', () => {
});

it('renders CheckboxControl with correct props', () => {
const checkboxControl = getWrapper({}).find(CheckboxControl);
const filters = { ...defaultProps.filters, freeOfCharge: '' };
const checkboxControl = getWrapper(filters).find(CheckboxControl);
expect(checkboxControl).to.have.length(1);
expect(checkboxControl.prop('id')).to.equal('charge');
expect(checkboxControl.prop('label')).to.equal('SearchControlsContainer.chargeLabel');
expect(checkboxControl.prop('onConfirm')).to.be.a('function');
expect(checkboxControl.prop('value')).to.equal(defaultProps.filters.charge);
expect(checkboxControl.prop('value')).to.be.false;
});

it('renders search Button with correct props', () => {
Expand Down Expand Up @@ -242,7 +243,8 @@ describe('pages/search/controls/SearchControlsContainer', () => {
selectControl.at(1).prop('onChange')(purpose);
expect(instance.handleFiltersChange.callCount).to.equal(1);
expect(instance.handleFiltersChange.lastCall.args[0]).to.deep.equal({
purpose: purpose.value });
purpose: purpose.value,
});
});

it('calls handleFiltersChange on unit SelectControl onChange', () => {
Expand Down Expand Up @@ -306,12 +308,12 @@ describe('pages/search/controls/SearchControlsContainer', () => {
});

it('calls handleFiltersChange on charge CheckboxControl control onConfirm', () => {
const charge = true;
const freeOfCharge = true;
expect(checkboxControl).to.have.length(1);
expect(checkboxControl.at(0).prop('onConfirm')).to.be.a('function');
checkboxControl.at(0).prop('onConfirm')(charge);
checkboxControl.at(0).prop('onConfirm')(freeOfCharge);
expect(instance.handleFiltersChange.callCount).to.equal(1);
expect(instance.handleFiltersChange.lastCall.args[0]).to.deep.equal({ charge });
expect(instance.handleFiltersChange.lastCall.args[0]).to.deep.equal({ freeOfCharge });
});
});

Expand Down
20 changes: 18 additions & 2 deletions app/state/selectors/__tests__/uiSearchFiltersSelector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import MockDate from 'mockdate';

import uiSearchFiltersSelector from 'state/selectors/uiSearchFiltersSelector';

function getState(date = '2015-10-10', start = '08:30') {
function getState(date = '2015-10-10', start = '08:30', freeOfCharge = '') {
return {
ui: {
search: {
filters: {
charge: false,
freeOfCharge,
date,
distance: '',
duration: 30,
Expand Down Expand Up @@ -46,4 +46,20 @@ describe('Selector: uiSearchFiltersSelector', () => {

expect(actual).to.deep.equal(expected);
});

describe('freeOfCharge', () => {
it('assigns true value when the correspondent argument is given a true value', () => {
const state = getState(undefined, undefined, true);
const actual = uiSearchFiltersSelector(state);

expect(actual.freeOfCharge).to.be.true;
});

it('assigns an empty string when the correspondent argument is not true', () => {
const state = getState(undefined, undefined, false);
const actual = uiSearchFiltersSelector(state);

expect(actual.freeOfCharge).to.equal('');
});
});
});
28 changes: 25 additions & 3 deletions app/state/selectors/__tests__/urlSearchFiltersSelector.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import urlSearchFiltersSelector from 'state/selectors/urlSearchFiltersSelector';

describe('Selector: urlSearchFiltersSelector', () => {
const filters = {
charge: false,
freeOfCharge: '',
date: '2015-10-10',
distance: '',
duration: '30',
Expand All @@ -20,9 +20,13 @@ describe('Selector: urlSearchFiltersSelector', () => {
municipality: '',
};

const getProps = (date = filters.date, start = filters.start) => ({
const getProps = (
date = filters.date,
start = filters.start,
freeOfCharge = filters.freeOfCharge
) => ({
location: {
search: `?charge=false&date=${date}&distance=&duration=30&end=23:30&page=1&people=&purpose=some-purpose&search=&start=${start}&unit=&useTimeRange=false`,
search: `?freeOfCharge=${freeOfCharge}&date=${date}&distance=&duration=30&end=23:30&page=1&people=&purpose=some-purpose&search=&start=${start}&unit=&useTimeRange=false`,
},
});

Expand All @@ -45,4 +49,22 @@ describe('Selector: urlSearchFiltersSelector', () => {

expect(actual).to.deep.equal(expected);
});

describe('freeOfCharge', () => {
it('assigns true value when the correspondent param is given a true value', () => {
const state = {};
const props = getProps(undefined, undefined, true);
const actual = urlSearchFiltersSelector(state, props);

expect(actual.freeOfCharge).to.be.true;
});

it('assigns an empty string when the correspondent param is not true', () => {
const state = {};
const props = getProps(undefined, undefined, false);
const actual = urlSearchFiltersSelector(state, props);

expect(actual.freeOfCharge).to.equal('');
});
});
});
2 changes: 1 addition & 1 deletion app/state/selectors/uiSearchFiltersSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const uiSearchFiltersSelector = createSelector(
omit(constants.SUPPORTED_SEARCH_FILTERS, ['lat', 'lon']),
filters,
{
charge: textBoolean(filters.charge),
freeOfCharge: textBoolean(filters.freeOfCharge) || '',
date: getDateString(filters.date),
page: parseInt(filters.page, 10) || 1,
useTimeRange: textBoolean(filters.useTimeRange),
Expand Down
2 changes: 1 addition & 1 deletion app/state/selectors/urlSearchFiltersSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const urlSearchFiltersSelector = createSelector(
omit(constants.SUPPORTED_SEARCH_FILTERS, ['lat', 'lon']),
filters,
{
charge: textBoolean(filters.charge),
freeOfCharge: textBoolean(filters.freeOfCharge) || '',
date: getDateString(filters.date),
page: parseInt(filters.page, 10) || 1,
useTimeRange: textBoolean(filters.useTimeRange),
Expand Down

0 comments on commit 5adf1af

Please sign in to comment.