Skip to content

Commit

Permalink
More progress; tests passing; building subscriber logic for permissio…
Browse files Browse the repository at this point in the history
…n monitor
  • Loading branch information
technoplato committed Mar 26, 2024
1 parent 5231ff2 commit 864ebc2
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
sendTo,
setup,
waitFor,
AnyActorRef,
} from 'xstate';

import { unimplementedPermissionMachineActions } from './permission.actions';
Expand Down Expand Up @@ -55,7 +56,7 @@ const countingMachineThatNeedsPermissionAt3 = setup({
},

{
target: ['disabled', '#handlingPermissions.active'],
target: ['disabled', '#permissionHandler.active'],
},
],
},
Expand All @@ -71,7 +72,7 @@ const countingMachineThatNeedsPermissionAt3 = setup({
"permissions. Right now we're doing everything inline" +
'but this will be extracted to something that is ' +
'straightforward for the end developer to use and test',
id: 'handlingPermissions',
id: 'permissionHandler',
/**
* what should go here....
*
Expand Down Expand Up @@ -288,12 +289,33 @@ describe('Permission Requester and Checker Machine', () => {
});
});

export type PermissionSubscribers = Array<AnyActorRef>;
export type PermissionSubscriberMap = Record<Permission, PermissionSubscribers>;

/**
* A map of that looks like this to start:
* {
* bluetooth: [],
* microphone: [],
* }
*/
export const EmptyPermissionSubscriberMap: PermissionSubscriberMap =
Object.values(Permissions).reduce(
(acc, permission) => ({
...acc,
}),
{} as PermissionSubscriberMap
);

describe('Permission Monitoring Machine', () => {
it('handle the happy path of being invoked, checking permission initially and then handle a permission request', async () => {
const permissionMonitoringMachine = setup({
types: {} as {
events: PermissionMonitoringMachineEvents;
context: { permissionsStatuses: PermissionStatusMapType };
context: {
permissionsStatuses: PermissionStatusMapType;
permissionSubscribers: PermissionSubscriberMap;
};
},
actors: {
applicationLifecycleReportingMachine:
Expand Down Expand Up @@ -340,6 +362,7 @@ describe('Permission Monitoring Machine', () => {

context: {
permissionsStatuses: InitialPermissionStatusMap,
permissionSubscribers: EmptyPermissionSubscriberMap,
},
states: {
applicationLifecycle: {
Expand Down Expand Up @@ -407,11 +430,9 @@ describe('Permission Monitoring Machine', () => {
},
}).start();

expect(actorRef.getSnapshot().context).toStrictEqual({
permissionsStatuses: {
[Permissions.bluetooth]: PermissionStatuses.unasked,
[permission]: PermissionStatuses.unasked,
},
expect(actorRef.getSnapshot().context.permissionsStatuses).toStrictEqual({
[Permissions.bluetooth]: PermissionStatuses.unasked,
[permission]: PermissionStatuses.unasked,
});

expect(actorRef.getSnapshot().value).toStrictEqual({
Expand All @@ -426,11 +447,9 @@ describe('Permission Monitoring Machine', () => {
);
});

expect(actorRef.getSnapshot().context).toStrictEqual({
permissionsStatuses: {
[Permissions.bluetooth]: PermissionStatuses.denied,
[permission]: PermissionStatuses.denied,
},
expect(actorRef.getSnapshot().context.permissionsStatuses).toStrictEqual({
[Permissions.bluetooth]: PermissionStatuses.denied,
[permission]: PermissionStatuses.denied,
});

actorRef.send({
Expand All @@ -448,11 +467,9 @@ describe('Permission Monitoring Machine', () => {
return state.children.someFooMachine?.getSnapshot().value === 'idle';
});

expect(actorRef.getSnapshot().context).toStrictEqual({
permissionsStatuses: {
[Permissions.bluetooth]: PermissionStatuses.denied,
[permission]: PermissionStatuses.granted,
},
expect(actorRef.getSnapshot().context.permissionsStatuses).toStrictEqual({
[Permissions.bluetooth]: PermissionStatuses.denied,
[permission]: PermissionStatuses.granted,
});
});

Expand Down

0 comments on commit 864ebc2

Please sign in to comment.