Releases: ezzatron/fake-geolocation
Releases · ezzatron/fake-geolocation
v0.15.0
Changed
- [BREAKING] This release updates the version of
fake-permissions
used for permissions handling tov0.14.x
, which includes breaking changes. See the[email protected]
release notes for details and updated usage examples.
v0.14.0
Changed
- [BREAKING] This release updates the version of
fake-permissions
used for permissions handling tov0.13.x
, which includes breaking changes. See the[email protected]
release notes for details and updated usage examples.
v0.13.2
Fixed
- Fixed an issue that prevented
observer.waitForCoordinates()
from working with coordinates that have aNaN
value for theheading
property. - Fixed a race condition that could prevent geolocation positions from being dispatched to a position watch before an associated
observer.waitForCoordinates()
call resolved.
v0.13.1
Fixed
- Fixed a memory leak in the Geolocation API implementation.
v0.13.0
Changed
- [BREAKING] This release includes an update to the version of
fake-permissions
used for permissions handling, which includes many breaking changes. See thefake-permissions
releases for details. - [BREAKING] The
createGeolocation()
function no longer takes auser
option. Access requests are now handled by thepermissionStore
object.
v0.12.0
Removed
- [BREAKING] Removed the
waitForCoordinates()
function - use geolocation observers instead. - [BREAKING] Removed the
waitForPositionError()
function - use geolocation observers instead. - [BREAKING] Removed the
compareCoordinates()
function.
Changed
- [BREAKING] Location services will no longer delay acquisition of coordinates unless the
acquireDelay
option is explicitly set to an amount of milliseconds. This makes coordinate jumps more predictable when usingfake-geolocation
in tests.
Added
- Added geolocation observers.
- The
createAPIs()
andcreateWrappedAPIs()
functions now return a geolocation observer in theobserver
property. - Added the
createGeolocationObserver()
function, which creates a new geolocation observer. - The
createAPIs()
andcreateWrappedAPIs()
functions now accept anacquireDelay
option, which is passed along to the location services. - Added the
GeolocationObserver
type. - Added the
GeolocationPositionErrorCode
type, which is an enum of all the possible error codes that can be thrown by the Geolocation API. - Added
PERMISSION_DENIED
,POSITION_UNAVAILABLE
, andTIMEOUT
as exported constants. These are useful when waiting for specific errors withobserver.waitForPositionError()
.
Geolocation observers
This release adds geolocation observers, which can be used to wait for specific changes to the coordinates or errors produced by a Geolocation API. This can be useful for testing scenarios where you want to wait for a specific state to be reached before continuing.
Observers are created for you when you call createAPIs()
or createWrappedAPIs()
. You can wait for specific sets of coordinates by calling observer.waitForCoordinates()
, wait for specific geolocation errors by calling observer.waitForPositionError()
, or wait for specific geolocation
permission states by calling observer.waitForPermissionState()
.
import {
createAPIs,
createCoordinates,
PERMISSION_DENIED,
POSITION_UNAVAILABLE,
} from "fake-geolocation";
const { geolocation, observer, permissions, user } = createAPIs();
// We need some coords to start with
const coordsA = createCoordinates({ latitude: 1, longitude: 2 });
const coordsB = createCoordinates({ latitude: 3, longitude: 4 });
// Jump to some coords and grant permission
user.jumpToCoordinates(coordsA);
user.grantPermission({ name: "geolocation" });
// Start watching the position
let position: GeolocationPosition | undefined;
let error: GeolocationPositionError | undefined;
geolocation.watchPosition(
(p) => {
position = p;
},
(e) => {
error = e;
},
);
// Start a Permissions API query
const status = await permissions.query({ name: "geolocation" });
// Wait for the position to be at coordsA
await observer.waitForCoordinates(coordsA);
// Outputs "true"
console.log(position?.coords.latitude === coordsA.latitude);
// Wait for the position to be at coordsA OR coordsB
await observer.waitForCoordinates([coordsA, coordsB]);
// Outputs "true"
console.log(position?.coords.latitude === coordsA.latitude);
// Wait for the position to have a latitude of 1
await observer.waitForCoordinates({ latitude: 1 });
// Outputs "true"
console.log(position?.coords.latitude === 1);
// Wait for the position to be at coordsB, while running a task
await observer.waitForCoordinates(coordsB, async () => {
user.jumpToCoordinates(coordsB);
});
// Outputs "true"
console.log(position?.coords.latitude === coordsB.latitude);
// Wait for the position to be at coordsB, using high accuracy
await observer.waitForCoordinates(coordsB, undefined, {
enableHighAccuracy: true,
});
// Outputs "true"
console.log(position?.coords.latitude === coordsB.latitude);
user.disableLocationServices();
// Wait for a POSITION_UNAVAILABLE error
await observer.waitForPositionError(POSITION_UNAVAILABLE);
// Outputs "true"
console.log(error?.code === POSITION_UNAVAILABLE);
// Wait for a POSITION_UNAVAILABLE OR PERMISSION_DENIED error
await observer.waitForPositionError([POSITION_UNAVAILABLE, PERMISSION_DENIED]);
// Outputs "true"
console.log(error?.code === POSITION_UNAVAILABLE);
// Wait for a PERMISSION_DENIED error, while running a task
await observer.waitForPositionError(PERMISSION_DENIED, async () => {
user.denyPermission({ name: "geolocation" });
});
// Outputs "true"
console.log(error?.code === PERMISSION_DENIED);
// You can also wait for geolocation permission states
await observer.waitForPermissionState("granted", async () => {
user.grantPermission({ name: "geolocation" });
});
// Outputs "true"
console.log(status.state === "granted");
Fixed
- Errors that are thrown asynchronously now use
queueMicrotask()
instead ofsetTimeout()
.
v0.11.1
Fixed
- Updated version constraint for
fake-permissions
.
v0.11.0
Changed
- [BREAKING] This release includes an update to the version of
fake-permissions
used for permissions handling, which includes many breaking changes. See the[email protected]
release notes for details and updated usage examples. - [BREAKING] The
handlePermissionRequest
option forcreateAPIs()
,createWrappedAPIs()
, andcreateUser()
has been renamed tohandleAccessRequest
in line with the changes tofake-permissions
. - [BREAKING] The
createGeolocation()
function now has apermissionStore
option that takes aPermissionsStore
object instead of apermissions
option that takes aPermissions
object. - [BREAKING] The
createGeolocation()
function now has auser
option that takes aUser
object instead of arequestPermission
option that takes a callback. - [BREAKING] The
LocationServices
type is now a type, instead of an interface. - [BREAKING] The
MutableLocationServices
type is now a type, instead of an interface. - [BREAKING] The
User
type is now a type, instead of an interface.
v0.10.1
Fixed
- Fixed an issue where calling
watchPosition()
with amaximumAge
ofInfinity
would cause thesuccessCallback
to be called with the first cached position indefinitely, even when setting new coordinates via location services.
v0.10.0
Added
- Added the
GeolocationPositionParameters
type, which can be used for typing simple objects that have the same properties asGeolocationPosition
, but don't implement the full interface.