angular-cn/packages/core/test/testing_internal_spec.ts

131 lines
4.0 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {SpyObject} from '@angular/core/testing/src/testing_internal';
class TestObj {
prop: any;
constructor(prop: any) { this.prop = prop; }
someFunc(): number { return -1; }
someComplexFunc(a: any) { return a; }
}
{
describe('testing', () => {
describe('should respect custom equality tester', () => {
beforeEach(() => {
const equalIfMarried =
(first: any, second: any) => { return first === 'kevin' && second === 'patricia'; };
jasmine.addCustomEqualityTester(equalIfMarried);
});
it('for positive test', () => { expect('kevin').toEqual('patricia'); });
it('for negative test', () => { expect('kevin').not.toEqual('kevin'); });
});
describe('equality', () => {
it('should structurally compare objects', () => {
2016-11-03 20:05:03 -04:00
const expected = new TestObj(new TestObj({'one': [1, 2]}));
const actual = new TestObj(new TestObj({'one': [1, 2]}));
const falseActual = new TestObj(new TestObj({'one': [1, 3]}));
expect(actual).toEqual(expected);
expect(falseActual).not.toEqual(expected);
});
});
describe('toEqual for Maps', () => {
it('should detect equality for same reference', () => {
2016-11-03 20:05:03 -04:00
const m1: Map<string, number> = new Map();
m1.set('a', 1);
expect(m1).toEqual(m1);
});
it('should detect equality for same content', () => {
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
m2.set('a', 1);
expect(m1).toEqual(m2);
});
it('should detect missing entries', () => {
2016-11-03 20:05:03 -04:00
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
expect(m1).not.toEqual(m2);
});
it('should detect different values', () => {
2016-11-03 20:05:03 -04:00
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
m2.set('a', 2);
expect(m1).not.toEqual(m2);
});
it('should detect additional entries', () => {
const m1: Map<string, number> = new Map();
m1.set('a', 1);
const m2: Map<string, number> = new Map();
m2.set('a', 1);
m2.set('b', 2);
expect(m1).not.toEqual(m2);
});
});
2014-11-25 18:16:53 -05:00
describe('spy objects', () => {
let spyObj: any;
2014-11-25 18:16:53 -05:00
beforeEach(() => { spyObj = new SpyObject(TestObj); });
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').and.callFake((a: any, b: any) => 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.spy('someFunc');
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.spy('someFunc');
spyObj.someFunc(null, 'hello');
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(null, 'hello');
});
it('should match using deep equality', () => {
spyObj.spy('someComplexFunc');
spyObj.someComplexFunc([1]);
expect(spyObj.spy('someComplexFunc')).toHaveBeenCalledWith([1]);
});
it('should support stubs', () => {
const s = SpyObject.stub({'a': 1}, {'b': 2});
expect(s.a()).toEqual(1);
expect(s.b()).toEqual(2);
});
it('should create spys for all methods', () => {
spyObj.spy('someFunc');
expect(() => spyObj.someFunc()).not.toThrow();
});
2014-11-25 18:16:53 -05:00
});
});
}