Skip to content

Commit 3b5ed3a

Browse files
authored
Add withTestCtx for Storage.test.ts (#117)
1 parent 740538b commit 3b5ed3a

File tree

1 file changed

+72
-42
lines changed

1 file changed

+72
-42
lines changed

extension/src/services/__tests__/Storage.test.ts

Lines changed: 72 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,44 @@ import {
88
Storage,
99
} from "../../services/Storage.ts";
1010

11-
const StorageLive = Layer.empty.pipe(
12-
Layer.provideMerge(Storage.Default),
13-
Layer.provide(TestVsCode.Default),
14-
Layer.provideMerge(
15-
Layer.succeed(ExtensionContext, {
16-
globalState: new Memento(),
17-
workspaceState: new Memento(),
18-
}),
19-
),
20-
);
21-
22-
it.layer(StorageLive)("Storage", (it) => {
23-
const key = createStorageKey("key", Schema.Struct({ value: Schema.Int }));
11+
const withTestCtx = Effect.fnUntraced(function* (
12+
ctx: { globalState?: Memento; workspaceState?: Memento } = {},
13+
) {
14+
const vscode = yield* TestVsCode.make();
15+
const layer = Layer.empty.pipe(
16+
Layer.provideMerge(Storage.Default),
17+
Layer.provide(TestVsCode.Default),
18+
Layer.provideMerge(
19+
Layer.succeed(ExtensionContext, {
20+
globalState: ctx.globalState ?? new Memento(),
21+
workspaceState: ctx.workspaceState ?? new Memento(),
22+
}),
23+
),
24+
);
25+
return {
26+
key: createStorageKey("key", Schema.Struct({ value: Schema.Int })),
27+
layer,
28+
vscode,
29+
};
30+
});
2431

25-
it.effect(
26-
"should return Option.None when no entry",
27-
Effect.fnUntraced(function* () {
32+
it.effect(
33+
"should return Option.None when no entry",
34+
Effect.fnUntraced(function* () {
35+
const { key, layer } = yield* withTestCtx();
36+
yield* Effect.gen(function* () {
2837
const storage = yield* Storage;
2938
const value = yield* storage.workspace.get(key);
3039
assert(Option.isOption(value));
31-
}),
32-
);
40+
}).pipe(Effect.provide(layer));
41+
}),
42+
);
3343

34-
it.effect(
35-
"should fallback to default without updating storage",
36-
Effect.fnUntraced(function* () {
44+
it.effect(
45+
"should fallback to default without updating storage",
46+
Effect.fnUntraced(function* () {
47+
const { key, layer } = yield* withTestCtx();
48+
yield* Effect.gen(function* () {
3749
const storage = yield* Storage;
3850
const defaultValue = { value: 1 };
3951

@@ -47,12 +59,15 @@ it.layer(StorageLive)("Storage", (it) => {
4759
"workspaceState": {},
4860
}
4961
`);
50-
}),
51-
);
62+
}).pipe(Effect.provide(layer));
63+
}),
64+
);
5265

53-
it.effect(
54-
"should encode value into the underlying store",
55-
Effect.fnUntraced(function* () {
66+
it.effect(
67+
"should encode value into the underlying store",
68+
Effect.fnUntraced(function* () {
69+
const { key, layer } = yield* withTestCtx();
70+
yield* Effect.gen(function* () {
5671
const storage = yield* Storage;
5772
yield* storage.workspace.set(key, { value: 2 });
5873

@@ -67,12 +82,20 @@ it.layer(StorageLive)("Storage", (it) => {
6782
},
6883
}
6984
`);
70-
}),
71-
);
85+
}).pipe(Effect.provide(layer));
86+
}),
87+
);
7288

73-
it.effect(
74-
"should replace existing value in the underlying store",
75-
Effect.fnUntraced(function* () {
89+
it.effect(
90+
"should replace existing value in the underlying store",
91+
Effect.fnUntraced(function* () {
92+
// initial state
93+
const workspaceState = new Memento();
94+
workspaceState.update("key", { value: 2 });
95+
96+
const { key, layer } = yield* withTestCtx({ workspaceState });
97+
98+
yield* Effect.gen(function* () {
7699
const storage = yield* Storage;
77100
yield* storage.workspace.set(key, { value: 3 });
78101

@@ -87,17 +110,24 @@ it.layer(StorageLive)("Storage", (it) => {
87110
},
88111
}
89112
`);
90-
}),
91-
);
113+
}).pipe(Effect.provide(layer));
114+
}),
115+
);
116+
117+
it.effect(
118+
"should throw StorageDecodeError badly encoded value",
119+
Effect.fnUntraced(function* () {
120+
const workspaceState = new Memento();
121+
workspaceState.update("key", "blah");
92122

93-
it.effect.fails(
94-
"should throw StorageDecodeError badly encoded value",
95-
Effect.fnUntraced(function* () {
123+
const { key, layer } = yield* withTestCtx({ workspaceState });
124+
125+
yield* Effect.gen(function* () {
96126
const storage = yield* Storage;
97-
const context = yield* ExtensionContext;
127+
const result = yield* Effect.either(storage.workspace.get(key));
98128

99-
yield* Effect.promise(() => context.workspaceState.update("key", "blah"));
100-
yield* storage.workspace.get(key);
101-
}),
102-
);
103-
});
129+
assert(result._tag === "Left", "Expected to fail decoding");
130+
assert(result.left._tag === "StorageDecodeError");
131+
}).pipe(Effect.provide(layer));
132+
}),
133+
);

0 commit comments

Comments
 (0)