Skip to content

Commit

Permalink
Merge pull request #300 from ddarren/ds-test-coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbieh committed Jun 1, 2023
2 parents b2982e2 + 5c29f71 commit 628c801
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/findNearest.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import findNearest from './findNearest';

describe('findNearest', () => {
it('returns first result from orderByDistance', () => {
const point = { latitude: 51.516241842, longitude: 7.456494328 };
const nearest = { latitude: 51.515400598, longitude: 7.45518541 };
const coords = [
{ latitude: 51.513357512, longitude: 7.45574331 },
nearest,
{ latitude: 51.516722545, longitude: 7.459863183 },
{ latitude: 51.517443592, longitude: 7.463232037 },
];

expect(findNearest(point, coords)).toEqual(nearest);
});
});
40 changes: 40 additions & 0 deletions src/getRoughCompassDirection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import getRoughCompassDirection from './getRoughCompassDirection';

describe('getRoughCompassDirection', () => {
describe('when exact compass direction is Northern', () => {
it('returns N', () => {
['NNE', 'NE', 'NNW', 'N'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'N'
);
});
});
});
describe('when exact compass direction is Eastern', () => {
it('returns E', () => {
['ENE', 'E', 'ESE', 'SE'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'E'
);
});
});
});
describe('when exact compass direction is Southern', () => {
it('returns S', () => {
['SSE', 'S', 'SSW', 'SW'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'S'
);
});
});
});
describe('when exact compass direction is Western', () => {
it('returns W', () => {
['WSW', 'W', 'WNW', 'NW'].forEach((exactCompassDirection) => {
expect(getRoughCompassDirection(exactCompassDirection)).toEqual(
'W'
);
});
});
});
});
8 changes: 4 additions & 4 deletions src/getRoughCompassDirection.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// Receives an exact compass direction (like WNW) and spits out a very rough
// and overly simplified direction (N|E|S|W). Use with caution!
const getRoughCompassDirection = (exact: string) => {
if (/^NNE|NE|NNW|N$/.test(exact)) {
if (/^(NNE|NE|NNW|N)$/.test(exact)) {
return 'N';
}

if (/^ENE|E|ESE|SE$/.test(exact)) {
if (/^(ENE|E|ESE|SE)$/.test(exact)) {
return 'E';
}

if (/^SSE|S|SSW|SW$/.test(exact)) {
if (/^(SSE|S|SSW|SW)$/.test(exact)) {
return 'S';
}

if (/^WSW|W|WNW|NW$/.test(exact)) {
if (/^(WSW|W|WNW|NW)$/.test(exact)) {
return 'W';
}
};
Expand Down
43 changes: 43 additions & 0 deletions src/isValidLatitude.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import isValidLatitude from './isValidLatitude';
import { MAXLAT, MINLAT } from './constants';

describe('isValidLatitude', () => {
describe('when value is a decimal', () => {
describe('when value is between MINLAT and MAXLAT', () => {
it('returns true', () => {
let value = MAXLAT - 1;
expect(isValidLatitude(value)).toEqual(true);
value = MINLAT + 1;
expect(isValidLatitude(value)).toEqual(true);
});
});
describe('when value is not between MINLAT and MAXLAT', () => {
it('returns false', () => {
let value = MAXLAT + 1;
expect(isValidLatitude(value)).toEqual(false);
value = MINLAT - 1;
expect(isValidLatitude(value)).toEqual(false);
});
});
});
describe('when value is a sexagesimal', () => {
describe('when value is between MINLAT and MAXLAT', () => {
it('returns true', () => {
const value = '51° 31\' 10.11" N';
expect(isValidLatitude(value)).toEqual(true);
});
});
describe('when value is not between MINLAT and MAXLAT', () => {
it('returns false', () => {
const value = '121°26′31″W';
expect(isValidLatitude(value)).toEqual(false);
});
});
});
describe('when value is not a decimal or sexagesimal', () => {
it('returns false', () => {
const value = 'foo';
expect(isValidLatitude(value)).toEqual(false);
});
});
});
1 change: 0 additions & 1 deletion src/isValidLatitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import isSexagesimal from './isSexagesimal';
import sexagesimalToDecimal from './sexagesimalToDecimal';
import { MAXLAT, MINLAT } from './constants';

// TODO: Add tests
const isValidLatitude = (value: any): boolean => {
if (isDecimal(value)) {
if (parseFloat(value) > MAXLAT || value < MINLAT) {
Expand Down
43 changes: 43 additions & 0 deletions src/isValidLongitude.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import isValidLongitude from './isValidLongitude';
import { MAXLON, MINLON } from './constants';

describe('isValidLongitude', () => {
describe('when value is a decimal', () => {
describe('when value is between MINLON and MAXLON', () => {
it('returns true', () => {
let value = MAXLON - 1;
expect(isValidLongitude(value)).toEqual(true);
value = MINLON + 1;
expect(isValidLongitude(value)).toEqual(true);
});
});
describe('when value is not between MINLON and MAXLON', () => {
it('returns false', () => {
let value = MAXLON + 1;
expect(isValidLongitude(value)).toEqual(false);
value = MINLON - 1;
expect(isValidLongitude(value)).toEqual(false);
});
});
});
describe('when value is a sexagesimal', () => {
describe('when value is between MINLON and MAXLON', () => {
it('returns true', () => {
const value = '51° 31\' 10.11" N';
expect(isValidLongitude(value)).toEqual(true);
});
});
describe('when value is not between MINLON and MAXLON', () => {
it('returns false', () => {
const value = '221°26′31″W';
expect(isValidLongitude(value)).toEqual(false);
});
});
});
describe('when value is not a decimal or sexagesimal', () => {
it('returns false', () => {
const value = 'foo';
expect(isValidLongitude(value)).toEqual(false);
});
});
});
1 change: 0 additions & 1 deletion src/isValidLongitude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import isSexagesimal from './isSexagesimal';
import sexagesimalToDecimal from './sexagesimalToDecimal';
import { MAXLON, MINLON } from './constants';

// TODO: Add tests
const isValidLongitude = (value: any): boolean => {
if (isDecimal(value)) {
if (parseFloat(value) > MAXLON || value < MINLON) {
Expand Down
7 changes: 7 additions & 0 deletions src/toDeg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import toDeg from './toDeg';

describe('toDeg', () => {
it('converts a value to a degree', () => {
expect(toDeg(35.238965)).toEqual(2019.0439689092252);
});
});
7 changes: 7 additions & 0 deletions src/toRad.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import toRad from './toRad';

describe('toRad', () => {
it('converts a value to a radian', () => {
expect(toRad(2019.0439689092252)).toEqual(35.238965);
});
});

0 comments on commit 628c801

Please sign in to comment.