2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
|
2017-03-02 15:12:46 -05:00
|
|
|
import {SpyObject} from '@angular/core/testing/src/testing_internal';
|
2015-05-27 12:53:37 -04:00
|
|
|
|
2014-10-29 14:26:52 -04:00
|
|
|
class TestObj {
|
2016-06-20 17:21:01 -04:00
|
|
|
prop: any;
|
2020-04-13 19:40:21 -04:00
|
|
|
constructor(prop: any) {
|
|
|
|
this.prop = prop;
|
|
|
|
}
|
|
|
|
someFunc(): number {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
someComplexFunc(a: any) {
|
|
|
|
return a;
|
|
|
|
}
|
2014-10-29 14:26:52 -04:00
|
|
|
}
|
|
|
|
|
2017-12-15 19:28:41 -05:00
|
|
|
{
|
2015-10-13 03:29:13 -04:00
|
|
|
describe('testing', () => {
|
2018-03-25 15:11:49 -04:00
|
|
|
describe('should respect custom equality tester', () => {
|
|
|
|
beforeEach(() => {
|
2020-04-13 19:40:21 -04:00
|
|
|
const equalIfMarried = (first: any, second: any) => {
|
|
|
|
return first === 'kevin' && second === 'patricia';
|
|
|
|
};
|
2018-03-25 15:11:49 -04:00
|
|
|
jasmine.addCustomEqualityTester(equalIfMarried);
|
|
|
|
});
|
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('for positive test', () => {
|
|
|
|
expect('kevin').toEqual('patricia');
|
|
|
|
});
|
2018-03-25 15:11:49 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('for negative test', () => {
|
|
|
|
expect('kevin').not.toEqual('kevin');
|
|
|
|
});
|
2018-03-25 15:11:49 -04:00
|
|
|
});
|
|
|
|
|
2014-11-17 20:39:39 -05:00
|
|
|
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]}));
|
2014-10-29 14:26:52 -04:00
|
|
|
|
|
|
|
expect(actual).toEqual(expected);
|
|
|
|
expect(falseActual).not.toEqual(expected);
|
|
|
|
});
|
|
|
|
});
|
2014-11-17 20:39:39 -05:00
|
|
|
|
|
|
|
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);
|
2014-11-17 20:39:39 -05:00
|
|
|
expect(m1).toEqual(m1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect equality for same content', () => {
|
2016-11-03 19:58:27 -04:00
|
|
|
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);
|
2014-11-17 20:39:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2014-11-17 20:39:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2014-11-17 20:39:39 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should detect additional entries', () => {
|
2016-11-03 19:58:27 -04:00
|
|
|
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-17 20:39:39 -05:00
|
|
|
});
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
describe('spy objects', () => {
|
2016-10-11 18:44:48 -04:00
|
|
|
let spyObj: any;
|
2014-11-25 18:16:53 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
spyObj = new SpyObject(TestObj);
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
|
2020-04-13 19:40:21 -04: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
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should record function calls', () => {
|
2016-10-11 18:44:48 -04:00
|
|
|
spyObj.spy('someFunc').and.callFake((a: any, b: any) => a + b);
|
2014-11-25 18:16:53 -05:00
|
|
|
|
2015-05-27 12:53:37 -04:00
|
|
|
expect(spyObj.someFunc(1, 2)).toEqual(3);
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2);
|
2014-11-25 18:16:53 -05:00
|
|
|
});
|
2015-04-14 16:26:42 -04:00
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should match multiple function calls', () => {
|
2019-02-08 17:01:51 -05:00
|
|
|
spyObj.spy('someFunc');
|
2015-05-27 12:53:37 -04:00
|
|
|
spyObj.someFunc(1, 2);
|
|
|
|
spyObj.someFunc(3, 4);
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(1, 2);
|
|
|
|
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(3, 4);
|
2015-04-23 12:13:42 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should match null arguments', () => {
|
2019-02-08 17:01:51 -05:00
|
|
|
spyObj.spy('someFunc');
|
2016-06-08 19:38:52 -04:00
|
|
|
spyObj.someFunc(null, 'hello');
|
|
|
|
expect(spyObj.spy('someFunc')).toHaveBeenCalledWith(null, 'hello');
|
2015-05-07 20:18:42 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should match using deep equality', () => {
|
2019-02-08 17:01:51 -05:00
|
|
|
spyObj.spy('someComplexFunc');
|
2015-04-23 12:13:42 -04:00
|
|
|
spyObj.someComplexFunc([1]);
|
2016-06-08 19:38:52 -04:00
|
|
|
expect(spyObj.spy('someComplexFunc')).toHaveBeenCalledWith([1]);
|
2015-04-23 12:13:42 -04:00
|
|
|
});
|
|
|
|
|
2016-06-08 19:38:52 -04:00
|
|
|
it('should support stubs', () => {
|
2016-11-03 19:58:27 -04:00
|
|
|
const s = SpyObject.stub({'a': 1}, {'b': 2});
|
2015-04-17 19:08:59 -04:00
|
|
|
expect(s.a()).toEqual(1);
|
|
|
|
expect(s.b()).toEqual(2);
|
|
|
|
});
|
|
|
|
|
2019-02-08 17:01:51 -05:00
|
|
|
it('should create spys for all methods', () => {
|
|
|
|
spyObj.spy('someFunc');
|
|
|
|
expect(() => spyObj.someFunc()).not.toThrow();
|
|
|
|
});
|
2014-11-25 18:16:53 -05:00
|
|
|
});
|
2014-10-29 14:26:52 -04:00
|
|
|
});
|
2015-02-16 23:04:03 -05:00
|
|
|
}
|