Skip to content

Commit a417503

Browse files
committed
More docs
1 parent a7c8dc3 commit a417503

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

docs/guide/testing.md

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { MockRegistry } from "@foundatiofx/fetchclient/mocks";
1313
## Basic Usage
1414

1515
```ts
16-
import { FetchClientProvider } from "@foundatiofx/fetchclient";
16+
import { FetchClient } from "@foundatiofx/fetchclient";
1717
import { MockRegistry } from "@foundatiofx/fetchclient/mocks";
1818

1919
// Create mock registry
@@ -25,13 +25,11 @@ mocks.onGet("/api/users").reply(200, [
2525
{ id: 2, name: "Bob" },
2626
]);
2727

28-
// Install on provider
29-
const provider = new FetchClientProvider();
30-
provider.setBaseUrl("https://api.example.com");
31-
mocks.install(provider);
28+
// Install on client
29+
const client = new FetchClient({ baseUrl: "https://api.example.com" });
30+
mocks.install(client);
3231

3332
// Make requests - they're mocked!
34-
const client = provider.getFetchClient();
3533
const response = await client.getJSON("/api/users");
3634
// response.data = [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]
3735

@@ -106,8 +104,7 @@ mocks.onDelete("/api/users/1").reply(204);
106104
Use `replyOnce` for mocks that should only match once:
107105

108106
```ts
109-
mocks.onPost("/api/users")
110-
.replyOnce(201, { id: 1 });
107+
mocks.onPost("/api/users").replyOnce(201, { id: 1 });
111108

112109
await client.postJSON("/api/users", {}); // Returns 201, { id: 1 }
113110
await client.postJSON("/api/users", {}); // Falls through to real fetch (or no match)
@@ -142,12 +139,12 @@ mocks.onGet("/api/data")
142139
Match based on request headers:
143140

144141
```ts
145-
mocks.onGet("/api/data")
146-
.withHeaders({ "Authorization": "Bearer token123" })
142+
mocks
143+
.onGet("/api/data")
144+
.withHeaders({ Authorization: "Bearer token123" })
147145
.reply(200, { authorized: true });
148146

149-
mocks.onGet("/api/data")
150-
.reply(401, { error: "Unauthorized" });
147+
mocks.onGet("/api/data").reply(401, { error: "Unauthorized" });
151148
```
152149

153150
## Request History
@@ -159,25 +156,25 @@ const mocks = new MockRegistry();
159156
mocks.onGet("/api/users").reply(200, []);
160157
mocks.onPost("/api/users").reply(201, {});
161158

162-
mocks.install(provider);
159+
mocks.install(client);
163160

164161
await client.getJSON("/api/users");
165162
await client.postJSON("/api/users", { name: "Alice" });
166163

167164
// Check history
168-
console.log(mocks.history.all.length); // 2
169-
console.log(mocks.history.get.length); // 1
165+
console.log(mocks.history.all.length); // 2
166+
console.log(mocks.history.get.length); // 1
170167
console.log(mocks.history.post.length); // 1
171168

172169
// Access request details
173170
const postRequest = mocks.history.post[0];
174-
console.log(postRequest.url); // "https://example.com/api/users"
171+
console.log(postRequest.url); // "https://example.com/api/users"
175172
console.log(postRequest.method); // "POST"
176173
```
177174

178175
## Standalone Fetch Replacement
179176

180-
Use `mocks.fetch` directly without installing on a provider:
177+
Use `mocks.fetch` directly without installing on a client:
181178

182179
```ts
183180
const mocks = new MockRegistry();
@@ -211,16 +208,13 @@ mocks.resetHistory();
211208

212209
```ts
213210
describe("User API", () => {
214-
let provider: FetchClientProvider;
215211
let client: FetchClient;
216212
let mocks: MockRegistry;
217213

218214
beforeEach(() => {
219-
provider = new FetchClientProvider();
220-
provider.setBaseUrl("https://api.example.com");
215+
client = new FetchClient({ baseUrl: "https://api.example.com" });
221216
mocks = new MockRegistry();
222-
mocks.install(provider);
223-
client = provider.getFetchClient();
217+
mocks.install(client);
224218
});
225219

226220
afterEach(() => {
@@ -253,11 +247,13 @@ function setupMocks() {
253247
}
254248

255249
describe("App", () => {
250+
let client: FetchClient;
256251
let mocks: MockRegistry;
257252

258253
beforeEach(() => {
254+
client = new FetchClient({ baseUrl: "https://api.example.com" });
259255
mocks = setupMocks();
260-
mocks.install(provider);
256+
mocks.install(client);
261257
});
262258

263259
afterEach(() => {
@@ -318,8 +314,9 @@ it("should handle 404", async () => {
318314
it("should handle network errors", async () => {
319315
mocks.onGet("/api/data").networkError("Connection refused");
320316

321-
await expect(client.getJSON("/api/data"))
322-
.rejects.toThrow("Connection refused");
317+
await expect(client.getJSON("/api/data")).rejects.toThrow(
318+
"Connection refused",
319+
);
323320
});
324321

325322
it("should handle timeouts", async () => {
@@ -334,14 +331,12 @@ it("should handle timeouts", async () => {
334331

335332
```ts
336333
it("should open circuit after failures", async () => {
337-
const provider = new FetchClientProvider();
338-
provider.useCircuitBreaker({ failureThreshold: 3 });
334+
const client = new FetchClient();
335+
client.useCircuitBreaker({ failureThreshold: 3 });
339336

340337
const mocks = new MockRegistry();
341338
mocks.onGet("/api/data").reply(500, { error: "Server error" });
342-
mocks.install(provider);
343-
344-
const client = provider.getFetchClient();
339+
mocks.install(client);
345340

346341
// Trigger failures
347342
for (let i = 0; i < 3; i++) {
@@ -361,17 +356,16 @@ it("should open circuit after failures", async () => {
361356

362357
```ts
363358
import { assertEquals } from "@std/assert";
364-
import { FetchClientProvider } from "@foundatiofx/fetchclient";
359+
import { FetchClient } from "@foundatiofx/fetchclient";
365360
import { MockRegistry } from "@foundatiofx/fetchclient/mocks";
366361

367362
Deno.test("fetches users", async () => {
368-
const provider = new FetchClientProvider();
363+
const client = new FetchClient();
369364
const mocks = new MockRegistry();
370365

371366
mocks.onGet("/api/users").reply(200, [{ id: 1 }]);
372-
mocks.install(provider);
367+
mocks.install(client);
373368

374-
const client = provider.getFetchClient();
375369
const response = await client.getJSON("/api/users");
376370

377371
assertEquals(response.data, [{ id: 1 }]);

0 commit comments

Comments
 (0)