Skip to content

Commit

Permalink
Only call permission request handler when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron committed Aug 15, 2023
1 parent e97959f commit 27cec1c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/location-services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function createLocationServices({

return {
async requestPermission() {
if (handlePermissionRequest) {
if (permissionState === PROMPT && handlePermissionRequest) {
permissionState = await handlePermissionRequest();
}

Expand Down
48 changes: 48 additions & 0 deletions test/jest/geolocation/get-current-position.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,30 @@ describe("Geolocation.getCurrentPosition()", () => {
expect(successFn).not.toHaveBeenCalled();
});
});

describe("when there is a permission request handler", () => {
let handlePermissionRequest: jest.Mock<HandlePermissionRequest>;

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", () => {
Expand Down Expand Up @@ -345,6 +369,30 @@ describe("Geolocation.getCurrentPosition()", () => {
});
});
});

describe("when there is a permission request handler", () => {
let handlePermissionRequest: jest.Mock<HandlePermissionRequest>;

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", () => {
Expand Down

0 comments on commit 27cec1c

Please sign in to comment.