Skip to content

Commit

Permalink
Add tests for Search and RoutePanel. Use snapshot testing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Tran Dinh committed Jun 14, 2017
1 parent 47f16cc commit ea9d17d
Show file tree
Hide file tree
Showing 16 changed files with 514 additions and 57 deletions.
5 changes: 3 additions & 2 deletions src/components/Geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Geocoder extends Component {
}

componentDidMount() {
if (this.props.focusOnMount) this.input.focus();
if (this.props.focusOnMount && this.input) this.input.focus();
}

onInput(e) {
Expand Down Expand Up @@ -126,7 +126,8 @@ class Geocoder extends Component {
value={this.props.searchString}
onChange={this.onInput}
placeholder={this.props.inputPlaceholder}
type='text' />;
type='text'
/>;

return (
<div className='w-full'>
Expand Down
1 change: 1 addition & 0 deletions src/components/RoutePanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,5 @@ const mapStateToProps = (state) => {
};
};

export {RoutePanel};
export default connect(mapStateToProps)(RoutePanel);
65 changes: 65 additions & 0 deletions src/components/RoutePanel.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React from 'react';
import renderer from 'react-test-renderer';

import {RoutePanel} from './RoutePanel';
import {bikeRoute, walkingRoute} from './fixtures/RoutePanel.fixtures.js';

it('bike route renders correctly', () => {

const component = renderer.create(
<RoutePanel
mapboxAccessToken='test'
modality='bike'
route={bikeRoute}
routeStatus='idle'
/>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('walking route renders correctly', () => {

const component = renderer.create(
<RoutePanel
mapboxAccessToken='test'
modality='walk'
route={walkingRoute}
routeStatus='idle'
/>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('pending route renders correctly', () => {

const component = renderer.create(
<RoutePanel
mapboxAccessToken='test'
modality='walk'
route={null}
routeStatus='pending'
/>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('error route renders correctly', () => {

const component = renderer.create(
<RoutePanel
mapboxAccessToken='test'
modality='walk'
route={null}
routeStatus='error'
/>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});
74 changes: 43 additions & 31 deletions src/components/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,53 @@ import {triggerMapUpdate, setDirectionsLocation, getPlaceInfo, resetStateKeys, s

class Search extends Component {
render() {
let SearchBar;

if (this.props.searchLocation === null) { // no place was selected yet
SearchBar = (
<Geocoder
onSelect={(data) => this.onSelect(data)}
searchString={this.props.searchString}
writeSearch={(value) => {
this.props.triggerMapUpdate();
this.props.writeSearch(value);
}}
resultsClass={this.styles.results}
inputClass={this.styles.input}
focusOnMount={true}
/>
);

} else { // There is a selected place
SearchBar = (
<div className={this.styles.input + ' flex-parent flex-parent--center-cross flex-parent--center-main'}>
<div className='w-full w420-mm pr42 txt-truncate'>
<PlaceName
location={this.props.searchLocation}
onClick={() => {
this.props.writeSearch(this.props.searchLocation.place_name);
this.props.resetStateKeys(['searchLocation', 'placeInfo']);
}}
/>
</div>
<div
id='search-directions'
className={'mr30 cursor-pointer right ' + this.styles.icon}
onClick={() => this.clickDirections()}
>
<img src={directionsIcon} alt='directions'/>
</div>
</div>
);

}

return (
<div className={this.styles.main}>
<div className={this.styles.icon}>
<svg className='icon color-gray'><use xlinkHref='#icon-search'></use></svg>
</div>
{
(this.props.searchLocation === null)// no place was selected yet
? <Geocoder
onSelect={(data) => this.onSelect(data)}
searchString={this.props.searchString}
writeSearch={(value) => {
this.props.triggerMapUpdate();
this.props.writeSearch(value);
}}
resultsClass={this.styles.results}
inputClass={this.styles.input}
focusOnMount={true}
/>
: <div className={this.styles.input + ' flex-parent flex-parent--center-cross flex-parent--center-main'}>
<div className='w-full w420-mm pr42 txt-truncate'>
<PlaceName
location={this.props.searchLocation}
onClick={() => {
this.props.writeSearch(this.props.searchLocation.place_name);
this.props.resetStateKeys(['searchLocation', 'placeInfo']);
}}
/>
</div>
<div
className={'mr30 cursor-pointer right ' + this.styles.icon}
onClick={() => this.clickDirections()}
>
<img src={directionsIcon} alt='directions'/>
</div>
</div>
}
{SearchBar}
<CloseButton
show={(this.props.searchString !== '' || this.props.searchLocation !== null)}
onClick={() => this.closeSearch()}
Expand Down Expand Up @@ -123,6 +134,7 @@ const mapDispatchToProps = (dispatch) => {
};
};

export {Search};
export default connect(
mapStateToProps,
mapDispatchToProps
Expand Down
73 changes: 73 additions & 0 deletions src/components/Search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react';
import renderer from 'react-test-renderer';
import {shallow} from 'enzyme';
import {createStore} from 'redux';
import {Provider} from 'react-redux';
import {defaultState} from '../reducers/index';
import reducer from '../reducers/index';

import {Search} from './Search';
import ConnectedSearch from './Search';
import {placeInfo, searchLocation} from './fixtures/Search.fixtures.js';

it('renders correctly with search location', () => {

const component = renderer.create(
<Search
getPlaceInfo={() => {}}
placeInfo={placeInfo}
resetStateKeys={() => {}}
searchLocation={searchLocation}
searchString='Paris'
setDirectionsLocation={() => {}}
setMode={() => {}}
setPlaceInfo={() => {}}
setSearchLocation={() => {}}
triggerMapUpdate={() => {}}
writeSearch={() => {}}
/>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('renders correctly without search location', () => {

let store = createStore(reducer, defaultState);

const component = renderer.create(
<Provider store={store}>
<ConnectedSearch/>
</Provider>
);

let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('behaves correctly on clicking directions', () => {

const setDirectionsLocation = jest.fn();
const setMode = jest.fn();

const search = shallow(
<Search
getPlaceInfo={() => {}}
placeInfo={placeInfo}
resetStateKeys={() => {}}
searchLocation={searchLocation}
searchString='Paris'
setDirectionsLocation={setDirectionsLocation}
setMode={setMode}
setPlaceInfo={() => {}}
setSearchLocation={() => {}}
triggerMapUpdate={() => {}}
writeSearch={() => {}}
/>
);

search.find('#search-directions').simulate('click');
expect(setDirectionsLocation).toBeCalledWith('to', searchLocation);
expect(setMode).toBeCalledWith('directions');
});
1 change: 0 additions & 1 deletion src/components/StyleSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const mapDispatchToProps = (dispatch) => {
};

export {StyleSwitch};

export default connect(
mapStateToProps,
mapDispatchToProps
Expand Down
26 changes: 14 additions & 12 deletions src/components/StyleSwitch.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import React from 'react';
import ReactDOM from 'react-dom';
import renderer from 'react-test-renderer';
import {shallow} from 'enzyme';
import {StyleSwitch} from './StyleSwitch';

it('renders without crashing', () => {
const div = document.createElement('div');
it('renders correctly', () => {

ReactDOM.render(<StyleSwitch
accessToken='abc'
center={[123, -37]}
setStateValues={() => {}}
mapStyle='streets'
triggerMapUpdate={() => {}}
zoom={14}
/>, div);
const component = renderer.create(
<StyleSwitch
accessToken='abc'
center={[123, -37]}
setStateValues={() => {}}
mapStyle='streets'
triggerMapUpdate={() => {}}
zoom={14}
/>
);

expect(true).toBe(true);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('switches style on change event', () => {
Expand Down
1 change: 0 additions & 1 deletion src/components/TrafficSwitch.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ const mapDispatchToProps = (dispatch) => {
};

export {TrafficSwitch};

export default connect(
mapStateToProps,
mapDispatchToProps
Expand Down
20 changes: 11 additions & 9 deletions src/components/TrafficSwitch.spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React from 'react';
import ReactDOM from 'react-dom';
import renderer from 'react-test-renderer';
import {shallow} from 'enzyme';
import {TrafficSwitch} from './TrafficSwitch';

it('renders without crashing', () => {
const div = document.createElement('div');
it('renders correctly', () => {

ReactDOM.render(<TrafficSwitch
triggerMapUpdate={() => {}}
mapStyle='streets'
setStateValues={() => {}}
/>, div);
const component = renderer.create(
<TrafficSwitch
triggerMapUpdate={() => {}}
mapStyle='streets'
setStateValues={() => {}}
/>
);

expect(true).toBe(true);
let tree = component.toJSON();
expect(tree).toMatchSnapshot();
});

it('switches style on change event', () => {
Expand Down
Loading

0 comments on commit ea9d17d

Please sign in to comment.