Skip to content

Commit 72f2bf1

Browse files
updated testing config
1 parent 9cf49ef commit 72f2bf1

File tree

5 files changed

+20
-32
lines changed

5 files changed

+20
-32
lines changed

package.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
{
22
"name": "effect-getting-started-course",
33
"version": "1.0.0",
4-
"description": "",
54
"main": "src/index.js",
5+
"author": "Sandro Maglione",
6+
"license": "MIT",
67
"scripts": {
78
"dev": "BASE_URL=https://pokeapi.co tsx src/index.ts",
8-
"logging": "tsx src/+1/logging.ts",
9-
"tsc": "tsc",
9+
"typecheck": "tsc",
1010
"test": "vitest"
1111
},
12-
"keywords": [],
13-
"author": "",
14-
"license": "ISC",
1512
"devDependencies": {
1613
"@types/node": "^20.14.10",
1714
"msw": "^2.3.1",

src/PokeApi.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,4 @@ export class PokeApi extends Context.Tag("PokeApi")<
3939
static readonly Live = Layer.effect(this, make).pipe(
4040
Layer.provide(Layer.mergeAll(PokemonCollection.Live, BuildPokeApiUrl.Live))
4141
);
42-
43-
static readonly Test = Layer.succeed(this, {
44-
getPokemon: Effect.gen(function* () {
45-
const response = yield* Effect.tryPromise({
46-
try: () => fetch(`http://localhost:3000/api/v2/pokemon/garchomp/`),
47-
catch: () => new FetchError(),
48-
});
49-
50-
if (!response.ok) {
51-
return yield* new FetchError();
52-
}
53-
54-
const json = yield* Effect.tryPromise({
55-
try: () => response.json(),
56-
catch: () => new JsonError(),
57-
});
58-
59-
return yield* Schema.decodeUnknown(Pokemon)(json);
60-
}),
61-
});
6242
}

src/index.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1-
import { Effect } from "effect";
1+
import { ConfigProvider, Effect, Layer } from "effect";
22
import { afterAll, afterEach, beforeAll, expect, it } from "vitest";
3-
import { program } from ".";
43
import { server } from "../test/node";
54
import { PokeApi } from "./PokeApi";
65

76
beforeAll(() => server.listen());
87
afterEach(() => server.resetHandlers());
98
afterAll(() => server.close());
109

11-
const mainTest = program.pipe(Effect.provide(PokeApi.Test));
10+
const TestConfigProvider = ConfigProvider.fromMap(
11+
new Map([["BASE_URL", "http://localhost:3000"]])
12+
);
13+
14+
const ConfigProviderLayer = Layer.setConfigProvider(TestConfigProvider);
15+
const MainLayer = PokeApi.Live.pipe(Layer.provide(ConfigProviderLayer));
16+
17+
const program = Effect.gen(function* () {
18+
const pokeApi = yield* PokeApi;
19+
return yield* pokeApi.getPokemon;
20+
});
21+
22+
const main = program.pipe(Effect.provide(MainLayer));
1223

1324
it("returns a valid pokemon", async () => {
14-
const response = await Effect.runPromise(mainTest);
25+
const response = await Effect.runPromise(main);
1526
expect(response).toEqual({
1627
id: 1,
1728
height: 10,

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const MainLayer = Layer.mergeAll(PokeApi.Live);
55

66
const PokemonRuntime = ManagedRuntime.make(MainLayer);
77

8-
export const program = Effect.gen(function* () {
8+
const program = Effect.gen(function* () {
99
const pokeApi = yield* PokeApi;
1010
return yield* pokeApi.getPokemon;
1111
});

test/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const mockPokemon: Pokemon = {
1010
};
1111

1212
export const handlers = [
13-
http.get("http://localhost:3000/api/v2/pokemon/garchomp/", () => {
13+
http.get("http://localhost:3000/api/v2/pokemon/*", () => {
1414
return HttpResponse.json(mockPokemon);
1515
}),
1616
];

0 commit comments

Comments
 (0)