11const RESPONSES = {
2- 'GET:https://socket.tutorcruncher.com/good/subjects' : {
2+ 'GET:https://socket.tutorcruncher.com/good/subjects' : ( ) => ( {
33 status : 200 ,
44 content : JSON . stringify ( [
55 { 'id' : 29 , 'name' : 'English Language' , 'category' : 'English' , 'link' : '29-english-language' } ,
66 { 'id' : 31 , 'name' : 'English Literature' , 'category' : 'English' , 'link' : '31-english-literature' } ,
77 { 'id' : 61 , 'name' : 'Chinese' , 'category' : 'Languages' , 'link' : '61-chinese' }
88 ] )
9- } ,
10- 'GET:https://socket.tutorcruncher.com/good/contractors' : {
9+ } ) ,
10+ 'GET:https://socket.tutorcruncher.com/good/contractors' : ( ) => ( {
1111 status : 200 ,
1212 content : JSON . stringify (
1313 [
@@ -36,7 +36,7 @@ const RESPONSES = {
3636 'distance' : null
3737 }
3838 ] )
39- }
39+ } )
4040}
4141
4242export function MockXMLHttpRequest ( ) {
@@ -57,9 +57,17 @@ export function MockXMLHttpRequest () {
5757 this . status = null
5858 this . statusText = null
5959 this . send = function ( data ) {
60- // console.log(`XHR ${_method}: ${_url}`)
61- const response = RESPONSES [ `${ _method } :${ _url } ` ]
62- if ( response ) {
60+ let args = null
61+ if ( _url . includes ( '?' ) ) {
62+ args = _url . substr ( _url . indexOf ( '?' ) + 1 , _url . length )
63+ _url = _url . substr ( 0 , _url . indexOf ( '?' ) )
64+ }
65+ // console.log(`XHR ${_method}: ${_url} args=${args}`)
66+ const f = RESPONSES [ `${ _method } :${ _url } ` ]
67+ const req = { method : _method , url : _url , args}
68+ global . xhr_calls . push ( req )
69+ if ( f ) {
70+ const response = f ( req )
6371 this . status = response . status
6472 this . responseText = response . content
6573 this . onload && this . onload ( )
@@ -70,5 +78,10 @@ export function MockXMLHttpRequest () {
7078}
7179
7280export function tick ( ) {
73- return new Promise ( resolve => setTimeout ( resolve , 0.01 ) )
81+ return new Promise ( resolve => setTimeout ( resolve , 0 ) )
82+ }
83+
84+ export function xhr_setup ( ) {
85+ global . xhr_calls = [ ]
86+ global . XMLHttpRequest = MockXMLHttpRequest
7487}
0 commit comments