Skip to content

Commit

Permalink
Fix 114 (#119)
Browse files Browse the repository at this point in the history
* add failing test

* tests green

* fixup test name

* wrap in acts

* wrap in acts

* fix tests
  • Loading branch information
craigmulligan authored Jun 12, 2023
1 parent 9d59351 commit f87b4a4
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 13 deletions.
99 changes: 90 additions & 9 deletions App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Home from './src/screens/Home';
import * as Location from 'expo-location';
import {getOrCreateRealtimeRecord} from './src/lib/data';
import {startRide} from './src/lib/ride';
import {RideModel, RealtimeDataModel} from './src/database';

it('App renders correctly for signed user.', async () => {
// check the home pages renders
Expand All @@ -17,6 +18,7 @@ it('App renders correctly for signed user.', async () => {

it('App registers location service and logs to db with distance calculated with active ride', async () => {
let tree!: ReactTestRenderer;
let record!: RealtimeDataModel;
await renderer.act(() => {
tree = renderer.create(<App />);
});
Expand Down Expand Up @@ -44,7 +46,9 @@ it('App registers location service and logs to db with distance calculated with
Location._emitLocation(newLocation);
});

let record = await getOrCreateRealtimeRecord();
await renderer.act(async () => {
record = await getOrCreateRealtimeRecord();
});
expect(record.distance).toBe(0);
expect(record.latitude).toBe(newLocation.coords.latitude);
expect(record.longitude).toBe(newLocation.coords.longitude);
Expand Down Expand Up @@ -79,8 +83,80 @@ it('App registers location service and logs to db with distance calculated with
tree.unmount();
});

it('MovingTime correctly handles locations recorded before the ride start.', async () => {
let tree!: ReactTestRenderer;
let ride!: RideModel;
let record!: RealtimeDataModel;

const coords = {
accuracy: 110,
latitude: 41.4027,
longitude: 2.1743,
heading: 10,
altitude: 41.0,
altitudeAccuracy: 0,
};

await renderer.act(async () => {
tree = renderer.create(<App />);
});

tree.root.findByType(Home);

await renderer.act(async () => {
record = await getOrCreateRealtimeRecord();
});

const startTime = Date.now();

// Trigger a location update
// before starting ride
await renderer.act(async () => {
// @ts-ignore
// this is a mock method
await Location._emitLocation({
timestamp: startTime - 5000,
mocked: true,
coords: {
...coords,
speed: 1,
},
});
});

await renderer.act(async () => {
// ensure there is an active ride
ride = await startRide();
// @ts-ignore
// this is a mock method
});

// Trigger a location update
// during ride
await renderer.act(async () => {
// @ts-ignore
// this is a mock method
await Location._emitLocation({
timestamp: ride.startedAt.getTime() + 1000,
mocked: true,
coords: {
...coords,
speed: 1,
},
});
});

// now ensure record.movingTime is back to zero.
// and doesn't take the location recorded
// before ride start into account.
expect(record.ride!.id).toBeTruthy();
expect(record.movingTime).toBe(1000);
});

it('App registers location service and logs to db with movingTime calculated with active ride', async () => {
let tree!: ReactTestRenderer;
let record!: RealtimeDataModel;

const coords = {
accuracy: 110,
latitude: 41.4027,
Expand All @@ -96,15 +172,16 @@ it('App registers location service and logs to db with movingTime calculated wit

tree.root.findByType(Home);

// Trigger a location update.
await renderer.act(async () => {
// ensure there is an active ride
await startRide();
// @ts-ignore
// this is a mock method
});

const record = await getOrCreateRealtimeRecord();
await renderer.act(async () => {
record = await getOrCreateRealtimeRecord();
});
expect(record.movingTime).toBe(0);
expect(record.lastLocationAt).toBe(null);
const startTime = Date.now();
Expand Down Expand Up @@ -138,7 +215,6 @@ it('App registers location service and logs to db with movingTime calculated wit
});
});

// check that
expect(record.movingTime).toBe(1000);

// Trigger a location update.
Expand All @@ -155,7 +231,6 @@ it('App registers location service and logs to db with movingTime calculated wit
});
});

// check that
expect(record.movingTime).toBe(2000);

// Trigger a location update.
Expand All @@ -176,12 +251,13 @@ it('App registers location service and logs to db with movingTime calculated wit
// because speed is == 0
expect(record.movingTime).toBe(2000);

// Now check for record in realtime db.
tree.unmount();
});

it('App registers location service and logs to db with without movingTime calculated with inactive ride', async () => {
let tree!: ReactTestRenderer;
let record!: RealtimeDataModel;

const coords = {
accuracy: 110,
latitude: 41.4027,
Expand All @@ -197,7 +273,9 @@ it('App registers location service and logs to db with without movingTime calcul

tree.root.findByType(Home);

const record = await getOrCreateRealtimeRecord();
await renderer.act(async () => {
record = await getOrCreateRealtimeRecord();
});
expect(record.movingTime).toBe(0);
expect(record.lastLocationAt).toBe(null);

Expand Down Expand Up @@ -240,6 +318,8 @@ it('App registers location service and logs to db with without movingTime calcul

it('App registers location service and logs to db without distance calculated with inactive ride', async () => {
let tree!: ReactTestRenderer;
let record!: RealtimeDataModel;

await renderer.act(() => {
tree = renderer.create(<App />);
});
Expand All @@ -265,8 +345,9 @@ it('App registers location service and logs to db without distance calculated wi
Location._emitLocation(newLocation);
});

let record = await getOrCreateRealtimeRecord();

await renderer.act(async () => {
record = await getOrCreateRealtimeRecord();
});
expect(record.distance).toBe(0);
expect(record.latitude).toBe(newLocation.coords.latitude);
expect(record.longitude).toBe(newLocation.coords.longitude);
Expand Down
1 change: 1 addition & 0 deletions setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'react-native-gesture-handler/jestSetup';

jest.useFakeTimers();
jest.mock(
'@nozbe/watermelondb/adapters/sqlite/makeDispatcher/index.native.js',
() => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/MapFollowLocation/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jest.mock('@react-navigation/elements', () => ({
it('map renders correctly', async () => {
// not much to test on this.
let tree!: ReactTestRenderer;
await renderer.act(() => {
renderer.act(() => {
tree = renderer.create(<MapWidget />);
});
tree.root.findByType(MapWidget);
Expand Down
12 changes: 10 additions & 2 deletions src/lib/data/movingTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import {LocationObject} from 'expo-location';
import {RealtimeDataModel} from '../../database';
import constants from '../../constants';

export function accumulateMovingTime(
export async function accumulateMovingTime(
lastRealTimeRecord: RealtimeDataModel,
currentLocation: LocationObject,
): number {
): Promise<number> {
// If we don't have a speed we return the previous
// if we have stopped we return the previous value
if (
currentLocation.coords.speed === null ||
currentLocation.coords.speed < constants.movingSpeed ||
!lastRealTimeRecord.lastLocationAt
) {
// we need to ensure that the
return lastRealTimeRecord.movingTime;
}

const ride = await lastRealTimeRecord.ride?.fetch();

if (ride && lastRealTimeRecord.lastLocationAt < ride?.startedAt) {
// check if we have a ride but this is the first location
return currentLocation.timestamp - ride.startedAt.getTime();
}

// we are moving and have speed.
// we should increment the movingTime
const diff =
Expand Down
2 changes: 1 addition & 1 deletion src/lib/data/realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function onLocation(
: 0;

const movingTime = realtimeData.ride?.id
? accumulateMovingTime(realtimeData, location)
? await accumulateMovingTime(realtimeData, location)
: 0;

console.log('accumulated distance: ', distance);
Expand Down

0 comments on commit f87b4a4

Please sign in to comment.