11import 'babel-polyfill' ;
22import expect from 'expect.js' ;
3- import FetchMock , { Mock } from '../' ;
3+ import FetchMock , { Mock } from '../src ' ;
44
55const fetch = new FetchMock ( require ( '../__mocks__' ) , {
6+ delay : 2000 , // delay 5s
67 fetch : require ( 'isomorphic-fetch' ) ,
78 exclude : [
89 'https://www.amazon.com' ,
@@ -28,7 +29,7 @@ describe('test fetch mock', () => {
2829 expect ( data ) . not . to . be . empty ( ) ;
2930 expect ( data ) . to . be . an ( 'array' ) ;
3031 expect ( data ) . to . have . length ( 2 ) ;
31- } ) ;
32+ } ) . timeout ( 20000 ) ;
3233
3334 it ( 'fetch /api/users?a=b' , async ( ) => {
3435 const response = await fetch ( '/api/users' ) ;
@@ -39,7 +40,7 @@ describe('test fetch mock', () => {
3940 expect ( data ) . not . to . be . empty ( ) ;
4041 expect ( data ) . to . be . an ( 'array' ) ;
4142 expect ( data ) . to . have . length ( 2 ) ;
42- } ) ;
43+ } ) . timeout ( 20000 ) ;
4344
4445 it ( 'fetch /api/users with url parameters' , async ( ) => {
4546 const response = await fetch ( '/api/users?name=John' ) ;
@@ -50,7 +51,7 @@ describe('test fetch mock', () => {
5051 expect ( data ) . not . to . be . empty ( ) ;
5152 expect ( data ) . to . be . an ( 'array' ) ;
5253 expect ( data ) . to . have . length ( 1 ) ;
53- } ) ;
54+ } ) . timeout ( 20000 ) ;
5455
5556 it ( 'fetch /api/users with post parameters' , async ( ) => {
5657 const response = await fetch ( '/api/users' , {
@@ -69,7 +70,7 @@ describe('test fetch mock', () => {
6970 expect ( data ) . not . to . be . empty ( ) ;
7071 expect ( data ) . to . be . an ( 'array' ) ;
7172 expect ( data ) . to . have . length ( 1 ) ;
72- } ) ;
73+ } ) . timeout ( 20000 ) ;
7374
7475 it ( 'fetch /api/users/{userId}' , async ( ) => {
7576 const response = await fetch ( '/api/users/123' ) ;
@@ -79,7 +80,7 @@ describe('test fetch mock', () => {
7980 expect ( data ) . not . to . be ( undefined ) ;
8081 expect ( data ) . not . to . be . empty ( ) ;
8182 expect ( data ) . to . be . property ( 'userId' , '123' ) ;
82- } ) ;
83+ } ) . timeout ( 20000 ) ;
8384
8485 it ( 'fetch /api/users/mockjs with mockjs' , async ( ) => {
8586 const response = await fetch ( '/api/users/mockjs' ) ;
@@ -90,7 +91,7 @@ describe('test fetch mock', () => {
9091 expect ( data ) . not . to . be . empty ( ) ;
9192 expect ( data ) . to . be . an ( 'array' ) ;
9293 expect ( data ) . to . have . length ( 2 ) ;
93- } ) ;
94+ } ) . timeout ( 20000 ) ;
9495
9596 it ( 'fetch /api/users/mockjs with mockjs' , async ( ) => {
9697 const response = await fetch ( '/api/users/mockjs' ) ;
@@ -101,7 +102,7 @@ describe('test fetch mock', () => {
101102 expect ( data ) . not . to . be . empty ( ) ;
102103 expect ( data ) . to . be . an ( 'array' ) ;
103104 expect ( data ) . to . have . length ( 2 ) ;
104- } ) ;
105+ } ) . timeout ( 20000 ) ;
105106
106107 it ( 'fetch /api/users/{userid} with prue data response' , async ( ) => {
107108 const response = await fetch ( '/api/users/pru/121' ) ;
@@ -112,7 +113,7 @@ describe('test fetch mock', () => {
112113 expect ( data ) . not . to . be . empty ( ) ;
113114 expect ( data ) . to . be . an ( 'object' ) ;
114115 expect ( data ) . to . be . property ( 'userId' , '121' ) ;
115- } ) ;
116+ } ) . timeout ( 20000 ) ;
116117
117118 it ( 'fetch exclude path' , async ( ) => {
118119 const response = await fetch ( 'https://www.amazon.com' ) ;
@@ -135,7 +136,7 @@ describe('test fetch mock', () => {
135136 } ) ,
136137 } ) ;
137138 expect ( status ) . to . be . eql ( 201 ) ;
138- } ) ;
139+ } ) . timeout ( 20000 ) ;
139140
140141 it ( 'put /api/users/123' , async ( ) => {
141142 const response = await fetch ( '/api/users/123' , {
@@ -154,7 +155,7 @@ describe('test fetch mock', () => {
154155 expect ( data ) . not . to . be . empty ( ) ;
155156 expect ( data ) . to . be . an ( 'object' ) ;
156157 expect ( data . userId ) . to . be . eql ( 123 ) ;
157- } ) ;
158+ } ) . timeout ( 20000 ) ;
158159
159160 it ( 'proxy other api server' , async ( ) => {
160161 const response = await fetch ( '/ip/?format=json' ) ;
@@ -165,5 +166,31 @@ describe('test fetch mock', () => {
165166 expect ( data ) . not . to . be . empty ( ) ;
166167 expect ( data ) . to . be . an ( 'object' ) ;
167168 expect ( data . ip ) . to . be . an ( 'string' ) ;
169+ } ) . timeout ( 20000 ) ;
170+
171+ describe ( 'test delay mock' , ( ) => {
172+
173+ it ( 'global delay' , async ( ) => {
174+ const start = new Date ( ) . getTime ( ) ;
175+ await fetch ( '/api/users' ) ;
176+ expect ( new Date ( ) . getTime ( ) - start ) . to . greaterThan ( 2000 ) ;
177+ } ) . timeout ( 20000 ) ;
178+
179+ it ( 'method delay 30ms' , async ( ) => {
180+ const start = new Date ( ) . getTime ( ) ;
181+ await fetch ( '/api/users' , {
182+ delay : 30 ,
183+ } ) ;
184+ expect ( new Date ( ) . getTime ( ) - start ) . to . greaterThan ( 30 ) ;
185+ } ) . timeout ( 20000 ) ;
186+
187+ it ( 'method delay 3000ms' , async ( ) => {
188+ const start = new Date ( ) . getTime ( ) ;
189+ await fetch ( '/api/users' , {
190+ delay : 3000 ,
191+ } ) ;
192+ expect ( new Date ( ) . getTime ( ) - start ) . to . greaterThan ( 3000 ) ;
193+ } ) . timeout ( 20000 ) ;
194+
168195 } ) ;
169196} ) ;
0 commit comments