-
-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathcreateRestrictedMethodMessenger.test.ts
More file actions
119 lines (98 loc) · 3.43 KB
/
createRestrictedMethodMessenger.test.ts
File metadata and controls
119 lines (98 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import { Messenger } from '@metamask/messenger';
import { createRestrictedMethodMessenger } from './createRestrictedMethodMessenger';
type FooAction = {
type: 'Foo:ping';
handler: () => string;
};
type BarAction = {
type: 'Bar:double';
handler: (n: number) => number;
};
type RootActions = FooAction | BarAction;
const getRootMessenger = (): Messenger<'Root', RootActions> => {
const messenger = new Messenger<'Root', RootActions>({ namespace: 'Root' });
const fooMessenger = new Messenger<'Foo', FooAction, never, typeof messenger>(
{
namespace: 'Foo',
parent: messenger,
},
);
const barMessenger = new Messenger<'Bar', BarAction, never, typeof messenger>(
{
namespace: 'Bar',
parent: messenger,
},
);
fooMessenger.registerActionHandler('Foo:ping', () => 'pong');
barMessenger.registerActionHandler('Bar:double', (value) => value * 2);
return messenger;
};
describe('createRestrictedMethodMessenger', () => {
it('returns undefined when actionNames is omitted', () => {
const rootMessenger = getRootMessenger();
expect(
createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
}),
).toBeUndefined();
});
it('returns undefined when actionNames is empty', () => {
const rootMessenger = getRootMessenger();
expect(
createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
// @ts-expect-error An empty array is rejected by the type system, but
// the runtime handles it as a no-op.
actionNames: [],
}),
).toBeUndefined();
});
it('exposes the requested action on the returned messenger', () => {
const rootMessenger = getRootMessenger();
const messenger = createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
actionNames: ['Foo:ping'] as const,
});
expect(messenger.call('Foo:ping')).toBe('pong');
});
it('uses the provided namespace for the returned messenger', () => {
const rootMessenger = getRootMessenger();
const messenger = createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
actionNames: ['Foo:ping'] as const,
});
// Registering an action under a different namespace must fail with an
// error that names the messenger's configured namespace.
expect(() =>
// @ts-expect-error Deliberately registering outside the child's action
// surface to probe its namespace.
messenger.registerActionHandler('Other:noop', () => undefined),
).toThrow(/wallet_example/u);
});
it('rejects calls to actions that were not requested', () => {
const rootMessenger = getRootMessenger();
const messenger = createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
actionNames: ['Foo:ping'] as const,
});
expect(() =>
// @ts-expect-error Intentionally calling an undelegated action.
messenger.call('Bar:double', 2),
).toThrow(/Bar:double/u);
});
it('exposes every requested action when multiple are delegated', () => {
const rootMessenger = getRootMessenger();
const messenger = createRestrictedMethodMessenger({
rootMessenger,
namespace: 'wallet_example',
actionNames: ['Foo:ping', 'Bar:double'] as const,
});
expect(messenger.call('Foo:ping')).toBe('pong');
expect(messenger.call('Bar:double', 3)).toBe(6);
});
});