@@ -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
7974mock .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
0 commit comments