Skip to content

Commit

Permalink
okay we are CCOOOOOKING
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 4, 2024
1 parent 63f239d commit 2afd353
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
92 changes: 84 additions & 8 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
permissionMonitoringMachine,
} from './permissionMonitor.machine';

const forever = 2 ^ (28 - 1);
const countingMachineThatNeedsPermissionAt3 = setup({
types: {
context: {} as { count: number; permissionStatus: PermissionStatus },
Expand Down Expand Up @@ -396,6 +397,19 @@ describe('Permission Monitoring Machine', () => {
log('subscribe to status updates'),
],
on: {
requestPermission: {
actions: [
sendTo(
({ system }) => {
return system.get(ActorSystemIds.permissionCheckerAndRequester);
},
({ event }) => ({
type: 'triggerPermissionRequest',
permission: event.permission,
})
),
],
},
permissionStatusChanged: {
description:
'Whenever the Permission Monitoring machine reports that a permission status has changed, we receive this event and can process and share with our siblings.',
Expand Down Expand Up @@ -426,11 +440,11 @@ describe('Permission Monitoring Machine', () => {
console.log('its granted yaya');
return {
// dynamic
type: 'permission.bluetooth.granted',
type: 'permission.granted.bluetooth',
};
} else {
return {
type: 'permission.bluetooth.denied',
type: 'permission.denied.bluetooth',
};
}
},
Expand All @@ -448,6 +462,17 @@ describe('Permission Monitoring Machine', () => {
id: 'someFeatureMachineId',
type: 'parallel',
states: {
logging: {
on: {
'*': {
actions: [
({ event }) => {
console.log('logging::::' + JSON.stringify(event, null, 2));
},
],
},
},
},
foo: {
initial: 'start',
states: {
Expand All @@ -459,17 +484,43 @@ describe('Permission Monitoring Machine', () => {
on: {
'permission.granted.bluetooth': { target: 'bluetoothGranted' },
'permission.denied.bluetooth': { target: 'bluetoothDenied' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
},
},
},
bluetoothGranted: {
type: 'final',
},
bluetoothDenied: {
type: 'final',
on: {
'permission.granted.bluetooth': { target: 'bluetoothGranted' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
type: 'permissionWasRequested',
permission: Permissions.bluetooth,
}),
},
},
},
},
},
handlingPermissions: {
on: {
permissionWasRequested: {
actions: [
sendTo('permissionHandler', ({ event }) => {
return {
type: 'requestPermission',
permission: event.permission,
};
}),
],
},
},
invoke: {
id: 'permissionHandler',
src: 'permissionReportingMachine',
Expand Down Expand Up @@ -531,13 +582,9 @@ describe('Permission Monitoring Machine', () => {
expect(featureMachineActor?.getSnapshot().value).toStrictEqual({
foo: 'waitingForPermission',
handlingPermissions: {},
logging: {},
});

// featureMachineActor?.send({
// type: 'sendPermissionRequest',
// permission: Permissions.bluetooth,
// });

expect(permissionMonitorActor.getSnapshot().value).toStrictEqual({
applicationLifecycle: 'applicationIsInForeground',
permissions: {},
Expand All @@ -561,12 +608,41 @@ describe('Permission Monitoring Machine', () => {
await waitFor(permissionCheckerActor, (state) => {
return state.value === 'idle';
});
expect(
permissionMonitorActor.getSnapshot().context.permissionsStatuses[
Permissions.bluetooth
]
).toBe('denied');

expect(permissionCheckerActor?.getSnapshot().value).toBe('idle');
expect(featureMachineActor?.getSnapshot().value).toStrictEqual({
foo: 'bluetoothDenied',
handlingPermissions: {},
logging: {},
});

// await waitFor(featureMachineActor, (state) => {
// return state.value === 'bluetoothDenied';
// });

featureMachineActor?.send({
type: 'user.didTapBluetoothRequestPermission',
permission: Permissions.bluetooth,
});

expect(permissionCheckerActor?.getSnapshot().value).toBe(
'requestingPermission'
);

await waitFor(permissionCheckerActor, (state) => {
return state.value === 'idle';
});
expect(featureMachineActor?.getSnapshot().value).toStrictEqual({
foo: 'bluetoothGranted',
handlingPermissions: {},
logging: {},
});
// await new Promise((resolve) => setTimeout(resolve, forever));
});

describe('Edge Cases', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export const permissionMonitoringMachine = setup({
}),
broadcastPermissionsToListeners: enqueueActions(
({ context, event, enqueue }) => {
// TODO this should only send permission updates for the recently modified permissions
// and is currently sending updates to all permissions to everyone
Object.keys(context.permissionSubscribers).forEach((permission) => {
context.permissionSubscribers[permission].forEach(
(actorRef: AnyActorRef) => {
Expand Down Expand Up @@ -194,8 +196,10 @@ export const permissionMonitoringMachine = setup({
],
},
permissionRequestCompleted: {
actions: 'assignPermissionRequestResultToContext',
// TODO 'broadcastPermissionsToListeners',
actions: [
'assignPermissionRequestResultToContext',
'broadcastPermissionsToListeners',
],
},
},
invoke: {
Expand Down

0 comments on commit 2afd353

Please sign in to comment.