Skip to content

Commit

Permalink
Verify parent child communication functioning
Browse files Browse the repository at this point in the history
  • Loading branch information
technoplato committed Mar 22, 2024
1 parent dc1d725 commit 590aa1d
Showing 1 changed file with 17 additions and 113 deletions.
130 changes: 17 additions & 113 deletions libs/permissions/permissionLogic/src/lib/permission-logic.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import {
createActor,
enqueueActions,
fromPromise,
log,
sendTo,
setup,
waitFor,
Expand Down Expand Up @@ -115,102 +114,17 @@ describe('bluetooth permission machine', () => {
});

it('should report permission to parent after a check', async () => {
const spy = jest.fn();

const fooMachine = setup({
types: {
context: {} as {
parent?: ActorRef<Snapshot<unknown>, ParentEvent>;
statuses: PermissionStatusMapType;
},
events: {} as PermissionMachineEvents,
input: {} as {
parent?: ActorRef<Snapshot<unknown>, ParentEvent>;
},
},

actions: {
checkedSendParent: enqueueActions(
({ context, enqueue }, event: ParentEvent) => {
if (!context.parent) {
console.log(
'WARN: an attempt to send an event to a non-existent parent'
);
return;
}

console.log('sending event to parent', event);
console.log(JSON.stringify(event.type, null, 2));

enqueue.sendTo(context.parent, event);
}
),
},

actors: {
checkAllPermissions: fromPromise(async () => {
const result =
await unimplementedPermissionMachineActions.checkAllPermissions();

// return InitialPermissionStatusMap;
return result;
}),
},
}).createMachine({
id: 'bluetoothPermissionActor',
context: ({ input }) => ({
parent: input.parent,
statuses: InitialPermissionStatusMap,
}),

initial: 'idle',

states: {
idle: {
on: {
triggerPermissionCheck: { target: 'checkingPermission' },
},
},

checkingPermission: {
on: {
triggerPermissionCheck: {
actions: [
log('triggerPermissionCheck within checkingPermission'),
],
},
},
invoke: {
src: 'checkAllPermissions',
onDone: {
target: 'idle',
actions: [
assign({
statuses: ({ event }) => event.output,
}),

{
type: 'checkedSendParent',
params({ event }) {
console.log(JSON.stringify(event, null, 2));

return {
type: 'allPermissionsChecked',
statuses: event.output,
};
},
},
],
},
},
},
},
});
let result: any;
const spy = (
something: /* TODO: change type to whatever an event is in xstate*/ any
) => {
result = something;
};

const parentMachine = setup({
types: {} as { events: ParentEvent },
actors: {
fooMachine,
permissionCheckerAndRequesterMachine,
},
}).createMachine({
on: {
Expand All @@ -227,25 +141,28 @@ describe('bluetooth permission machine', () => {
},
invoke: {
id: 'someFooMachine',
src: 'fooMachine',
src: 'permissionCheckerAndRequesterMachine',
input: ({ self }) => ({ parent: self }),
},
entry: [
sendTo('someFooMachine', {
type: 'triggerPermissionCheck',
}),
],
});

const actorRef = createActor(parentMachine).start();
actorRef.send({ type: 'triggerPermissionCheck' });

await waitFor(
actorRef,
(state) => state.children.someFooMachine?.getSnapshot().value === 'idle',
{ timeout: 0 }
);

expect(spy).toHaveBeenCalledTimes(1);
expect(result).not.toBeNull();
expect(result.event).toStrictEqual({
type: 'allPermissionsChecked',
statuses: {
[Permissions.bluetooth]: PermissionStatuses.denied,
[Permissions.microphone]: PermissionStatuses.denied,
},
});
});
});

Expand Down Expand Up @@ -291,7 +208,6 @@ const permissionCheckerAndRequesterMachine = setup({
const result =
await unimplementedPermissionMachineActions.checkAllPermissions();

// return InitialPermissionStatusMap;
return result;
}),
},
Expand All @@ -312,23 +228,11 @@ const permissionCheckerAndRequesterMachine = setup({
},

checkingPermission: {
on: {
triggerPermissionCheck: {
actions: [log('triggerPermissionCheck within checkingPermission')],
},
},
invoke: {
src: 'checkAllPermissions',
onDone: {
target: 'idle',
actions: [
// {
// type: 'checkedSendParent',
// params({ context, event }) {
// return { type: 'FOO' };
// },
// },

assign({
statuses: ({ event }) => event.output,
}),
Expand Down

0 comments on commit 590aa1d

Please sign in to comment.