Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@david-mart
Created September 7, 2017 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-mart/2764b1ad99211a28575b72b19d7e7256 to your computer and use it in GitHub Desktop.
Save david-mart/2764b1ad99211a28575b72b19d7e7256 to your computer and use it in GitHub Desktop.
import { expect } from 'chai';
import {
MAP_FILTERS_CHANGED,
MAP_OPEN_DIALOG,
MAP_SET_TYPE_ID,
MAP_TOGGLE_TRAFFIC,
MAP_OPEN_INFO_WINDOW,
MAP_CLOSE_INFO_WINDOW,
MAP_VIEWPORT_CHANGE,
MAP_SET_DRAWING_MODE,
GROUPS_ADD_PROJECT_SUCCESS,
GROUPS_REMOVE_PROJECT_SUCCESS
} from '../constants/action-types';
import reducer from './map-reducer';
describe('Reducer - Map', () => {
const getInitState = () => {
return {
filters: {
species: 'Centaurian',
affiliation: 'Ravagers',
olderThan: 25
},
activeDialog: 'Groot',
mapTypeId: 'roadmap',
traffic: false,
infoWindows: [
{
projectId: 1,
project: {
id: 1,
group_ids: [1, 2, 3] // eslint-disable-line camelcase
}
},
{ projectId: 2, project: { id: 2 } }
],
viewport: {
center: { lat: 1, lng: 2 },
zoom: 100
},
drawing: {
mode: ''
}
};
};
const newFilters = {
species: 'Zehoberei',
affiliation: 'Guardians of the Galaxy'
};
const newActiveDialog = 'The Orb';
it('should set initial state by default', () => {
const action = {
type: 'UNKNOWN'
};
const expected = getInitState();
const actual = reducer(getInitState(), action);
expect(actual).deep.equals(expected);
});
describe('when MAP_FILTERS_CHANGED action is dispatched ', () => {
it('should update the filters state', () => {
const action = {
type: MAP_FILTERS_CHANGED,
filters: newFilters
};
const expected = 'Guardians of the Galaxy';
const actual = reducer(getInitState(), action).filters.affiliation;
expect(actual).equals(expected);
});
it('should leave other filters unchanged', () => {
const action = {
type: MAP_FILTERS_CHANGED,
payload: {
filters: newFilters
}
};
const expected = 25;
const actual = reducer(getInitState(), action).filters.olderThan;
expect(actual).equals(expected);
});
});
describe('when MAP_OPEN_DIALOG action is dispatched', () => {
it('should update active dialog state', () => {
const action = {
type: MAP_OPEN_DIALOG,
payload: newActiveDialog
};
const expected = 'The Orb';
const actual = reducer(getInitState(), action).activeDialog;
expect(actual).equals(expected);
});
});
describe('when MAP_SET_TYPE_ID action is dispatched', () => {
it('should update `mapTypeId` state', () => {
const action = {
type: MAP_SET_TYPE_ID,
payload: 'hybrid'
};
const expected = 'hybrid';
const actual = reducer(getInitState(), action).mapTypeId;
expect(actual).equals(expected);
});
});
describe('when MAP_TOGGLE_TRAFFIC action is dispatched', () => {
it('should toggle `traffic` state', () => {
const action = {
type: MAP_TOGGLE_TRAFFIC
};
const expected = true;
const actual = reducer(getInitState(), action).traffic;
expect(actual).equals(expected);
});
});
describe('when MAP_OPEN_INFO_WINDOW action is dispatched', () => {
it('should append new project to open info windows state', () => {
const action = {
type: MAP_OPEN_INFO_WINDOW,
payload: { project: { id: 3, name: 'The Manila Masters' } }
};
const expected = { project: { id: 3, name: 'The Manila Masters' } };
const actual = reducer(getInitState(), action).infoWindows[2];
expect(actual).deep.equals(expected);
});
});
describe('when MAP_CLOSE_INFO_WINDOW action is dispatched', () => {
it('should remove the project with selected id from the list', () => {
const action = {
type: MAP_CLOSE_INFO_WINDOW,
dataType: 'project',
payload: 2
};
const expected = 1;
const actual = reducer(getInitState(), action).infoWindows.length;
expect(actual).equals(expected);
});
});
describe('when MAP_VIEWPORT_CHANGE action is dispatched', () => {
it('should merge the new viewport data into the state\'s exisitng viewport', () => {
const action = {
type: MAP_VIEWPORT_CHANGE,
payload: {
zoom: 50
}
};
const expected = { ...getInitState().viewport, zoom: 50 };
const actual = reducer(getInitState(), action).viewport;
expect(actual).to.deep.equals(expected);
});
});
describe('when MAP_SET_DRAWING_MODE action is dispatched', () => {
it('should set new drawing mode state', () => {
const expected = 'polygon';
const action = {
type: MAP_SET_DRAWING_MODE,
mode: 'polygon'
};
const actual = reducer(getInitState(), action).drawing.mode;
expect(actual).equals(expected);
});
});
describe('when GROUPS_ADD_PROJECT_SUCCESS action is dispatched', () => {
it('should add the group id to the infowindow project group_ids array', () => {
const expected = [1, 2, 3, 5];
const action = {
type: GROUPS_ADD_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 5
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
it('should not create duplicat ids in the group_ids array', () => {
const expected = [1, 2, 3];
const action = {
type: GROUPS_ADD_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 2
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
});
describe('when GROUPS_REMOVE_PROJECT_SUCCESS action is dispatched', () => {
it('should remove the group id from the infowindow project group_ids array', () => {
const expected = [1, 3];
const action = {
type: GROUPS_REMOVE_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 2
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
});
});
import { expect } from 'chai';
import {
MAP_FILTERS_CHANGED,
MAP_OPEN_DIALOG,
MAP_SET_TYPE_ID,
MAP_TOGGLE_TRAFFIC,
MAP_OPEN_INFO_WINDOW,
MAP_CLOSE_INFO_WINDOW,
MAP_VIEWPORT_CHANGE,
MAP_SET_DRAWING_MODE,
GROUPS_ADD_PROJECT_SUCCESS,
GROUPS_REMOVE_PROJECT_SUCCESS
} from '../constants/action-types';
import reducer from './map-reducer';
describe('Reducer - Map', () => {
const getInitState = () => {
return {
filters: {
species: 'Centaurian',
affiliation: 'Ravagers',
olderThan: 25
},
activeDialog: 'Groot',
mapTypeId: 'roadmap',
traffic: false,
infoWindows: [
{
projectId: 1,
project: {
id: 1,
group_ids: [1, 2, 3] // eslint-disable-line camelcase
}
},
{ projectId: 2, project: { id: 2 } }
],
viewport: {
center: { lat: 1, lng: 2 },
zoom: 100
},
drawing: {
mode: ''
}
};
};
const newFilters = {
species: 'Zehoberei',
affiliation: 'Guardians of the Galaxy'
};
const newActiveDialog = 'The Orb';
it('should set initial state by default', () => {
const action = {
type: 'UNKNOWN'
};
const expected = getInitState();
const actual = reducer(getInitState(), action);
expect(actual).deep.equals(expected);
});
describe('when MAP_FILTERS_CHANGED action is dispatched ', () => {
it('should update the filters state', () => {
const action = {
type: MAP_FILTERS_CHANGED,
filters: newFilters
};
const expected = 'Guardians of the Galaxy';
const actual = reducer(getInitState(), action).filters.affiliation;
expect(actual).equals(expected);
});
it('should leave other filters unchanged', () => {
const action = {
type: MAP_FILTERS_CHANGED,
payload: {
filters: newFilters
}
};
const expected = 25;
const actual = reducer(getInitState(), action).filters.olderThan;
expect(actual).equals(expected);
});
});
describe('when MAP_OPEN_DIALOG action is dispatched', () => {
it('should update active dialog state', () => {
const action = {
type: MAP_OPEN_DIALOG,
payload: newActiveDialog
};
const expected = 'The Orb';
const actual = reducer(getInitState(), action).activeDialog;
expect(actual).equals(expected);
});
});
describe('when MAP_SET_TYPE_ID action is dispatched', () => {
it('should update `mapTypeId` state', () => {
const action = {
type: MAP_SET_TYPE_ID,
payload: 'hybrid'
};
const expected = 'hybrid';
const actual = reducer(getInitState(), action).mapTypeId;
expect(actual).equals(expected);
});
});
describe('when MAP_TOGGLE_TRAFFIC action is dispatched', () => {
it('should toggle `traffic` state', () => {
const action = {
type: MAP_TOGGLE_TRAFFIC
};
const expected = true;
const actual = reducer(getInitState(), action).traffic;
expect(actual).equals(expected);
});
});
describe('when MAP_OPEN_INFO_WINDOW action is dispatched', () => {
it('should append new project to open info windows state', () => {
const action = {
type: MAP_OPEN_INFO_WINDOW,
payload: { project: { id: 3, name: 'The Manila Masters' } }
};
const expected = { project: { id: 3, name: 'The Manila Masters' } };
const actual = reducer(getInitState(), action).infoWindows[2];
expect(actual).deep.equals(expected);
});
});
describe('when MAP_CLOSE_INFO_WINDOW action is dispatched', () => {
it('should remove the project with selected id from the list', () => {
const action = {
type: MAP_CLOSE_INFO_WINDOW,
dataType: 'project',
payload: 2
};
const expected = 1;
const actual = reducer(getInitState(), action).infoWindows.length;
expect(actual).equals(expected);
});
});
describe('when MAP_VIEWPORT_CHANGE action is dispatched', () => {
it('should merge the new viewport data into the state\'s exisitng viewport', () => {
const action = {
type: MAP_VIEWPORT_CHANGE,
payload: {
zoom: 50
}
};
const expected = { ...getInitState().viewport, zoom: 50 };
const actual = reducer(getInitState(), action).viewport;
expect(actual).to.deep.equals(expected);
});
});
describe('when MAP_SET_DRAWING_MODE action is dispatched', () => {
it('should set new drawing mode state', () => {
const expected = 'polygon';
const action = {
type: MAP_SET_DRAWING_MODE,
mode: 'polygon'
};
const actual = reducer(getInitState(), action).drawing.mode;
expect(actual).equals(expected);
});
});
describe('when GROUPS_ADD_PROJECT_SUCCESS action is dispatched', () => {
it('should add the group id to the infowindow project group_ids array', () => {
const expected = [1, 2, 3, 5];
const action = {
type: GROUPS_ADD_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 5
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
it('should not create duplicat ids in the group_ids array', () => {
const expected = [1, 2, 3];
const action = {
type: GROUPS_ADD_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 2
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
});
describe('when GROUPS_REMOVE_PROJECT_SUCCESS action is dispatched', () => {
it('should remove the group id from the infowindow project group_ids array', () => {
const expected = [1, 3];
const action = {
type: GROUPS_REMOVE_PROJECT_SUCCESS,
payload: {
projectId: 1,
groupId: 2
}
};
const actual = reducer(getInitState(), action).infoWindows[0].project.group_ids;
expect(actual).to.deep.equal(expected);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment