angular-docs-cn/modules/angular2/test/test_lib/test_lib_spec.ts

148 lines
4.5 KiB
TypeScript
Raw Normal View History

import {
describe,
it,
iit,
ddescribe,
expect,
tick,
SpyObject,
beforeEach,
proxy,
containsRegexp
} from 'angular2/test_lib';
2015-08-20 17:28:25 -04:00
import {DOM} from 'angular2/src/core/dom/dom_adapter';
import {MapWrapper} from 'angular2/src/core/facade/collection';
2015-08-26 14:41:41 -04:00
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) }
}
2014-11-25 18:16:53 -05:00
export function main() {
describe('test_lib', () => {
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}));
});
});
2014-11-25 18:16:53 -05:00
describe("spy objects", () => {
var spyObj;
beforeEach(() => { spyObj = <any>new SpyTestObj(); });
2014-11-25 18:16:53 -05:00
it("should return a new spy func with no calls",
() => { expect(spyObj.spy("someFunc")).not.toHaveBeenCalled(); });
2014-11-25 18:16:53 -05:00
it("should record function calls", () => {
spyObj.spy("someFunc").andCallFake((a, b) => {return a + b});
2014-11-25 18:16:53 -05:00
expect(spyObj.someFunc(1, 2)).toEqual(3);
expect(spyObj.spy("someFunc")).toHaveBeenCalledWith(1, 2);
2014-11-25 18:16:53 -05:00
});
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);
});
2014-11-25 18:16:53 -05:00
});
2015-05-26 16:57:13 -04:00
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();
});
});
});
}