Skip to content

Commit f6c1b8c

Browse files
committed
fix: added documentation, and forcing a publication to update npmjs
1 parent ac83a3e commit f6c1b8c

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

README.md

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,60 @@ mock.get('/test/:id', ResponseUtils.delayed(1000, (args: HandlerArgument) => {
6868
}));
6969
```
7070

71-
#### Types
72-
Full documentation of types can be seen [here](https://www.utgaard.xyz/yet-another-fetch-mock/),
73-
or [here](https://github.com/nutgaard/yet-another-fetch-mock/blob/master/src/types.ts) if you prefer reading typescript code.
74-
75-
7671
### Teardown
7772

7873
```javascript
7974
mock.restore();
8075
```
8176

77+
### Usage in tests
78+
The library ships with a custom `Middleware` that allows for introspection of intercepted fetch-calls.
79+
80+
The `spy` exposes seven diffrent methods, six (`middleware` is used for setup) of which can be used to verify that a call has been intercepted.
81+
All methods accept an optional argument of the type `RouteMatcher` which can be created using the `MatcherUtils`.
82+
In cases where you don't send in an `RouteMatcher`, then a default "match-everything"-matcher is used.
83+
84+
#### Example
85+
```typescript
86+
import FetchMock, { MatcherUtils, SpyMiddleware } from 'yet-another-fetch-mock';
87+
88+
describe('test using yet-another-fetch-mock', () => {
89+
let mock: FetchMock;
90+
let spy: SpyMiddleware;
91+
92+
beforeEach(() => {
93+
spy = new SpyMiddleware();
94+
mock = FetchMock.configure({
95+
middleware: spy.middleware
96+
});
97+
expect(spy.size()).toBe(0);
98+
});
99+
100+
afterEach(() => {
101+
mock.restore();
102+
});
103+
104+
it('should capture calls', (done) => {
105+
mock.get('/test/:id', { data: 'test' });
106+
Promise.all([
107+
fetch('/test/121'),
108+
fetch('/test/122')
109+
])
110+
.then(() => {
111+
expect(spy.size()).toBe(2);
112+
expect(spy.lastCall()).not.toBeNull();
113+
expect(spy.lastUrl()).toBe('/test/122');
114+
expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(true);
115+
done();
116+
})
117+
});
118+
});
119+
```
120+
121+
122+
#### Types
123+
Full documentation of types can be seen [here](https://www.utgaard.xyz/yet-another-fetch-mock/),
124+
or [here](https://github.com/nutgaard/yet-another-fetch-mock/blob/master/src/types.ts) if you prefer reading typescript code.
82125

83126

84127
### Tips

test/spy-middleware.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,15 @@ describe('SpyMiddlware', () => {
9999
done();
100100
});
101101
});
102+
103+
it('should capture calls', done => {
104+
mock.get('/test/:id', { data: 'test' });
105+
Promise.all([fetch('/test/121'), fetch('/test/122')]).then(() => {
106+
expect(spy.size()).toBe(2);
107+
expect(spy.lastCall()).not.toBeNull();
108+
expect(spy.lastUrl()).toBe('/test/122');
109+
expect(spy.called(MatcherUtils.url('/test/:id'))).toBe(true);
110+
done();
111+
});
112+
});
102113
});

0 commit comments

Comments
 (0)