UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

156 lines 6.69 kB
'use strict';var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { if (typeof Reflect === "object" && typeof Reflect.decorate === "function") return Reflect.decorate(decorators, target, key, desc); switch (arguments.length) { case 2: return decorators.reduceRight(function(o, d) { return (d && d(o)) || o; }, target); case 3: return decorators.reduceRight(function(o, d) { return (d && d(target, key)), void 0; }, void 0); case 4: return decorators.reduceRight(function(o, d) { return (d && d(target, key, o)) || o; }, desc); } }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var angular2_1 = require('angular2/angular2'); var static_request_1 = require('../static_request'); var enums_1 = require('../enums'); var lang_1 = require('angular2/src/facade/lang'); var exceptions_1 = require('angular2/src/facade/exceptions'); var Rx = require('@reactivex/rxjs/dist/cjs/Rx'); var Subject = Rx.Subject, ReplaySubject = Rx.ReplaySubject; /** * * Mock Connection to represent a {@link Connection} for tests. * **/ var MockConnection = (function () { function MockConnection(req) { this.response = new ReplaySubject(1).take(1); this.readyState = enums_1.ReadyStates.Open; this.request = req; } /** * Sends a mock response to the connection. This response is the value that is emitted to the * {@link EventEmitter} returned by {@link Http}. * * ### Example * * ``` * var connection; * backend.connections.subscribe(c => connection = c); * http.request('data.json').subscribe(res => console.log(res.text())); * connection.mockRespond(new Response('fake response')); //logs 'fake response' * ``` * */ MockConnection.prototype.mockRespond = function (res) { if (this.readyState === enums_1.ReadyStates.Done || this.readyState === enums_1.ReadyStates.Cancelled) { throw new exceptions_1.BaseException('Connection has already been resolved'); } this.readyState = enums_1.ReadyStates.Done; this.response.next(res); this.response.complete(); }; /** * Not yet implemented! * * Sends the provided {@link Response} to the `downloadObserver` of the `Request` * associated with this connection. */ MockConnection.prototype.mockDownload = function (res) { // this.request.downloadObserver.onNext(res); // if (res.bytesLoaded === res.totalBytes) { // this.request.downloadObserver.onCompleted(); // } }; // TODO(jeffbcross): consider using Response type /** * Emits the provided error object as an error to the {@link Response} {@link EventEmitter} * returned * from {@link Http}. */ MockConnection.prototype.mockError = function (err) { // Matches XHR semantics this.readyState = enums_1.ReadyStates.Done; this.response.error(err); }; return MockConnection; })(); exports.MockConnection = MockConnection; /** * A mock backend for testing the {@link Http} service. * * This class can be injected in tests, and should be used to override providers * to other backends, such as {@link XHRBackend}. * * ### Example * * ``` * import {MockBackend, DefaultOptions, Http} from 'angular2/http'; * it('should get some data', inject([AsyncTestCompleter], (async) => { * var connection; * var injector = Injector.resolveAndCreate([ * MockBackend, * provide(Http, {useFactory: (backend, defaultOptions) => { * return new Http(backend, defaultOptions) * }, deps: [MockBackend, DefaultOptions]})]); * var http = injector.get(Http); * var backend = injector.get(MockBackend); * //Assign any newly-created connection to local variable * backend.connections.subscribe(c => connection = c); * http.request('data.json').subscribe((res) => { * expect(res.text()).toBe('awesome'); * async.done(); * }); * connection.mockRespond(new Response('awesome')); * })); * ``` * * This method only exists in the mock implementation, not in real Backends. **/ var MockBackend = (function () { function MockBackend() { var _this = this; this.connectionsArray = []; this.connections = new Subject(); this.connections.subscribe(function (connection) { return _this.connectionsArray.push(connection); }); this.pendingConnections = new Subject(); } /** * Checks all connections, and raises an exception if any connection has not received a response. * * This method only exists in the mock implementation, not in real Backends. */ MockBackend.prototype.verifyNoPendingRequests = function () { var pending = 0; this.pendingConnections.subscribe(function (c) { return pending++; }); if (pending > 0) throw new exceptions_1.BaseException(pending + " pending connections to be resolved"); }; /** * Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve * connections, if it's expected that there are connections that have not yet received a response. * * This method only exists in the mock implementation, not in real Backends. */ MockBackend.prototype.resolveAllConnections = function () { this.connections.subscribe(function (c) { return c.readyState = 4; }); }; /** * Creates a new {@link MockConnection}. This is equivalent to calling `new * MockConnection()`, except that it also will emit the new `Connection` to the `connections` * emitter of this `MockBackend` instance. This method will usually only be used by tests * against the framework itself, not by end-users. */ MockBackend.prototype.createConnection = function (req) { if (!lang_1.isPresent(req) || !(req instanceof static_request_1.Request)) { throw new exceptions_1.BaseException("createConnection requires an instance of Request, got " + req); } var connection = new MockConnection(req); this.connections.next(connection); return connection; }; MockBackend = __decorate([ angular2_1.Injectable(), __metadata('design:paramtypes', []) ], MockBackend); return MockBackend; })(); exports.MockBackend = MockBackend; //# sourceMappingURL=mock_backend.js.map