Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more tests #12

Merged
merged 2 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"prettier/prettier": ["error", { "singleQuote": true, "tabWidth": 4 }]
},
"globals": {
"shallow": true
"shallow": true,
"mount": true
},
"settings": {
"react": {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/node_modules
/dist
/coverage
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
node_modules/
__tests__/
__mocks__/
coverage/
.babelrc
.eslintrc.json
.gitignore
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ install:

script:
- npm run lint
- npm run test
- npm run coverage
- npm run build
14 changes: 14 additions & 0 deletions __mocks__/MockLeafletPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const setStartAngleSpy = jest.fn();
const setStopAngleSpy = jest.fn();
const setDirectionSpy = jest.fn();

export default jest.fn().mockImplementation(() => {
return {
setStartAngle: setStartAngleSpy,
setStopAngle: setStopAngleSpy,
setDirection: setDirectionSpy,
_layerAdd: () => {}
};
});

export { setStartAngleSpy, setStopAngleSpy, setDirectionSpy };
92 changes: 87 additions & 5 deletions __tests__/AbstractComponent.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
import React from 'react';
import React, { cloneElement, createRef } from 'react';
import { Map, withLeaflet } from 'react-leaflet';
import AbstractComponent from '../src/AbstractComponent';
import MockLeafletPlugin, {
setStartAngleSpy,
setStopAngleSpy,
setDirectionSpy
} from '../__mocks__/MockLeafletPlugin';

const MockComponent = withLeaflet(
class MockComponent extends AbstractComponent {
get leafletComponent() {
return MockLeafletPlugin;
}
}
);

function updateProps(wrapper, props) {
wrapper.setProps({
children: cloneElement(wrapper.props().children, props)
});
}

const mockOptions = {
position: [51.505, -0.09],
radius: 2000,
startAngle: 90,
stopAngle: 180,
color: 'white'
};
let wrapper;
let testRef;

describe('<AbstractComponent />', () => {
it('should throw an error when leafletComponent is not implemented', () => {
expect(() => {
shallow(<AbstractComponent />);
}).toThrowError('leafletComponent getter not implemented');
beforeEach(() => {
MockLeafletPlugin.mockClear();
setStartAngleSpy.mockClear();
setStopAngleSpy.mockClear();
setDirectionSpy.mockClear();
testRef = createRef();
wrapper = mount(
<Map>
<MockComponent {...mockOptions} ref={testRef} />
</Map>
);
});
describe('constructor', () => {
it('should throw an error when leafletComponent is not implemented', () => {
expect(() => {
shallow(<AbstractComponent />);
}).toThrowError('leafletComponent getter not implemented');
});
it('should call the leaflet plugin constructor with the expected arguments', () => {
const { position, ...options } = mockOptions;
expect(MockLeafletPlugin).toHaveBeenCalledWith(position, options);
});
});

describe('prop changes', () => {
it('should call the setStartAngle method if props change', () => {
updateProps(wrapper, { startAngle: 100 });
expect(setStartAngleSpy).toHaveBeenCalledWith(100);
});
it('should call the setStopAngle method if props change', () => {
updateProps(wrapper, { stopAngle: 200 });
expect(setStopAngleSpy).toHaveBeenCalledWith(200);
});
it('should not call the setStartAngle method if props do not change', () => {
updateProps(wrapper, { startAngle: 90 });
expect(setStartAngleSpy).toHaveBeenCalledTimes(0);
});
it('should not call the setStopAngle method if props do not change', () => {
updateProps(wrapper, { stopAngle: 180 });
expect(setStopAngleSpy).toHaveBeenCalledTimes(0);
});
});

describe('ref methods', () => {
it('should expose the setStartAngle ref method', () => {
testRef.current.setStartAngle(30);
expect(setStartAngleSpy).toHaveBeenCalledWith(30);
});
it('should expose the setStopAngle ref method', () => {
testRef.current.setStopAngle(30);
expect(setStopAngleSpy).toHaveBeenCalledWith(30);
});
it('should expose the setDirection ref method', () => {
testRef.current.setDirection(30, 60);
expect(setDirectionSpy).toHaveBeenCalledWith(30, 60);
});
});
});
10 changes: 9 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module.exports = {
setupFiles: ['<rootDir>/test.config.js']
setupFiles: ['<rootDir>/test.config.js'],
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100
}
}
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "src/index.js",
"scripts": {
"test": "jest",
"coverage": "jest --coverage",
"lint": "eslint ./src/**/*.js ./__tests__/**/*.js",
"changelog": "auto-changelog -p --tag-prefix v",
"release": "np",
Expand Down
11 changes: 8 additions & 3 deletions src/AbstractComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ import 'leaflet-semicircle';

export default class AbstractComponent extends Path {
createLeafletElement(props) {
const { position, ...options } = props;
// props.leaflet is not used but destructured out so it's not passed to this.leafletComponent
const { position, leaflet, ...options } = props; // eslint-disable-line no-unused-vars
return new this.leafletComponent(position, options);
}

updateLeafletElement(fromProps, { startAngle, stopAngle }) {
this.leafletElement.setStartAngle(startAngle);
this.leafletElement.setStopAngle(stopAngle);
if (fromProps.startAngle !== startAngle) {
this.setStartAngle(startAngle);
}
if (fromProps.stopAngle !== stopAngle) {
this.setStopAngle(stopAngle);
}
}

setDirection(direction, size) {
Expand Down