Skip to content

Commit 7b60d82

Browse files
committed
Add tests to example
1 parent d9c1325 commit 7b60d82

File tree

3 files changed

+259
-2
lines changed

3 files changed

+259
-2
lines changed

packages/examples/packages/manage-state/src/index.test.ts

+238
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,244 @@ describe('onRpcRequest', () => {
2020
});
2121
});
2222

23+
describe('setState', () => {
24+
it('sets the state to the params', async () => {
25+
const { request } = await installSnap();
26+
27+
expect(
28+
await request({
29+
method: 'setState',
30+
params: {
31+
value: {
32+
items: ['foo'],
33+
},
34+
},
35+
}),
36+
).toRespondWith(null);
37+
38+
expect(
39+
await request({
40+
method: 'getState',
41+
}),
42+
).toRespondWith({
43+
items: ['foo'],
44+
});
45+
});
46+
47+
it('sets the state at a specific key', async () => {
48+
const { request } = await installSnap();
49+
50+
expect(
51+
await request({
52+
method: 'setState',
53+
params: {
54+
value: 'foo',
55+
key: 'nested.key',
56+
},
57+
}),
58+
).toRespondWith(null);
59+
60+
expect(
61+
await request({
62+
method: 'getState',
63+
}),
64+
).toRespondWith({
65+
nested: {
66+
key: 'foo',
67+
},
68+
});
69+
});
70+
71+
it('sets the unencrypted state to the params', async () => {
72+
const { request } = await installSnap();
73+
74+
expect(
75+
await request({
76+
method: 'setState',
77+
params: {
78+
value: {
79+
items: ['foo'],
80+
},
81+
encrypted: false,
82+
},
83+
}),
84+
).toRespondWith(null);
85+
86+
expect(
87+
await request({
88+
method: 'getState',
89+
}),
90+
).toRespondWith(null);
91+
92+
expect(
93+
await request({
94+
method: 'getState',
95+
params: {
96+
encrypted: false,
97+
},
98+
}),
99+
).toRespondWith({
100+
items: ['foo'],
101+
});
102+
});
103+
104+
it('throws if the state is not an object and no key is specified', async () => {
105+
const { request } = await installSnap();
106+
107+
const response = await request({
108+
method: 'setState',
109+
params: {
110+
value: 'foo',
111+
},
112+
});
113+
114+
expect(response).toRespondWithError(
115+
expect.objectContaining({
116+
code: -32602,
117+
message:
118+
'Invalid params: Value must be an object if key is not provided.',
119+
}),
120+
);
121+
});
122+
});
123+
124+
describe('getState', () => {
125+
it('returns `null` if no state has been set', async () => {
126+
const { request } = await installSnap();
127+
128+
const response = await request({
129+
method: 'getState',
130+
});
131+
132+
expect(response).toRespondWith(null);
133+
});
134+
135+
it('returns the state', async () => {
136+
const { request } = await installSnap();
137+
138+
await request({
139+
method: 'setState',
140+
params: {
141+
value: {
142+
items: ['foo'],
143+
},
144+
},
145+
});
146+
147+
const response = await request({
148+
method: 'getState',
149+
});
150+
151+
expect(response).toRespondWith({
152+
items: ['foo'],
153+
});
154+
});
155+
156+
it('returns the state at a specific key', async () => {
157+
const { request } = await installSnap();
158+
159+
await request({
160+
method: 'setState',
161+
params: {
162+
value: {
163+
nested: {
164+
key: 'foo',
165+
},
166+
},
167+
},
168+
});
169+
170+
const response = await request({
171+
method: 'getState',
172+
params: {
173+
key: 'nested.key',
174+
},
175+
});
176+
177+
expect(response).toRespondWith('foo');
178+
});
179+
180+
it('returns the unencrypted state', async () => {
181+
const { request } = await installSnap();
182+
183+
await request({
184+
method: 'setState',
185+
params: {
186+
value: {
187+
items: ['foo'],
188+
},
189+
encrypted: false,
190+
},
191+
});
192+
193+
const response = await request({
194+
method: 'getState',
195+
params: {
196+
encrypted: false,
197+
},
198+
});
199+
200+
expect(response).toRespondWith({
201+
items: ['foo'],
202+
});
203+
});
204+
});
205+
206+
describe('clearState', () => {
207+
it('clears the state', async () => {
208+
const { request } = await installSnap();
209+
210+
await request({
211+
method: 'setState',
212+
params: {
213+
value: {
214+
items: ['foo'],
215+
},
216+
},
217+
});
218+
219+
await request({
220+
method: 'clearState',
221+
});
222+
223+
const response = await request({
224+
method: 'getState',
225+
});
226+
227+
expect(response).toRespondWith(null);
228+
});
229+
230+
it('clears the unencrypted state', async () => {
231+
const { request } = await installSnap();
232+
233+
await request({
234+
method: 'setState',
235+
params: {
236+
value: {
237+
items: ['foo'],
238+
},
239+
encrypted: false,
240+
},
241+
});
242+
243+
await request({
244+
method: 'clearState',
245+
params: {
246+
encrypted: false,
247+
},
248+
});
249+
250+
const response = await request({
251+
method: 'getState',
252+
params: {
253+
encrypted: false,
254+
},
255+
});
256+
257+
expect(response).toRespondWith(null);
258+
});
259+
});
260+
23261
describe('legacy_setState', () => {
24262
it('sets the state to the params', async () => {
25263
const { request } = await installSnap();

packages/snaps-simulation/src/methods/specifications.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function resolve(result: unknown) {
5959
* resolve with `undefined`.
6060
* @returns The function implementation.
6161
*/
62-
export function asyncResolve(result?: unknown) {
62+
export function asyncResolve<Type>(result?: Type) {
6363
return async () => result;
6464
}
6565

packages/snaps-simulation/src/simulation.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import { getSnapFile } from './files';
3838
import type { SnapHelpers } from './helpers';
3939
import { getHelpers } from './helpers';
4040
import { resolveWithSaga } from './interface';
41-
import { getEndowments } from './methods';
41+
import { asyncResolve, getEndowments } from './methods';
4242
import {
4343
getPermittedClearSnapStateMethodImplementation,
4444
getPermittedGetSnapStateMethodImplementation,
@@ -123,6 +123,22 @@ export type RestrictedMiddlewareHooks = {
123123
};
124124

125125
export type PermittedMiddlewareHooks = {
126+
/**
127+
* A hook that gets whether the requesting origin has a given permission.
128+
*
129+
* @param permissionName - The name of the permission to check.
130+
* @returns Whether the origin has the permission.
131+
*/
132+
hasPermission: (permissionName: string) => boolean;
133+
134+
/**
135+
* A hook that returns a promise that resolves once the extension is unlocked.
136+
*
137+
* @param shouldShowUnlockRequest - Whether to show the unlock request.
138+
* @returns A promise that resolves once the extension is unlocked.
139+
*/
140+
getUnlockPromise: (shouldShowUnlockRequest: boolean) => Promise<void>;
141+
126142
/**
127143
* A hook that returns the Snap's auxiliary file for the given path. This hook
128144
* is bound to the Snap ID.
@@ -372,6 +388,9 @@ export function getPermittedHooks(
372388
runSaga: RunSagaFunction,
373389
): PermittedMiddlewareHooks {
374390
return {
391+
hasPermission: () => true,
392+
getUnlockPromise: asyncResolve(),
393+
375394
getSnapFile: async (path: string, encoding: AuxiliaryFileEncoding) =>
376395
await getSnapFile(snapFiles.auxiliaryFiles, path, encoding),
377396

0 commit comments

Comments
 (0)