From 27cec1ccbdd19cea4b76dc31ccf92a8ae0d514c8 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Wed, 16 Aug 2023 09:48:52 +1000 Subject: [PATCH] Only call permission request handler when needed --- src/location-services.ts | 2 +- .../geolocation/get-current-position.spec.ts | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/location-services.ts b/src/location-services.ts index cdf7f65..05bd9ac 100644 --- a/src/location-services.ts +++ b/src/location-services.ts @@ -26,7 +26,7 @@ export function createLocationServices({ return { async requestPermission() { - if (handlePermissionRequest) { + if (permissionState === PROMPT && handlePermissionRequest) { permissionState = await handlePermissionRequest(); } diff --git a/test/jest/geolocation/get-current-position.spec.ts b/test/jest/geolocation/get-current-position.spec.ts index e04a62d..a47d2e7 100644 --- a/test/jest/geolocation/get-current-position.spec.ts +++ b/test/jest/geolocation/get-current-position.spec.ts @@ -128,6 +128,30 @@ describe("Geolocation.getCurrentPosition()", () => { expect(successFn).not.toHaveBeenCalled(); }); }); + + describe("when there is a permission request handler", () => { + let handlePermissionRequest: jest.Mock; + + beforeEach(async () => { + handlePermissionRequest = jest.fn(); + locationServices = createLocationServices({ + handlePermissionRequest, + }); + geolocation = createGeolocation({ locationServices }); + + locationServices.setPermissionState(DENIED); + }); + + describe("when reading the position", () => { + beforeEach(async () => { + await getCurrentPosition(geolocation, successFn, errorFn); + }); + + it("does not call the permission request handler", () => { + expect(handlePermissionRequest).not.toHaveBeenCalled(); + }); + }); + }); }); describe("when permission is granted", () => { @@ -345,6 +369,30 @@ describe("Geolocation.getCurrentPosition()", () => { }); }); }); + + describe("when there is a permission request handler", () => { + let handlePermissionRequest: jest.Mock; + + beforeEach(async () => { + handlePermissionRequest = jest.fn(); + locationServices = createLocationServices({ + handlePermissionRequest, + }); + geolocation = createGeolocation({ locationServices }); + + locationServices.setPermissionState(GRANTED); + }); + + describe("when reading the position", () => { + beforeEach(async () => { + await getCurrentPosition(geolocation, successFn, errorFn); + }); + + it("does not call the permission request handler", () => { + expect(handlePermissionRequest).not.toHaveBeenCalled(); + }); + }); + }); }); describe("when permission has not been requested", () => {