Skip to content
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
24 changes: 24 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ interface TSMLReactConfig {

declare var tsml_react_config: Readonly<TSMLReactConfig> | undefined;

//google analytics globals
declare var gtag:
| ((
type: 'event',
action: string,
params: {
event_category: string;
event_label: string;
}
) => void)
| undefined;

declare var ga:
| ((
type: 'send',
params: {
hitType: 'event';
eventCategory: string;
eventAction: string;
eventLabel: string;
}
) => void)
| undefined;

//declaration merge for IE compat
interface Navigator {
msSaveBlob: (blob: Blob, name: string) => void;
Expand Down
28 changes: 28 additions & 0 deletions src/helpers/analytics.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { analyticsEvent } from '.';

window.gtag = jest.fn();
window.ga = jest.fn();

describe('analytics', () => {
it('calls gtag if defined', () => {
analyticsEvent({ action: 'foo', category: 'bar', label: 'baz' });

expect(gtag).toHaveBeenCalledWith('event', 'foo', {
event_category: 'bar',
event_label: 'baz',
});
});

it('calls ga if defined when gtag is undefined', () => {
window.gtag = undefined;

analyticsEvent({ action: 'foo', category: 'bar', label: 'baz' });

expect(ga).toHaveBeenCalledWith('send', {
eventAction: 'foo',
eventCategory: 'bar',
eventLabel: 'baz',
hitType: 'event',
});
});
});
1 change: 1 addition & 0 deletions src/helpers/distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export function distance(a, b) {
Math.sin(aRadLat) * Math.sin(bRadLat) +
Math.cos(aRadLat) * Math.cos(bRadLat) * Math.cos(radTheta);

// TODO: How to test the scenario for this line?
if (dist > 1) dist = 1;

dist = Math.acos(dist);
Expand Down
52 changes: 52 additions & 0 deletions src/helpers/distance.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { distance } from './distance';
import { settings } from './settings';

jest.mock('./settings', () => ({
settings: { distance_unit: 'mi' },
}));

describe('distance', () => {
//exact
it('returns 0 for exact location', () => {
const obj = { latitude: 1, longitude: 1 };
expect(distance(obj, obj)).toStrictEqual(0);
});

//miles
it.each`
a | b | expected
${{ latitude: 1, longitude: 1 }} | ${{ latitude: 2, longitude: 2 }} | ${97.69}
${{ latitude: 10, longitude: 10 }} | ${{ latitude: 20, longitude: 20 }} | ${959.82}
${{ latitude: 100, longitude: 100 }} | ${{ latitude: 200, longitude: 200 }} | ${7697.83}
`('miles: yields $expected with $a and $b', ({ a, b, expected }) => {
expect(distance(a, b)).toStrictEqual(expected);
});

//kilometers
it.each`
a | b | expected
${{ latitude: 1, longitude: 1 }} | ${{ latitude: 2, longitude: 2 }} | ${157.22}
${{ latitude: 10, longitude: 10 }} | ${{ latitude: 20, longitude: 20 }} | ${1544.68}
${{ latitude: 100, longitude: 100 }} | ${{ latitude: 200, longitude: 200 }} | ${12388.45}
`('kilometers: yields $expected with $a and $b', ({ a, b, expected }) => {
settings.distance_unit = 'km';
expect(distance(a, b)).toStrictEqual(expected);
});

//null checks
it.each`
a | b
${null} | ${null}
${{ latitude: 1 }} | ${null}
${{ longitude: 1 }} | ${null}
${{ latitude: 1, longitude: 1 }} | ${null}
${null} | ${{ latitude: 1 }}
${null} | ${{ longitude: 1 }}
${null} | ${{ latitude: 1, longitude: 1 }}
${{ latitude: 1 }} | ${{ latitude: 1 }}
${{ latitude: 1, longitude: 1 }} | ${{ latitude: 1 }}
${{ latitude: 1 }} | ${{ latitude: 1, longitude: 1 }}
`('yields null with $a and $b', ({ a, b }) => {
expect(distance(a, b)).toStrictEqual(null);
});
});