1+ import {
2+ AsyncTestCompleter ,
3+ afterEach ,
4+ beforeEach ,
5+ ddescribe ,
6+ describe ,
7+ expect ,
8+ iit ,
9+ inject ,
10+ it ,
11+ xit ,
12+ SpyObject
13+ } from 'angular2/test_lib' ;
14+ import { ObservableWrapper } from 'angular2/src/core/facade/async' ;
15+ import { BrowserXhr } from 'angular2/src/http/backends/browser_xhr' ;
16+ import { MockConnection , MockBackend } from 'angular2/src/http/backends/mock_backend' ;
17+ import { bind , Injector } from 'angular2/core' ;
18+ import { Request } from 'angular2/src/http/static_request' ;
19+ import { Response } from 'angular2/src/http/static_response' ;
20+ import { Headers } from 'angular2/src/http/headers' ;
21+ import { Map } from 'angular2/src/core/facade/collection' ;
22+ import { RequestOptions , BaseRequestOptions } from 'angular2/src/http/base_request_options' ;
23+ import { BaseResponseOptions , ResponseOptions } from 'angular2/src/http/base_response_options' ;
24+ import { ResponseTypes } from 'angular2/src/http/enums' ;
25+
26+ export function main ( ) {
27+ describe ( 'MockBackend' , ( ) => {
28+
29+ var backend ;
30+ var sampleRequest1 ;
31+ var sampleResponse1 ;
32+ var sampleRequest2 ;
33+ var sampleResponse2 ;
34+ var connection ;
35+
36+ beforeEach ( ( ) => {
37+ var injector = Injector . resolveAndCreate (
38+ [ bind ( ResponseOptions ) . toClass ( BaseResponseOptions ) , MockBackend ] ) ;
39+ backend = injector . get ( MockBackend ) ;
40+ var base = new BaseRequestOptions ( ) ;
41+ sampleRequest1 = new Request ( base . merge ( new RequestOptions ( { url : 'https://google.com' } ) ) ) ;
42+ sampleResponse1 = new Response ( new ResponseOptions ( { body : 'response1' } ) ) ;
43+ sampleRequest2 = new Request ( base . merge ( new RequestOptions ( { url : 'https://google.com' } ) ) ) ;
44+ sampleResponse2 = new Response ( new ResponseOptions ( { body : 'response2' } ) ) ;
45+ } ) ;
46+
47+ it ( 'should create a new MockBackend' , ( ) => { expect ( backend ) . toBeAnInstanceOf ( MockBackend ) } ) ;
48+
49+ it ( 'should create a new MockConnection' ,
50+ ( ) => { expect ( backend . createConnection ( sampleRequest1 ) ) . toBeAnInstanceOf ( MockConnection ) } ) ;
51+
52+ it ( 'should create a new connection and allow subscription' , ( ) => {
53+ let connection = backend . createConnection ( sampleRequest1 ) ;
54+ connection . response . subscribe ( ( ) => { } ) ;
55+ } ) ;
56+
57+ it ( 'should allow responding after subscription' , inject ( [ AsyncTestCompleter ] , async => {
58+ let connection = backend . createConnection ( sampleRequest1 ) ;
59+ connection . response . subscribe ( ( res ) => { async . done ( ) ; } ) ;
60+ connection . mockRespond ( sampleResponse1 ) ;
61+ } ) ) ;
62+
63+ it ( 'should allow subscribing after responding' , inject ( [ AsyncTestCompleter ] , async => {
64+ let connection = backend . createConnection ( sampleRequest1 ) ;
65+ connection . mockRespond ( sampleResponse1 ) ;
66+ connection . response . subscribe ( ( res ) => { async . done ( ) ; } ) ;
67+ } ) ) ;
68+
69+ it ( 'should allow responding after subscription with an error' ,
70+ inject ( [ AsyncTestCompleter ] , async => {
71+ let connection = backend . createConnection ( sampleRequest1 ) ;
72+ connection . response . subscribe ( null , ( ) => { async . done ( ) ; } ) ;
73+ connection . mockError ( new Error ( 'nope' ) ) ;
74+ } ) ) ;
75+
76+ it ( 'should not throw when there are no unresolved requests' ,
77+ inject ( [ AsyncTestCompleter ] , async => {
78+ let connection = backend . createConnection ( sampleRequest1 ) ;
79+ connection . response . subscribe ( ( ) => { async . done ( ) ; } ) ;
80+ connection . mockRespond ( sampleResponse1 ) ;
81+ backend . verifyNoPendingRequests ( ) ;
82+ } ) ) ;
83+
84+ xit ( 'should throw when there are unresolved requests' , inject ( [ AsyncTestCompleter ] , async => {
85+ let connection = backend . createConnection ( sampleRequest1 ) ;
86+ connection . response . subscribe ( ( ) => { async . done ( ) ; } ) ;
87+ backend . verifyNoPendingRequests ( ) ;
88+ } ) ) ;
89+
90+ it ( 'should work when requests are resolved out of order' ,
91+ inject ( [ AsyncTestCompleter ] , async => {
92+ let connection1 = backend . createConnection ( sampleRequest1 ) ;
93+ let connection2 = backend . createConnection ( sampleRequest1 ) ;
94+ connection1 . response . subscribe ( ( ) => { async . done ( ) ; } ) ;
95+ connection2 . response . subscribe ( ( ) => { } ) ;
96+ connection2 . mockRespond ( sampleResponse1 ) ;
97+ connection1 . mockRespond ( sampleResponse1 ) ;
98+ backend . verifyNoPendingRequests ( ) ;
99+ } ) ) ;
100+
101+ xit ( 'should allow double subscribing' , inject ( [ AsyncTestCompleter ] , async => {
102+ let responses = [ sampleResponse1 , sampleResponse2 ] ;
103+ backend . connections . subscribe ( c => c . mockRespond ( responses . shift ( ) ) ) ;
104+ let responseObservable = backend . createConnection ( sampleRequest1 ) . response ;
105+ responseObservable . subscribe ( res => expect ( res . text ( ) ) . toBe ( 'response1' ) ) ;
106+ responseObservable . subscribe ( res => expect ( res . text ( ) ) . toBe ( 'response2' ) , null ,
107+ async . done ) ;
108+ } ) ) ;
109+
110+ // TODO(robwormald): readyStates are leaving?
111+ it ( 'should allow resolution of requests manually' , ( ) => {
112+ let connection1 : MockConnection = backend . createConnection ( sampleRequest1 ) ;
113+ let connection2 : MockConnection = backend . createConnection ( sampleRequest1 ) ;
114+ connection1 . response . subscribe ( ( ) => { } ) ;
115+ connection2 . response . subscribe ( ( ) => { } ) ;
116+ backend . resolveAllConnections ( ) ;
117+ backend . verifyNoPendingRequests ( ) ;
118+ } ) ;
119+ } ) ;
120+ }
0 commit comments