From 931cceea01dfa2007b5a19163e091ca60d1ff406 Mon Sep 17 00:00:00 2001 From: Erin Millard Date: Wed, 16 Aug 2023 13:32:56 +1000 Subject: [PATCH] Test permissions with timeouts --- .../geolocation/get-current-position.spec.ts | 99 ++++++++++++++++++- 1 file changed, 96 insertions(+), 3 deletions(-) diff --git a/test/jest/geolocation/get-current-position.spec.ts b/test/jest/geolocation/get-current-position.spec.ts index 5b3efe5..8c50e0d 100644 --- a/test/jest/geolocation/get-current-position.spec.ts +++ b/test/jest/geolocation/get-current-position.spec.ts @@ -1,4 +1,5 @@ import { jest } from "@jest/globals"; +import { sleep } from "../../../src/async.js"; import { DENIED, GRANTED, @@ -80,7 +81,7 @@ describe("Geolocation.getCurrentPosition()", () => { locationServices.setPosition(positionA); }); - describe("when the handler does not change the state", () => { + describe("when the handler resets the permission immediately", () => { beforeEach(() => { handlePermissionRequestA.mockImplementation( async (): Promise => PROMPT, @@ -148,7 +149,41 @@ describe("Geolocation.getCurrentPosition()", () => { }); }); - describe("when the handler denies the permission", () => { + describe("when the handler resets the permission after a delay", () => { + beforeEach(() => { + handlePermissionRequestA.mockImplementation( + async (): Promise => { + await sleep(20); + + return PROMPT; + }, + ); + }); + + describe("when reading the position with a timeout", () => { + beforeEach(async () => { + await getCurrentPosition(geolocation, successFn, errorFn, { + timeout: 10, + }); + }); + + it("calls the error callback with a GeolocationPositionError with a code of PERMISSION_DENIED and an empty message", () => { + expect(errorFn).toHaveBeenCalled(); + expect(errorFn.mock.calls[0][0]).toBeDefined(); + + const error = errorFn.mock + .calls[0][0] as GeolocationPositionError; + + expect(error).toBeInstanceOf(GeolocationPositionError); + expect(error.code).toBe( + GeolocationPositionError.PERMISSION_DENIED, + ); + expect(error.message).toBe(""); + }); + }); + }); + + describe("when the handler denies the permission immediately", () => { beforeEach(() => { handlePermissionRequestA.mockImplementation( async (): Promise => DENIED, @@ -180,7 +215,41 @@ describe("Geolocation.getCurrentPosition()", () => { }); }); - describe("when the handler grants the permission", () => { + describe("when the handler denies the permission after a delay", () => { + beforeEach(() => { + handlePermissionRequestA.mockImplementation( + async (): Promise => { + await sleep(20); + + return DENIED; + }, + ); + }); + + describe("when reading the position with a timeout", () => { + beforeEach(async () => { + await getCurrentPosition(geolocation, successFn, errorFn, { + timeout: 10, + }); + }); + + it("calls the error callback with a GeolocationPositionError with a code of PERMISSION_DENIED and an empty message", () => { + expect(errorFn).toHaveBeenCalled(); + expect(errorFn.mock.calls[0][0]).toBeDefined(); + + const error = errorFn.mock + .calls[0][0] as GeolocationPositionError; + + expect(error).toBeInstanceOf(GeolocationPositionError); + expect(error.code).toBe( + GeolocationPositionError.PERMISSION_DENIED, + ); + expect(error.message).toBe(""); + }); + }); + }); + + describe("when the handler grants the permission immediately", () => { beforeEach(() => { handlePermissionRequestA.mockImplementation( async (): Promise => GRANTED, @@ -202,6 +271,30 @@ describe("Geolocation.getCurrentPosition()", () => { }); }); + describe("when the handler grants the permission after a delay", () => { + beforeEach(() => { + handlePermissionRequestA.mockImplementation( + async (): Promise => { + await sleep(20); + + return GRANTED; + }, + ); + }); + + describe("when reading the position with a timeout", () => { + beforeEach(async () => { + await getCurrentPosition(geolocation, successFn, errorFn, { + timeout: 10, + }); + }); + + it("does not include the time spent waiting for permission in the timeout", () => { + expect(successFn).toHaveBeenCalledWith(positionA); + }); + }); + }); + describe("when the handler is removed", () => { beforeEach(() => { locationServices.removePermissionRequestHandler(