Skip to content

Commit

Permalink
Simplify error checking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jleh committed Jul 18, 2020
1 parent 8cb7aa6 commit 001942a
Showing 1 changed file with 11 additions and 31 deletions.
42 changes: 11 additions & 31 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,9 @@ describe('QTH locator', () => {
expect(BDPair.deg).toBeCloseTo(deg);
};

const expectInvalidGridErr = (fn, a, b) => {
expect.assertions(2);

try {
fn(a, b);
} catch (error) {
expect(error).toHaveProperty('message', 'Input is not valid locator string');
expect(error).toBeInstanceOf(Error);
}
};

const expectInvalidLatLngErr = (fn, a, b) => {
expect.assertions(4);
const expectInvalidGridErr = fn => expect(fn).toThrow('Input is not valid locator string');

try {
fn(a, b);
} catch (error) {
expect(error).toHaveProperty('message', 'Input is not a valid coordinate');
expect(error).toBeInstanceOf(Error);
}
};
const expectInvalidLatLngErr = fn => expect(fn).toThrow('Input is not a valid coordinate');

it('Converts locator string to coordinates', () => {
expectCoordinates(qthLocator.locatorToLatLng('KP20le'), 60.188, 24.958);
Expand All @@ -66,19 +48,19 @@ describe('QTH locator', () => {
});

it('Detect invalid grid', () => {
expectInvalidGridErr(qthLocator.locatorToLatLng, 'RZ73');
expectInvalidGridErr(() => qthLocator.locatorToLatLng('RZ73'));
});

it('Locate debatable grid - It is in spec!', () => {
expectCoordinates(qthLocator.locatorToLatLng('RR73'), 83.479, 174.96);
});

it('Detect short grid', () => {
expectInvalidGridErr(qthLocator.locatorToLatLng, 'R73');
expectInvalidGridErr(() => qthLocator.locatorToLatLng('R73'));
});

it('detect invalid grid in bearingDistance 1', () => {
expectInvalidGridErr(qthLocator.bearingDistance, 'FN20qr', 'F030ll');
expectInvalidGridErr(() => qthLocator.bearingDistance('FN20qr', 'F030ll'));
});

it('Converts latLng to grid', () => {
Expand All @@ -88,13 +70,11 @@ describe('QTH locator', () => {
expect(qthLocator.latLngToLocator(-22.904788, -43.184915)).toBe('GG87jc');
});

it('Throws error for invalid latitude', () => {
expectInvalidLatLngErr(qthLocator.latLngToLocator, 91, 120);
expectInvalidLatLngErr(qthLocator.latLngToLocator, -91, 120);
});

it('Throws error for invalid longitude', () => {
expectInvalidLatLngErr(qthLocator.latLngToLocator, 55, -181);
expectInvalidLatLngErr(qthLocator.latLngToLocator, 55, 181);
it('Throws error for invalid coordinates', () => {
expectInvalidLatLngErr(() => qthLocator.latLngToLocator(91, 120));
expectInvalidLatLngErr(() => qthLocator.latLngToLocator(-91, 120));
expectInvalidLatLngErr(() => qthLocator.latLngToLocator(55, 181));
expectInvalidLatLngErr(() => qthLocator.latLngToLocator(55, -181));
expectInvalidLatLngErr(() => qthLocator.latLngToLocator(-91, -181));
});
});

0 comments on commit 001942a

Please sign in to comment.