|
| 1 | +// Based on https://github.com/angular/angular/blob/master/modules/angular2/test/testing/testing_public_spec.ts |
| 2 | +/* tslint:disable:no-unused-variable */ |
| 3 | +/** |
| 4 | + * Tests that show what goes wrong when the tests are incorrectly written or have a problem |
| 5 | + */ |
| 6 | +import { |
| 7 | + BadTemplateUrl, ButtonComp, |
| 8 | + ChildChildComp, ChildComp, ChildWithChildComp, |
| 9 | + ExternalTemplateComp, |
| 10 | + FancyService, MockFancyService, |
| 11 | + InputComp, |
| 12 | + MyIfComp, MyIfChildComp, MyIfParentComp, |
| 13 | + MockChildComp, MockChildChildComp, |
| 14 | + ParentComp, |
| 15 | + TestProvidersComp, TestViewProvidersComp |
| 16 | +} from './bag'; |
| 17 | + |
| 18 | +import { DebugElement } from 'angular2/core'; |
| 19 | +import { By } from 'angular2/platform/browser'; |
| 20 | + |
| 21 | +import { |
| 22 | + beforeEach, beforeEachProviders, withProviders, |
| 23 | + describe, ddescribe, xdescribe, |
| 24 | + expect, it, iit, xit, |
| 25 | + async, inject, fakeAsync, tick, |
| 26 | + ComponentFixture, TestComponentBuilder |
| 27 | +} from 'angular2/testing'; |
| 28 | + |
| 29 | +import { provide } from 'angular2/core'; |
| 30 | +import { ViewMetadata } from 'angular2/core'; |
| 31 | +import { PromiseWrapper } from 'angular2/src/facade/promise'; |
| 32 | +import { XHR } from 'angular2/src/compiler/xhr'; |
| 33 | +import { XHRImpl } from 'angular2/src/platform/browser/xhr_impl'; |
| 34 | +import { Observable } from 'rxjs/Rx'; |
| 35 | + |
| 36 | +//////// SPECS ///////////// |
| 37 | + |
| 38 | +xdescribe('async & inject testing errors', () => { |
| 39 | + let originalJasmineIt: any; |
| 40 | + let originalJasmineBeforeEach: any; |
| 41 | + |
| 42 | + let patchJasmineIt = () => { |
| 43 | + let deferred = PromiseWrapper.completer(); |
| 44 | + originalJasmineIt = jasmine.getEnv().it; |
| 45 | + jasmine.getEnv().it = (description: string, fn: Function): jasmine.Spec => { |
| 46 | + let done = () => { deferred.resolve(); }; |
| 47 | + (<any>done).fail = (err: any) => { deferred.reject(err); }; |
| 48 | + fn(done); |
| 49 | + return null; |
| 50 | + }; |
| 51 | + return deferred.promise; |
| 52 | + }; |
| 53 | + |
| 54 | + let restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; }; |
| 55 | + |
| 56 | + let patchJasmineBeforeEach = () => { |
| 57 | + let deferred = PromiseWrapper.completer(); |
| 58 | + originalJasmineBeforeEach = jasmine.getEnv().beforeEach; |
| 59 | + jasmine.getEnv().beforeEach = (fn: any): void => { |
| 60 | + let done = () => { deferred.resolve(); }; |
| 61 | + (<any>done).fail = (err: any) => { deferred.reject(err); }; |
| 62 | + fn(done); |
| 63 | + return null; |
| 64 | + }; |
| 65 | + return deferred.promise; |
| 66 | + }; |
| 67 | + |
| 68 | + let restoreJasmineBeforeEach = |
| 69 | + () => { jasmine.getEnv().beforeEach = originalJasmineBeforeEach; }; |
| 70 | + |
| 71 | + const shouldNotSucceed = |
| 72 | + (done: DoneFn) => () => done.fail( 'Expected an error, but did not get one.'); |
| 73 | + |
| 74 | + const shouldFail = |
| 75 | + (done: DoneFn, emsg: string) => (err: any) => { expect(err).toEqual(emsg); done(); }; |
| 76 | + |
| 77 | + it('should fail when an asynchronous error is thrown', (done: DoneFn) => { |
| 78 | + let itPromise = patchJasmineIt(); |
| 79 | + |
| 80 | + it('throws an async error', |
| 81 | + async(inject([], () => { setTimeout(() => { throw new Error('bar'); }, 0); }))); |
| 82 | + |
| 83 | + itPromise.then( |
| 84 | + shouldNotSucceed(done), |
| 85 | + err => { |
| 86 | + expect(err).toEqual('bar'); |
| 87 | + done(); |
| 88 | + }); |
| 89 | + restoreJasmineIt(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should fail when a returned promise is rejected', (done: DoneFn) => { |
| 93 | + let itPromise = patchJasmineIt(); |
| 94 | + |
| 95 | + it('should fail with an error from a promise', async(inject([], () => { |
| 96 | + let deferred = PromiseWrapper.completer(); |
| 97 | + let p = deferred.promise.then(() => { expect(1).toEqual(2); }); |
| 98 | + |
| 99 | + deferred.reject('baz'); |
| 100 | + return p; |
| 101 | + }))); |
| 102 | + |
| 103 | + itPromise.then( |
| 104 | + shouldNotSucceed(done), |
| 105 | + err => { |
| 106 | + expect(err).toEqual('Uncaught (in promise): baz'); |
| 107 | + done(); |
| 108 | + }); |
| 109 | + restoreJasmineIt(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should fail when an error occurs inside inject', (done: DoneFn) => { |
| 113 | + let itPromise = patchJasmineIt(); |
| 114 | + |
| 115 | + it('throws an error', inject([], () => { throw new Error('foo'); })); |
| 116 | + |
| 117 | + itPromise.then( |
| 118 | + shouldNotSucceed(done), |
| 119 | + shouldFail(done, 'foo') |
| 120 | + ); |
| 121 | + restoreJasmineIt(); |
| 122 | + }); |
| 123 | + |
| 124 | + // TODO(juliemr): reenable this test when we are using a test zone and can capture this error. |
| 125 | + it('should fail when an asynchronous error is thrown', (done: DoneFn) => { |
| 126 | + let itPromise = patchJasmineIt(); |
| 127 | + |
| 128 | + it('throws an async error', |
| 129 | + async(inject([], () => { setTimeout(() => { throw new Error('bar'); }, 0); }))); |
| 130 | + |
| 131 | + itPromise.then( |
| 132 | + shouldNotSucceed(done), |
| 133 | + shouldFail(done, 'bar') |
| 134 | + ); |
| 135 | + restoreJasmineIt(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should fail when XHR loading of a template fails', (done: DoneFn) => { |
| 139 | + let itPromise = patchJasmineIt(); |
| 140 | + |
| 141 | + it('should fail with an error from a promise', |
| 142 | + async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => { |
| 143 | + tcb.createAsync(BadTemplateUrl); |
| 144 | + }))); |
| 145 | + |
| 146 | + itPromise.then( |
| 147 | + shouldNotSucceed(done), |
| 148 | + shouldFail(done, 'Uncaught (in promise): Failed to load non-existant.html') |
| 149 | + ); |
| 150 | + restoreJasmineIt(); |
| 151 | + }, 10000); |
| 152 | + |
| 153 | + describe('using beforeEachProviders', () => { |
| 154 | + beforeEachProviders(() => [provide(FancyService, {useValue: new FancyService()})]); |
| 155 | + |
| 156 | + beforeEach( |
| 157 | + inject([FancyService], (service: FancyService) => { expect(service.value).toEqual('real value'); })); |
| 158 | + |
| 159 | + describe('nested beforeEachProviders', () => { |
| 160 | + |
| 161 | + it('should fail when the injector has already been used', () => { |
| 162 | + patchJasmineBeforeEach(); |
| 163 | + expect(() => { |
| 164 | + beforeEachProviders(() => [provide(FancyService, {useValue: new FancyService()})]); |
| 165 | + }) |
| 166 | + .toThrowError('beforeEachProviders was called after the injector had been used ' + |
| 167 | + 'in a beforeEach or it block. This invalidates the test injector'); |
| 168 | + restoreJasmineBeforeEach(); |
| 169 | + }); |
| 170 | + }); |
| 171 | + }); |
| 172 | +}); |
0 commit comments