Skip to content

Commit

Permalink
docs(wiki): improve starter snippet (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
diego-aquino authored Aug 29, 2024
1 parent 75f4735 commit cf8d5d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type MySchema = HttpSchema<{
POST: {
request: { body: User };
response: {
201: { body: User }; // User create
201: { body: User }; // User created
400: { body: RequestError }; // Bad request
409: { body: RequestError }; // Conflict
};
Expand Down Expand Up @@ -128,7 +128,7 @@ test('should list users', async () => {

// 7. Declare your mocks
// https://bit.ly/zimic-interceptor-http#http-interceptormethodpath
const listHandler = myInterceptor
const myHandler = myInterceptor
.get('/users')
// 7.1. Use restrictions to make declarative assertions and narrow down your mocks
// https://bit.ly/zimic-interceptor-http#http-handlerwithrestriction
Expand All @@ -144,27 +144,25 @@ test('should list users', async () => {
.respond({ status: 200, body: users });

// 8. Run your application and make requests
const fetchedUsers = await myApplication.fetchUsers({
token,
filters: { username: 'diego' },
});
expect(fetchedUsers).toEqual(users);
// ...

// 9. Assert yours requests
// 9. Check the requests you expect
// https://bit.ly/zimic-interceptor-http#http-handlerrequests
const listRequests = listHandler.requests();
expect(listRequests).toHaveLength(1);
const requests = myHandler.requests();
expect(requests).toHaveLength(1);

// The following assertions are automatically checked by the declared
// restrictions and thus are not necessary. Requests not matching them will
// cause warnings and not be intercepted by default.
// The following expects are automatically checked by the restrictions
// we declared above. Requests not matching them will cause warnings and not
// be intercepted.

// If you are not using restrictions, asserting the requests manually is
// a good practice:
expect(listRequests[0].headers.get('authorization')).toBe(`Bearer ${token}`);
expect(requests[0].headers.get('authorization')).toBe(`Bearer ${token}`);

expect(listRequests[0].searchParams.size).toBe(1);
expect(listRequests[0].searchParams.get('username')).toBe('diego');
expect(requests[0].searchParams.size).toBe(1);
expect(requests[0].searchParams.get('username')).toBe('diego');

expect(requests[0].body).toBe(null);
});
```

Expand Down
30 changes: 14 additions & 16 deletions docs/wiki/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type MySchema = HttpSchema<{
POST: {
request: { body: User };
response: {
201: { body: User }; // User create
201: { body: User }; // User created
400: { body: RequestError }; // Bad request
409: { body: RequestError }; // Conflict
};
Expand Down Expand Up @@ -139,7 +139,7 @@ test('should list users', async () => {

// 7. Declare your mocks
// https://bit.ly/zimic-interceptor-http#http-interceptormethodpath
const listHandler = myInterceptor
const myHandler = myInterceptor
.get('/users')
// 7.1. Use restrictions to make declarative assertions and narrow down your mocks
// https://bit.ly/zimic-interceptor-http#http-handlerwithrestriction
Expand All @@ -155,27 +155,25 @@ test('should list users', async () => {
.respond({ status: 200, body: users });

// 8. Run your application and make requests
const fetchedUsers = await myApplication.fetchUsers({
token,
filters: { username: 'diego' },
});
expect(fetchedUsers).toEqual(users);
// ...

// 9. Assert yours requests
// 9. Check the requests you expect
// https://bit.ly/zimic-interceptor-http#http-handlerrequests
const listRequests = listHandler.requests();
expect(listRequests).toHaveLength(1);
const requests = myHandler.requests();
expect(requests).toHaveLength(1);

// The following assertions are automatically checked by the declared
// restrictions and thus are not necessary. Requests not matching them will
// cause warnings and not be intercepted by default.
// The following expects are automatically checked by the restrictions
// we declared above. Requests not matching them will cause warnings and not
// be intercepted.

// If you are not using restrictions, asserting the requests manually is
// a good practice:
expect(listRequests[0].headers.get('authorization')).toBe(`Bearer ${token}`);
expect(requests[0].headers.get('authorization')).toBe(`Bearer ${token}`);

expect(listRequests[0].searchParams.size).toBe(1);
expect(listRequests[0].searchParams.get('username')).toBe('diego');
expect(requests[0].searchParams.size).toBe(1);
expect(requests[0].searchParams.get('username')).toBe('diego');

expect(requests[0].body).toBe(null);
});
```

Expand Down

0 comments on commit cf8d5d5

Please sign in to comment.