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

feat(@aws-amplify/geo): add geofence APIs #9743

Merged
merged 13 commits into from
Apr 4, 2022
174 changes: 170 additions & 4 deletions packages/geo/__tests__/Geo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ import {
awsConfig,
TestPlacePascalCase,
testPlaceCamelCase,
} from './data';
validGeometry,
validGeofences,
validGeofence1,
singleGeofenceCamelcaseResults,
batchGeofencesCamelcaseResults,
} from './testData';

import {
mockBatchPutGeofenceCommand,
mockGetGeofenceCommand,
mockListGeofencesCommand,
} from './testUtils';

LocationClient.prototype.send = jest.fn(async command => {
if (
Expand Down Expand Up @@ -134,7 +145,7 @@ describe('Geo', () => {
geo.configure({});

expect(() => geo.getAvailableMaps()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and run `amplify push` after"
"No map resources found in amplify config, run 'amplify add geo' to create one and run `amplify push` after"
);
});

Expand All @@ -159,7 +170,7 @@ describe('Geo', () => {
geo.configure({});

expect(() => geo.getDefaultMap()).toThrow(
"No map resources found in amplify config, run 'amplify add geo' to create them and run `amplify push` after"
"No map resources found in amplify config, run 'amplify add geo' to create one and run `amplify push` after"
);
});

Expand Down Expand Up @@ -309,7 +320,7 @@ describe('Geo', () => {
});

describe('searchByCoordinates', () => {
const testCoordinates: Coordinates = [12345, 67890];
const testCoordinates: Coordinates = [45, 90];

test('should search with just coordinate input', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
Expand Down Expand Up @@ -371,4 +382,159 @@ describe('Geo', () => {
);
});
});

describe('saveGeofences', () => {
test('saveGeofences with a single geofence', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementationOnce(mockBatchPutGeofenceCommand);

const geo = new GeoClass();
geo.configure(awsConfig);

// Check that results are what's expected
const results = await geo.saveGeofences(validGeofence1);
expect(results).toEqual(singleGeofenceCamelcaseResults);

// Expect that the API was called with the proper input
const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
const output = {
Entries: [
{
GeofenceId: validGeofence1.geofenceId,
Geometry: {
Polygon: validGeofence1.geometry.polygon,
},
},
],
CollectionName: 'geofenceCollectionExample',
};
expect(input).toEqual(output);
});

test('saveGeofences with multiple geofences', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementation(mockBatchPutGeofenceCommand);

const geo = new GeoClass();
geo.configure(awsConfig);

// Check that results are what's expected
const results = await geo.saveGeofences(validGeofences);
expect(results).toEqual(batchGeofencesCamelcaseResults);

// Expect that the API was called the right amount of times
const expectedNumberOfCalls = Math.floor(validGeofences.length / 10) + 1;
expect(LocationClient.prototype.send).toHaveBeenCalledTimes(
expectedNumberOfCalls
);
});

test('should fail if there is no provider', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

const geo = new GeoClass();
geo.configure(awsConfig);
geo.removePluggable('AmazonLocationService');

await expect(geo.saveGeofences(validGeofence1)).rejects.toThrow(
'No plugin found in Geo for the provider'
);
});
});

describe('getGeofence', () => {
test('getGeofence returns the right geofence', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementationOnce(mockGetGeofenceCommand);

const geo = new GeoClass();
geo.configure(awsConfig);

// Check that results are what's expected
const results = await geo.getGeofence('testGeofenceId');
const expected = {
geofenceId: 'testGeofenceId',
geometry: validGeometry,
createTime: '2020-04-01T21:00:00.000Z',
updateTime: '2020-04-01T21:00:00.000Z',
status: 'ACTIVE',
};
expect(results).toEqual(expected);

// Expect that the API was called with the proper input
const spyon = jest.spyOn(LocationClient.prototype, 'send');
const input = spyon.mock.calls[0][0].input;
const output = {
GeofenceId: 'testGeofenceId',
CollectionName: 'geofenceCollectionExample',
};
expect(input).toEqual(output);
});
});

describe('listGeofences', () => {
test('listGeofences gets the first 100 geofences when no arguments are given', async () => {
jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementationOnce(mockListGeofencesCommand);

const geo = new GeoClass();
geo.configure(awsConfig);

// Check that results are what's expected
const results = await geo.listGeofences();
expect(results.entries.length).toEqual(100);
});

test('listGeofences gets the second 100 geofences when nextToken is passed', async () => {
jest.spyOn(Credentials, 'get').mockImplementation(() => {
return Promise.resolve(credentials);
});

LocationClient.prototype.send = jest
.fn()
.mockImplementation(mockListGeofencesCommand);

const geo = new GeoClass();
geo.configure(awsConfig);

// Check that results are what's expected

const first100Geofences = await geo.listGeofences();

const second100Geofences = await geo.listGeofences({
nextToken: first100Geofences.nextToken,
});

expect(second100Geofences.entries.length).toEqual(100);
expect(second100Geofences.entries[0].geofenceId).toEqual(
'validGeofenceId100'
);
expect(second100Geofences.entries[99].geofenceId).toEqual(
'validGeofenceId199'
);
});
});
});
Loading