refactor(test): rename test_lib to testing
Old test_lib is now testing_internal test_lib_public is now testing
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
describe,
|
||||
expect,
|
||||
fakeAsync,
|
||||
flushMicrotasks,
|
||||
iit,
|
||||
inject,
|
||||
it,
|
||||
Log,
|
||||
tick,
|
||||
xit
|
||||
} from 'angular2/testing_internal';
|
||||
import {TimerWrapper, PromiseWrapper} from 'angular2/src/core/facade/async';
|
||||
import {BaseException} from 'angular2/src/core/facade/exceptions';
|
||||
import {Parser} from 'angular2/src/core/change_detection/change_detection';
|
||||
|
||||
export function main() {
|
||||
describe('fake async', () => {
|
||||
it('should run synchronous code', () => {
|
||||
var ran = false;
|
||||
fakeAsync(() => { ran = true; })();
|
||||
|
||||
expect(ran).toEqual(true);
|
||||
});
|
||||
|
||||
it('should pass arguments to the wrapped function', () => {
|
||||
fakeAsync((foo, bar) => {
|
||||
expect(foo).toEqual('foo');
|
||||
expect(bar).toEqual('bar');
|
||||
})('foo', 'bar');
|
||||
});
|
||||
|
||||
it('should work with inject()',
|
||||
inject([Parser], fakeAsync((parser) => { expect(parser).toBeAnInstanceOf(Parser); })));
|
||||
|
||||
it('should throw on nested calls', () => {
|
||||
expect(() => { fakeAsync(() => { fakeAsync(() => null)(); })(); })
|
||||
.toThrowError('fakeAsync() calls can not be nested');
|
||||
});
|
||||
|
||||
it('should flush microtasks before returning', () => {
|
||||
var thenRan = false;
|
||||
|
||||
fakeAsync(() => { PromiseWrapper.resolve(null).then(_ => { thenRan = true; }); })();
|
||||
|
||||
expect(thenRan).toEqual(true);
|
||||
});
|
||||
|
||||
|
||||
it('should propagate the return value',
|
||||
() => { expect(fakeAsync(() => 'foo')()).toEqual('foo'); });
|
||||
|
||||
describe('Promise', () => {
|
||||
it('should run asynchronous code', fakeAsync(() => {
|
||||
var thenRan = false;
|
||||
PromiseWrapper.resolve(null).then((_) => { thenRan = true; });
|
||||
|
||||
expect(thenRan).toEqual(false);
|
||||
|
||||
flushMicrotasks();
|
||||
expect(thenRan).toEqual(true);
|
||||
}));
|
||||
|
||||
it('should run chained thens', fakeAsync(() => {
|
||||
var log = new Log();
|
||||
|
||||
PromiseWrapper.resolve(null).then((_) => log.add(1)).then((_) => log.add(2));
|
||||
|
||||
expect(log.result()).toEqual('');
|
||||
|
||||
flushMicrotasks();
|
||||
expect(log.result()).toEqual('1; 2');
|
||||
}));
|
||||
|
||||
it('should run Promise created in Promise', fakeAsync(() => {
|
||||
var log = new Log();
|
||||
|
||||
PromiseWrapper.resolve(null).then((_) => {
|
||||
log.add(1);
|
||||
PromiseWrapper.resolve(null).then((_) => log.add(2));
|
||||
});
|
||||
|
||||
expect(log.result()).toEqual('');
|
||||
|
||||
flushMicrotasks();
|
||||
expect(log.result()).toEqual('1; 2');
|
||||
}));
|
||||
|
||||
// TODO(vicb): check why this doesn't work in JS - linked to open issues on GH ?
|
||||
xit('should complain if the test throws an exception during async calls', () => {
|
||||
expect(() => {
|
||||
fakeAsync(() => {
|
||||
PromiseWrapper.resolve(null).then((_) => { throw new BaseException('async'); });
|
||||
flushMicrotasks();
|
||||
})();
|
||||
}).toThrowError('async');
|
||||
});
|
||||
|
||||
it('should complain if a test throws an exception', () => {
|
||||
expect(() => { fakeAsync(() => { throw new BaseException('sync'); })(); })
|
||||
.toThrowError('sync');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('timers', () => {
|
||||
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
|
||||
var ran = false;
|
||||
TimerWrapper.setTimeout(() => {ran = true}, 0);
|
||||
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
tick();
|
||||
expect(ran).toEqual(true);
|
||||
}));
|
||||
|
||||
|
||||
it('should run queued timer after sufficient clock ticks', fakeAsync(() => {
|
||||
var ran = false;
|
||||
TimerWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
|
||||
tick(6);
|
||||
expect(ran).toEqual(false);
|
||||
|
||||
tick(6);
|
||||
expect(ran).toEqual(true);
|
||||
}));
|
||||
|
||||
it('should run queued timer only once', fakeAsync(() => {
|
||||
var cycles = 0;
|
||||
TimerWrapper.setTimeout(() => { cycles++; }, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
}));
|
||||
|
||||
it('should not run cancelled timer', fakeAsync(() => {
|
||||
var ran = false;
|
||||
var id = TimerWrapper.setTimeout(() => { ran = true; }, 10);
|
||||
TimerWrapper.clearTimeout(id);
|
||||
|
||||
tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
}));
|
||||
|
||||
it('should throw an error on dangling timers', () => {
|
||||
expect(() => { fakeAsync(() => { TimerWrapper.setTimeout(() => {}, 10); })(); })
|
||||
.toThrowError('1 timer(s) still in the queue.');
|
||||
});
|
||||
|
||||
it('should throw an error on dangling periodic timers', () => {
|
||||
expect(() => { fakeAsync(() => { TimerWrapper.setInterval(() => {}, 10); })(); })
|
||||
.toThrowError('1 periodic timer(s) still in the queue.');
|
||||
});
|
||||
|
||||
it('should run periodic timers', fakeAsync(() => {
|
||||
var cycles = 0;
|
||||
var id = TimerWrapper.setInterval(() => { cycles++; }, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(2);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(3);
|
||||
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
|
||||
it('should not run cancelled periodic timer', fakeAsync(() => {
|
||||
var ran = false;
|
||||
var id = TimerWrapper.setInterval(() => { ran = true; }, 10);
|
||||
TimerWrapper.clearInterval(id);
|
||||
|
||||
tick(10);
|
||||
expect(ran).toEqual(false);
|
||||
}));
|
||||
|
||||
it('should be able to cancel periodic timers from a callback', fakeAsync(() => {
|
||||
var cycles = 0;
|
||||
var id;
|
||||
|
||||
id = TimerWrapper.setInterval(() => {
|
||||
cycles++;
|
||||
TimerWrapper.clearInterval(id);
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
|
||||
tick(10);
|
||||
expect(cycles).toEqual(1);
|
||||
}));
|
||||
|
||||
it('should process microtasks before timers', fakeAsync(() => {
|
||||
var log = new Log();
|
||||
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
|
||||
|
||||
TimerWrapper.setTimeout(() => log.add('timer'), 9);
|
||||
|
||||
var id = TimerWrapper.setInterval(() => log.add('periodic timer'), 10);
|
||||
|
||||
expect(log.result()).toEqual('');
|
||||
|
||||
tick(10);
|
||||
expect(log.result()).toEqual('microtask; timer; periodic timer');
|
||||
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
|
||||
it('should process micro-tasks created in timers before next timers', fakeAsync(() => {
|
||||
var log = new Log();
|
||||
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('microtask'));
|
||||
|
||||
TimerWrapper.setTimeout(() => {
|
||||
log.add('timer');
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('t microtask'));
|
||||
}, 9);
|
||||
|
||||
var id = TimerWrapper.setInterval(() => {
|
||||
log.add('periodic timer');
|
||||
PromiseWrapper.resolve(null).then((_) => log.add('pt microtask'));
|
||||
}, 10);
|
||||
|
||||
tick(10);
|
||||
expect(log.result())
|
||||
.toEqual('microtask; timer; t microtask; periodic timer; pt microtask');
|
||||
|
||||
tick(10);
|
||||
expect(log.result())
|
||||
.toEqual(
|
||||
'microtask; timer; t microtask; periodic timer; pt microtask; periodic timer; pt microtask');
|
||||
|
||||
TimerWrapper.clearInterval(id);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('outside of the fakeAsync zone', () => {
|
||||
it('calling flushMicrotasks should throw', () => {
|
||||
expect(() => { flushMicrotasks(); })
|
||||
.toThrowError('The code should be running in the fakeAsync zone to call this function');
|
||||
});
|
||||
|
||||
it('calling tick should throw', () => {
|
||||
expect(() => { tick(); })
|
||||
.toThrowError('The code should be running in the fakeAsync zone to call this function');
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
import {
|
||||
AsyncTestCompleter,
|
||||
beforeEach,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
dispatchEvent,
|
||||
expect,
|
||||
iit,
|
||||
inject,
|
||||
beforeEachBindings,
|
||||
it,
|
||||
xit,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {Injectable, NgIf, provide} from 'angular2/core';
|
||||
import {Directive, Component, View, ViewMetadata} from 'angular2/src/core/metadata';
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({template: `<span>Original {{childBinding}}</span>`, directives: []})
|
||||
@Injectable()
|
||||
class ChildComp {
|
||||
childBinding: string;
|
||||
constructor() { this.childBinding = 'Child'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({template: `<span>Mock</span>`})
|
||||
@Injectable()
|
||||
class MockChildComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent-comp'})
|
||||
@View({template: `Parent(<child-comp></child-comp>)`, directives: [ChildComp]})
|
||||
@Injectable()
|
||||
class ParentComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'my-if-comp'})
|
||||
@View({template: `MyIf(<span *ng-if="showMore">More</span>)`, directives: [NgIf]})
|
||||
@Injectable()
|
||||
class MyIfComp {
|
||||
showMore: boolean = false;
|
||||
}
|
||||
|
||||
@Component({selector: 'child-child-comp'})
|
||||
@View({template: `<span>ChildChild</span>`})
|
||||
@Injectable()
|
||||
class ChildChildComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({
|
||||
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
||||
directives: [ChildChildComp]
|
||||
})
|
||||
@Injectable()
|
||||
class ChildWithChildComp {
|
||||
childBinding: string;
|
||||
constructor() { this.childBinding = 'Child'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'child-child-comp'})
|
||||
@View({template: `<span>ChildChild Mock</span>`})
|
||||
@Injectable()
|
||||
class MockChildChildComp {
|
||||
}
|
||||
|
||||
|
||||
|
||||
class FancyService {
|
||||
value: string = 'real value';
|
||||
}
|
||||
|
||||
class MockFancyService extends FancyService {
|
||||
value: string = 'mocked out value';
|
||||
}
|
||||
|
||||
@Component({selector: 'my-service-comp', bindings: [FancyService]})
|
||||
@View({template: `injected value: {{fancyService.value}}`})
|
||||
class TestBindingsComp {
|
||||
constructor(private fancyService: FancyService) {}
|
||||
}
|
||||
|
||||
@Component({selector: 'my-service-comp', viewProviders: [FancyService]})
|
||||
@View({template: `injected value: {{fancyService.value}}`})
|
||||
class TestViewBindingsComp {
|
||||
constructor(private fancyService: FancyService) {}
|
||||
}
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('test component builder', function() {
|
||||
it('should instantiate a component with valid DOM',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.createAsync(ChildComp).then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Original Child');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should allow changing members of the component',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.createAsync(MyIfComp).then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('MyIf()');
|
||||
|
||||
rootTestComponent.debugElement.componentInstance.showMore = true;
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('MyIf(More)');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a template',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
|
||||
.createAsync(MockChildComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Mock');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a view',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideView(ChildComp,
|
||||
new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
|
||||
.createAsync(ChildComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Modified Child');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override component dependencies',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
|
||||
.createAsync(ParentComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Parent(Mock)');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it("should override child component's dependencies",
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideDirective(ParentComp, ChildComp, ChildWithChildComp)
|
||||
.overrideDirective(ChildWithChildComp, ChildChildComp, MockChildChildComp)
|
||||
.createAsync(ParentComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
||||
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a provider',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideProviders(TestBindingsComp,
|
||||
[provide(FancyService, {useClass: MockFancyService})])
|
||||
.createAsync(TestBindingsComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('injected value: mocked out value');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should override a viewBinding',
|
||||
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
|
||||
|
||||
tcb.overrideViewProviders(TestViewBindingsComp,
|
||||
[provide(FancyService, {useClass: MockFancyService})])
|
||||
.createAsync(TestViewBindingsComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('injected value: mocked out value');
|
||||
async.done();
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import {
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
ddescribe,
|
||||
expect,
|
||||
tick,
|
||||
SpyObject,
|
||||
beforeEach,
|
||||
proxy,
|
||||
containsRegexp
|
||||
} from 'angular2/testing_internal';
|
||||
|
||||
import {DOM} from 'angular2/src/core/dom/dom_adapter';
|
||||
import {MapWrapper} from 'angular2/src/core/facade/collection';
|
||||
import {RegExpWrapper} from 'angular2/src/core/facade/lang';
|
||||
|
||||
class TestObj {
|
||||
prop;
|
||||
constructor(prop) { this.prop = prop; }
|
||||
someFunc(): number { return -1; }
|
||||
someComplexFunc(a) { return a; }
|
||||
}
|
||||
|
||||
class SpyTestObj extends SpyObject {
|
||||
constructor() { super(TestObj); }
|
||||
noSuchMethod(m) { return super.noSuchMethod(m) }
|
||||
}
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('testing', () => {
|
||||
describe('equality', () => {
|
||||
it('should structurally compare objects', () => {
|
||||
var expected = new TestObj(new TestObj({'one': [1, 2]}));
|
||||
var actual = new TestObj(new TestObj({'one': [1, 2]}));
|
||||
var falseActual = new TestObj(new TestObj({'one': [1, 3]}));
|
||||
|
||||
expect(actual).toEqual(expected);
|
||||
expect(falseActual).not.toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("toHaveCssClass", () => {
|
||||
it("should assert that the CSS class is present", () => {
|
||||
var el = DOM.createElement('div');
|
||||
DOM.addClass(el, 'matias');
|
||||
expect(el).toHaveCssClass('matias');
|
||||
});
|
||||
|
||||
it("should assert that the CSS class is not present", () => {
|
||||
var el = DOM.createElement('div');
|
||||
DOM.addClass(el, 'matias');
|
||||
expect(el).not.toHaveCssClass('fatias');
|
||||
});
|
||||
});
|
||||
|
||||
describe('toEqual for Maps', () => {
|
||||
it('should detect equality for same reference', () => {
|
||||
var m1 = MapWrapper.createFromStringMap({'a': 1});
|
||||
expect(m1).toEqual(m1);
|
||||
});
|
||||
|
||||
it('should detect equality for same content', () => {
|
||||
expect(MapWrapper.createFromStringMap({'a': 1}))
|
||||
.toEqual(MapWrapper.createFromStringMap({'a': 1}));
|
||||
});
|
||||
|
||||
it('should detect missing entries', () => {
|
||||
expect(MapWrapper.createFromStringMap({'a': 1}))
|
||||
.not.toEqual(MapWrapper.createFromStringMap({}));
|
||||
});
|
||||
|
||||
it('should detect different values', () => {
|
||||
expect(MapWrapper.createFromStringMap({'a': 1}))
|
||||
.not.toEqual(MapWrapper.createFromStringMap({'a': 2}));
|
||||
});
|
||||
|
||||
it('should detect additional entries', () => {
|
||||
expect(MapWrapper.createFromStringMap({'a': 1}))
|
||||
.not.toEqual(MapWrapper.createFromStringMap({'a': 1, 'b': 1}));
|
||||
});
|
||||
});
|
||||
|
||||
describe("spy objects", () => {
|
||||
var spyObj;
|
||||
|
||||
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
|
||||
|
||||
it("should return a new spy func with no calls",
|
||||
() => { expect(spyObj.spy("someFunc")).not.toHaveBeenCalled(); });
|
||||
|
||||
it("should record function calls", () => {
|
||||
spyObj.spy("someFunc").andCallFake((a, b) => {return a + b});
|
||||
|
||||
expect(spyObj.someFunc(1, 2)).toEqual(3);
|
||||
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1, 2);
|
||||
});
|
||||
|
||||
it("should match multiple function calls", () => {
|
||||
spyObj.someFunc(1, 2);
|
||||
spyObj.someFunc(3, 4);
|
||||
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1, 2);
|
||||
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(3, 4);
|
||||
});
|
||||
|
||||
it("should match null arguments", () => {
|
||||
spyObj.someFunc(null, "hello");
|
||||
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(null, "hello");
|
||||
});
|
||||
|
||||
it("should match using deep equality", () => {
|
||||
spyObj.someComplexFunc([1]);
|
||||
expect(spyObj.spy("someComplexFunc")).toHaveBeenCalledWith([1]);
|
||||
});
|
||||
|
||||
it("should support stubs", () => {
|
||||
var s = SpyObject.stub({"a": 1}, {"b": 2});
|
||||
|
||||
expect(s.a()).toEqual(1);
|
||||
expect(s.b()).toEqual(2);
|
||||
});
|
||||
|
||||
it('should create spys for all methods',
|
||||
() => { expect(() => spyObj.someFunc()).not.toThrow(); });
|
||||
|
||||
it('should create a default spy that does not fail for numbers', () => {
|
||||
// Previously needed for rtts_assert. Revisit this behavior.
|
||||
expect(spyObj.someFunc()).toBe(null);
|
||||
});
|
||||
});
|
||||
|
||||
describe('containsRegexp', () => {
|
||||
|
||||
it('should allow any prefix and suffix', () => {
|
||||
expect(RegExpWrapper.firstMatch(containsRegexp('b'), 'abc')).toBeTruthy();
|
||||
expect(RegExpWrapper.firstMatch(containsRegexp('b'), 'adc')).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should match various special characters', () => {
|
||||
expect(RegExpWrapper.firstMatch(containsRegexp('a.b'), 'a.b')).toBeTruthy();
|
||||
expect(RegExpWrapper.firstMatch(containsRegexp('axb'), 'a.b')).toBeFalsy();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
library angular2.test.testing.testing_spec;
|
||||
|
||||
/**
|
||||
* This is intentionally left blank. The public test lib is only for TS/JS
|
||||
* apps.
|
||||
*/
|
||||
main() {}
|
||||
@@ -0,0 +1,341 @@
|
||||
import {
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
describe,
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
expect,
|
||||
tick,
|
||||
beforeEach,
|
||||
inject,
|
||||
injectAsync,
|
||||
beforeEachProviders,
|
||||
TestComponentBuilder
|
||||
} from 'angular2/testing';
|
||||
|
||||
import {Injectable, NgIf, bind} from 'angular2/core';
|
||||
import {Directive, Component, View, ViewMetadata} from 'angular2/angular2';
|
||||
|
||||
// Services, and components for the tests.
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({template: `<span>Original {{childBinding}}</span>`, directives: []})
|
||||
@Injectable()
|
||||
class ChildComp {
|
||||
childBinding: string;
|
||||
constructor() { this.childBinding = 'Child'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({template: `<span>Mock</span>`})
|
||||
@Injectable()
|
||||
class MockChildComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'parent-comp'})
|
||||
@View({template: `Parent(<child-comp></child-comp>)`, directives: [ChildComp]})
|
||||
@Injectable()
|
||||
class ParentComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'my-if-comp'})
|
||||
@View({template: `MyIf(<span *ng-if="showMore">More</span>)`, directives: [NgIf]})
|
||||
@Injectable()
|
||||
class MyIfComp {
|
||||
showMore: boolean = false;
|
||||
}
|
||||
|
||||
@Component({selector: 'child-child-comp'})
|
||||
@View({template: `<span>ChildChild</span>`})
|
||||
@Injectable()
|
||||
class ChildChildComp {
|
||||
}
|
||||
|
||||
@Component({selector: 'child-comp'})
|
||||
@View({
|
||||
template: `<span>Original {{childBinding}}(<child-child-comp></child-child-comp>)</span>`,
|
||||
directives: [ChildChildComp]
|
||||
})
|
||||
@Injectable()
|
||||
class ChildWithChildComp {
|
||||
childBinding: string;
|
||||
constructor() { this.childBinding = 'Child'; }
|
||||
}
|
||||
|
||||
@Component({selector: 'child-child-comp'})
|
||||
@View({template: `<span>ChildChild Mock</span>`})
|
||||
@Injectable()
|
||||
class MockChildChildComp {
|
||||
}
|
||||
|
||||
class FancyService {
|
||||
value: string = 'real value';
|
||||
getAsyncValue() { return Promise.resolve('async value'); }
|
||||
}
|
||||
|
||||
class MockFancyService extends FancyService {
|
||||
value: string = 'mocked out value';
|
||||
}
|
||||
|
||||
@Component({selector: 'my-service-comp', providers: [FancyService]})
|
||||
@View({template: `injected value: {{fancyService.value}}`})
|
||||
class TestProvidersComp {
|
||||
constructor(private fancyService: FancyService) {}
|
||||
}
|
||||
|
||||
@Component({selector: 'my-service-comp', viewProviders: [FancyService]})
|
||||
@View({template: `injected value: {{fancyService.value}}`})
|
||||
class TestViewProvidersComp {
|
||||
constructor(private fancyService: FancyService) {}
|
||||
}
|
||||
|
||||
|
||||
export function main() {
|
||||
describe('angular2 jasmine matchers', () => {
|
||||
describe('toHaveCssClass', () => {
|
||||
it('should assert that the CSS class is present', () => {
|
||||
var el = document.createElement('div');
|
||||
el.classList.add('matias');
|
||||
expect(el).toHaveCssClass('matias');
|
||||
});
|
||||
|
||||
it('should assert that the CSS class is not present', () => {
|
||||
var el = document.createElement('div');
|
||||
el.classList.add('matias');
|
||||
expect(el).not.toHaveCssClass('fatias');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('using the test injector with the inject helper', () => {
|
||||
it('should run normal tests', () => { expect(true).toEqual(true); });
|
||||
|
||||
it('should run normal async tests', (done) => {
|
||||
setTimeout(() => {
|
||||
expect(true).toEqual(true);
|
||||
done();
|
||||
}, 0);
|
||||
});
|
||||
|
||||
describe('setting up Providers', () => {
|
||||
beforeEachProviders(() => [bind(FancyService).toValue(new FancyService())]);
|
||||
|
||||
it('should use set up providers',
|
||||
inject([FancyService], (service) => { expect(service.value).toEqual('real value'); }));
|
||||
|
||||
it('should wait until returned promises', injectAsync([FancyService], (service) => {
|
||||
return service.getAsyncValue().then(
|
||||
(value) => { expect(value).toEqual('async value'); });
|
||||
}));
|
||||
|
||||
describe('using beforeEach', () => {
|
||||
beforeEach(inject([FancyService],
|
||||
(service) => { service.value = 'value modified in beforeEach'; }));
|
||||
|
||||
it('should use modified providers', inject([FancyService], (service) => {
|
||||
expect(service.value).toEqual('value modified in beforeEach');
|
||||
}));
|
||||
});
|
||||
|
||||
describe('using async beforeEach', () => {
|
||||
beforeEach(injectAsync([FancyService], (service) => {
|
||||
return service.getAsyncValue().then((value) => { service.value = value; });
|
||||
}));
|
||||
|
||||
it('should use asynchronously modified value',
|
||||
inject([FancyService], (service) => { expect(service.value).toEqual('async value'); }));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('errors', () => {
|
||||
var originalJasmineIt: any;
|
||||
var originalJasmineBeforeEach: any;
|
||||
var patchJasmineIt = () => {
|
||||
originalJasmineIt = jasmine.getEnv().it;
|
||||
jasmine.getEnv().it = (description: string, fn) => {
|
||||
var done = () => {};
|
||||
(<any>done).fail = (err) => { throw new Error(err) };
|
||||
fn(done);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
var restoreJasmineIt = () => { jasmine.getEnv().it = originalJasmineIt; };
|
||||
|
||||
var patchJasmineBeforeEach = () => {
|
||||
originalJasmineBeforeEach = jasmine.getEnv().beforeEach;
|
||||
jasmine.getEnv().beforeEach = (fn: any) => {
|
||||
var done = () => {};
|
||||
(<any>done).fail = (err) => { throw new Error(err) };
|
||||
fn(done);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
var restoreJasmineBeforeEach =
|
||||
() => { jasmine.getEnv().beforeEach = originalJasmineBeforeEach; }
|
||||
|
||||
it('should fail when return was forgotten in it', () => {
|
||||
expect(() => {
|
||||
patchJasmineIt();
|
||||
it('forgets to return a promise', injectAsync([], () => { return true; }));
|
||||
})
|
||||
.toThrowError('Error: injectAsync was expected to return a promise, but the ' +
|
||||
' returned value was: true');
|
||||
restoreJasmineIt();
|
||||
});
|
||||
|
||||
it('should fail when synchronous spec returns promise', () => {
|
||||
expect(() => {
|
||||
patchJasmineIt();
|
||||
it('returns an extra promise', inject([], () => { return Promise.resolve('true'); }));
|
||||
}).toThrowError('inject returned a promise. Did you mean to use injectAsync?');
|
||||
restoreJasmineIt();
|
||||
});
|
||||
|
||||
it('should fail when return was forgotten in beforeEach', () => {
|
||||
expect(() => {
|
||||
patchJasmineBeforeEach();
|
||||
beforeEach(injectAsync([], () => { return true; }));
|
||||
})
|
||||
.toThrowError('Error: injectAsync was expected to return a promise, but the ' +
|
||||
' returned value was: true');
|
||||
restoreJasmineBeforeEach();
|
||||
});
|
||||
|
||||
it('should fail when synchronous beforeEach returns promise', () => {
|
||||
expect(() => {
|
||||
patchJasmineBeforeEach();
|
||||
beforeEach(inject([], () => { return Promise.resolve('true'); }));
|
||||
}).toThrowError('inject returned a promise. Did you mean to use injectAsync?');
|
||||
restoreJasmineBeforeEach();
|
||||
});
|
||||
|
||||
describe('using beforeEachProviders', () => {
|
||||
beforeEachProviders(() => [bind(FancyService).toValue(new FancyService())]);
|
||||
|
||||
beforeEach(
|
||||
inject([FancyService], (service) => { expect(service.value).toEqual('real value'); }));
|
||||
|
||||
describe('nested beforeEachProviders', () => {
|
||||
|
||||
it('should fail when the injector has already been used', () => {
|
||||
expect(() => {
|
||||
patchJasmineBeforeEach();
|
||||
beforeEachProviders(() => [bind(FancyService).toValue(new FancyService())]);
|
||||
})
|
||||
.toThrowError('beforeEachProviders was called after the injector had been used ' +
|
||||
'in a beforeEach or it block. This invalidates the test injector');
|
||||
restoreJasmineBeforeEach();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('test component builder', function() {
|
||||
it('should instantiate a component with valid DOM',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.createAsync(ChildComp).then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Original Child');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should allow changing members of the component',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.createAsync(MyIfComp).then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('MyIf()');
|
||||
|
||||
rootTestComponent.debugElement.componentInstance.showMore = true;
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('MyIf(More)');
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a template',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideTemplate(MockChildComp, '<span>Mock</span>')
|
||||
.createAsync(MockChildComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Mock');
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a view',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideView(
|
||||
ChildComp,
|
||||
new ViewMetadata({template: '<span>Modified {{childBinding}}</span>'}))
|
||||
.createAsync(ChildComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Modified Child');
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override component dependencies',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideDirective(ParentComp, ChildComp, MockChildComp)
|
||||
.createAsync(ParentComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement).toHaveText('Parent(Mock)');
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it("should override child component's dependencies",
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideDirective(ParentComp, ChildComp, ChildWithChildComp)
|
||||
.overrideDirective(ChildWithChildComp, ChildChildComp, MockChildChildComp)
|
||||
.createAsync(ParentComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('Parent(Original Child(ChildChild Mock))');
|
||||
|
||||
});
|
||||
}));
|
||||
|
||||
it('should override a provider',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideProviders(TestProvidersComp,
|
||||
[bind(FancyService).toClass(MockFancyService)])
|
||||
.createAsync(TestProvidersComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('injected value: mocked out value');
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
it('should override a viewProvider',
|
||||
injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
|
||||
|
||||
return tcb.overrideViewProviders(TestViewProvidersComp,
|
||||
[bind(FancyService).toClass(MockFancyService)])
|
||||
.createAsync(TestViewProvidersComp)
|
||||
.then((rootTestComponent) => {
|
||||
rootTestComponent.detectChanges();
|
||||
expect(rootTestComponent.debugElement.nativeElement)
|
||||
.toHaveText('injected value: mocked out value');
|
||||
});
|
||||
}));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
import {describe, it, iit, ddescribe, expect, BrowserDetection} from 'angular2/testing_internal';
|
||||
import {StringMapWrapper} from 'angular2/src/core/facade/collection';
|
||||
|
||||
export function main() {
|
||||
describe('BrowserDetection', () => {
|
||||
|
||||
var browsers = [
|
||||
{
|
||||
name: 'Chrome',
|
||||
ua: 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: true
|
||||
},
|
||||
{
|
||||
name: 'Chrome mobile',
|
||||
ua: 'Mozilla/5.0 (Linux; Android 5.1.1; D5803 Build/23.4.A.0.546) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.133 Mobile Safari/537.36',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: true
|
||||
},
|
||||
{
|
||||
name: 'Firefox',
|
||||
ua: 'Mozilla/5.0 (X11; Linux i686; rv:40.0) Gecko/20100101 Firefox/40.0',
|
||||
isFirefox: true,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: false,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'IE9',
|
||||
ua: 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727)',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: true,
|
||||
isWebkit: false,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'IE10',
|
||||
ua: 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C)',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: true,
|
||||
isWebkit: false,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'IE11',
|
||||
ua: 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: true,
|
||||
isWebkit: false,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Edge',
|
||||
ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: true,
|
||||
isIE: false,
|
||||
isWebkit: false,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Android4.1',
|
||||
ua: 'Mozilla/5.0 (Linux; U; Android 4.1.1; en-us; Android SDK built for x86 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
isFirefox: false,
|
||||
isAndroid: true,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Android4.2',
|
||||
ua: 'Mozilla/5.0 (Linux; U; Android 4.2; en-us; Android SDK built for x86 Build/JOP40C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
isFirefox: false,
|
||||
isAndroid: true,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Android4.3',
|
||||
ua: 'Mozilla/5.0 (Linux; U; Android 4.3; en-us; Android SDK built for x86 Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30',
|
||||
isFirefox: false,
|
||||
isAndroid: true,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Android4.4',
|
||||
ua: 'Mozilla/5.0 (Linux; Android 4.4.2; Android SDK built for x86 Build/KK) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Safari7',
|
||||
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/7.1.7 Safari/537.85.16',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'Safari8',
|
||||
ua: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'iOS7',
|
||||
ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 7_1 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D167 Safari/9537.53',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: true,
|
||||
isSlow: true,
|
||||
supportsIntlApi: false
|
||||
},
|
||||
{
|
||||
name: 'iOS8',
|
||||
ua: 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_4 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12H141 Safari/600.1.4',
|
||||
isFirefox: false,
|
||||
isAndroid: false,
|
||||
isEdge: false,
|
||||
isIE: false,
|
||||
isWebkit: true,
|
||||
isIOS7: false,
|
||||
isSlow: false,
|
||||
supportsIntlApi: false
|
||||
}
|
||||
];
|
||||
|
||||
browsers.forEach((browser: {[key: string]: any}) => {
|
||||
it(`should detect ${StringMapWrapper.get(browser, 'name')}`, () => {
|
||||
var bd = new BrowserDetection(<string>StringMapWrapper.get(browser, 'ua'));
|
||||
expect(bd.isFirefox).toBe(StringMapWrapper.get(browser, 'isFirefox'));
|
||||
expect(bd.isAndroid).toBe(StringMapWrapper.get(browser, 'isAndroid'));
|
||||
expect(bd.isEdge).toBe(StringMapWrapper.get(browser, 'isEdge'));
|
||||
expect(bd.isIE).toBe(StringMapWrapper.get(browser, 'isIE'));
|
||||
expect(bd.isWebkit).toBe(StringMapWrapper.get(browser, 'isWebkit'));
|
||||
expect(bd.isIOS7).toBe(StringMapWrapper.get(browser, 'isIOS7'));
|
||||
expect(bd.isSlow).toBe(StringMapWrapper.get(browser, 'isSlow'));
|
||||
expect(bd.supportsIntlApi).toBe(StringMapWrapper.get(browser, 'supportsIntlApi'));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user