Skip to content

Commit

Permalink
Happy path counting feature machine with permissions workign
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Apr 8, 2024
1 parent 2ac4eae commit fff98c7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
permissionStatus: PermissionStatuses.unasked,
},

entry: log('Counting machine started'),

states: {
counting: {
initial: 'enabled',
Expand All @@ -33,7 +31,13 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
always: [
{
target: 'disabled',
guard: ({ context }) => context.count >= 3,
guard: ({ context }) =>
context.count >= 3 &&
context.permissionStatus !== PermissionStatuses.granted,
},
{
target: 'finished',
guard: ({ context }) => context.count >= 5,
},
],
on: {
Expand All @@ -47,7 +51,13 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
disabled: {
id: 'countingDisabled',
on: {
'permission.granted.bluetooth': { target: 'enabled' },
'permission.granted.bluetooth': {
target: 'enabled',
actions: [
() => console.log('permission granted'),
assign({ permissionStatus: PermissionStatuses.granted }),
],
},
'permission.denied.bluetooth': { target: 'bluetoothDenied' },
'user.didTapBluetoothRequestPermission': {
actions: raise({
Expand All @@ -70,6 +80,9 @@ export const countingMachineThatNeedsPermissionAt3 = setup({
},
},
},
finished: {
type: 'final',
},
},
},

Expand Down
42 changes: 25 additions & 17 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Counting Machine That Needs Permission At 3', () => {
// }).inspect,
});
applicationActor.start();
applicationActor.getSnapshot();

const permissionMonitorActor = applicationActor.system.get(
ActorSystemIds.permissionMonitoring
Expand All @@ -53,12 +54,12 @@ describe('Counting Machine That Needs Permission At 3', () => {
expect(permissionMonitorActor).toBeDefined();
expect(countingPermissionReporter).toBeDefined();

console.log(countingPermissionReporter.getSnapshot().value);
await new Promise((resolve) => setTimeout(resolve, 1000));
console.log(countingPermissionReporter.getSnapshot().value);
await new Promise((resolve) => setTimeout(resolve, 0));

const state = permissionMonitorActor.getSnapshot();
console.log({ v: state.context.permissionSubscribers });
console.log({
v: state.context.permissionSubscribers[Permissions.bluetooth][0].id,
});
expect(
state.context.permissionSubscribers[Permissions.bluetooth]?.length
).toEqual(1);
Expand Down Expand Up @@ -96,19 +97,26 @@ describe('Counting Machine That Needs Permission At 3', () => {
permission: Permissions.bluetooth,
});

// await waitFor(permissionCheckerActor, (state) => state.value === 'idle');
//
// expect(countingActor.getSnapshot().context.permissionStatus).toBe(PermissionStatuses.granted);
//
// // Send 'count.inc' events to increment the count to 5
// countingActor.send({ type: 'count.inc' });
// countingActor.send({ type: 'count.inc' });
//
// expect(countingActor.getSnapshot().context.count).toBe(5);
// expect(countingActor.getSnapshot().value).toStrictEqual({
// counting: 'finished',
// handlingPermissions: 'idle',
// });
await waitFor(permissionCheckerActor, (state) => state.value === 'idle');
await waitFor(countingActor, (state) => state.value.counting === 'enabled');

expect(countingActor.getSnapshot().value).toStrictEqual({
counting: 'enabled',
handlingPermissions: {},
});

expect(countingActor.getSnapshot().context.permissionStatus).toBe(
PermissionStatuses.granted
);

// Send 'count.inc' events to increment the count to 5
countingActor.send({ type: 'count.inc' });
countingActor.send({ type: 'count.inc' });

expect(countingActor.getSnapshot().context.count).toBe(5);
expect(countingActor.getSnapshot().value.counting).toStrictEqual(
'finished'
);
// await new Promise((resolve) => setTimeout(resolve, vLongTime));
});
// vLongTime // prettyMuchForever
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ export const permissionReportingMachine = setup({
const actorRef: AnyActorRef = system.get(
ActorSystemIds.permissionMonitoring
);
console.log({ actorRef });
console.log('hi');
return actorRef;
},
({ self, context }) => ({
Expand Down

0 comments on commit fff98c7

Please sign in to comment.