-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { StdGeolocation } from "./types/std.js"; | ||
|
||
let canConstruct = false; | ||
|
||
export function createDelegatedGeolocation(_: { | ||
delegates: StdGeolocation[]; | ||
}): { | ||
geolocation: StdGeolocation; | ||
selectDelegate: SelectDelegate; | ||
} { | ||
canConstruct = true; | ||
|
||
return { | ||
geolocation: new Geolocation(), | ||
|
||
selectDelegate() {}, | ||
}; | ||
} | ||
|
||
export type SelectDelegate = (delegate: StdGeolocation) => void; | ||
|
||
export class Geolocation { | ||
/** | ||
* @deprecated Use the `createDelegatedPermissions()` function instead. | ||
*/ | ||
constructor() { | ||
if (!canConstruct) throw new TypeError("Illegal constructor"); | ||
canConstruct = false; | ||
} | ||
|
||
getCurrentPosition(): void { | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
watchPosition(): number { | ||
throw new Error("Not implemented"); | ||
} | ||
|
||
clearWatch(): void { | ||
throw new Error("Not implemented"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { | ||
Permissions, | ||
createPermissionStore, | ||
createPermissions, | ||
} from "fake-permissions"; | ||
import { GEOLOCATION } from "fake-permissions/constants/permission-name"; | ||
import { PROMPT } from "fake-permissions/constants/permission-state"; | ||
import { | ||
MutableLocationServices, | ||
SelectDelegate, | ||
User, | ||
createDelegatedGeolocation, | ||
createGeolocation, | ||
createLocationServices, | ||
createUser, | ||
} from "../../src/index.js"; | ||
import { StdGeolocation } from "../../src/types/std.js"; | ||
|
||
describe("Delegated geolocation", () => { | ||
let locationServicesA: MutableLocationServices; | ||
let locationServicesB: MutableLocationServices; | ||
let permissionsA: Permissions<typeof GEOLOCATION>; | ||
let permissionsB: Permissions<typeof GEOLOCATION>; | ||
let userA: User<typeof GEOLOCATION>; | ||
let userB: User<typeof GEOLOCATION>; | ||
let delegateA: StdGeolocation; | ||
let delegateB: StdGeolocation; | ||
let geolocation: StdGeolocation; | ||
let selectDelegate: SelectDelegate; | ||
|
||
beforeEach(() => { | ||
locationServicesA = createLocationServices(); | ||
locationServicesB = createLocationServices(); | ||
|
||
const permissionStoreA = createPermissionStore({ | ||
initialStates: new Map([[{ name: GEOLOCATION }, PROMPT]]), | ||
}); | ||
const permissionStoreB = createPermissionStore({ | ||
initialStates: new Map([[{ name: GEOLOCATION }, PROMPT]]), | ||
}); | ||
|
||
permissionsA = createPermissions({ permissionStore: permissionStoreA }); | ||
permissionsB = createPermissions({ permissionStore: permissionStoreB }); | ||
|
||
userA = createUser({ | ||
locationServices: locationServicesA, | ||
permissionStore: permissionStoreA, | ||
}); | ||
userB = createUser({ | ||
locationServices: locationServicesB, | ||
permissionStore: permissionStoreB, | ||
}); | ||
|
||
delegateA = createGeolocation({ | ||
async requestPermission(descriptor) { | ||
return userA.requestPermission(descriptor); | ||
}, | ||
|
||
locationServices: locationServicesA, | ||
permissions: permissionsA, | ||
}); | ||
delegateB = createGeolocation({ | ||
async requestPermission(descriptor) { | ||
return userB.requestPermission(descriptor); | ||
}, | ||
|
||
locationServices: locationServicesB, | ||
permissions: permissionsB, | ||
}); | ||
|
||
({ geolocation, selectDelegate } = createDelegatedGeolocation({ | ||
delegates: [delegateA, delegateB], | ||
})); | ||
}); | ||
|
||
it("cannot be instantiated directly", async () => { | ||
const instantiateGeolocation = () => { | ||
new (geolocation.constructor as new (p: object) => unknown)({}); | ||
}; | ||
|
||
expect(instantiateGeolocation).toThrow(TypeError); | ||
expect(instantiateGeolocation).toThrow("Illegal constructor"); | ||
}); | ||
}); |