本文整理汇总了TypeScript中@angular/core/testing/src/testing_internal.it函数的典型用法代码示例。如果您正苦于以下问题:TypeScript it函数的具体用法?TypeScript it怎么用?TypeScript it使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了it函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
t.describe('valid URLs', () => {
const validUrls = [
'',
'http://abc',
'HTTP://abc',
'https://abc',
'HTTPS://abc',
'ftp://abc',
'FTP://abc',
'mailto:me@example.com',
'MAILTO:me@example.com',
'tel:123-123-1234',
'TEL:123-123-1234',
'#anchor',
'/page1.md',
'http://JavaScript/my.js',
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/', // Truncated.
'data:video/webm;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/',
'data:audio/opus;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/',
];
for (const url of validUrls) {
t.it(`valid ${url}`, () => t.expect(sanitizeUrl(url)).toEqual(url));
}
});
示例2: describe
describe('isNumeric', () => {
it('should return true when passing correct numeric string',
() => { expect(isNumeric('2')).toBe(true); });
it('should return true when passing correct double string',
() => { expect(isNumeric('1.123')).toBe(true); });
it('should return true when passing correct negative string',
() => { expect(isNumeric('-2')).toBe(true); });
it('should return true when passing correct scientific notation string',
() => { expect(isNumeric('1e5')).toBe(true); });
it('should return false when passing incorrect numeric',
() => { expect(isNumeric('a')).toBe(false); });
it('should return false when passing parseable but non numeric',
() => { expect(isNumeric('2a')).toBe(false); });
});
示例3: describe
describe('required', () => {
it('should error on an empty string',
() => { expect(Validators.required(new FormControl(''))).toEqual({'required': true}); });
it('should error on null',
() => { expect(Validators.required(new FormControl(null))).toEqual({'required': true}); });
it('should not error on undefined', () => {
expect(Validators.required(new FormControl(undefined))).toEqual({'required': true});
});
it('should not error on a non-empty string',
() => { expect(Validators.required(new FormControl('not empty'))).toBeNull(); });
it('should accept zero as valid',
() => { expect(Validators.required(new FormControl(0))).toBeNull(); });
it('should error on an empty array',
() => expect(Validators.required(new FormControl([]))).toEqual({'required': true}));
it('should not error on a non-empty array',
() => expect(Validators.required(new FormControl([1, 2]))).toBeNull());
});
示例4: describe
describe('devModeEqual', () => {
it('should do the deep comparison of iterables', () => {
expect(devModeEqual([['one']], [['one']])).toBe(true);
expect(devModeEqual(['one'], ['one', 'two'])).toBe(false);
expect(devModeEqual(['one', 'two'], ['one'])).toBe(false);
expect(devModeEqual(['one'], 'one')).toBe(false);
expect(devModeEqual(['one'], new Object())).toBe(false);
expect(devModeEqual('one', ['one'])).toBe(false);
expect(devModeEqual(new Object(), ['one'])).toBe(false);
});
it('should compare primitive numbers', () => {
expect(devModeEqual(1, 1)).toBe(true);
expect(devModeEqual(1, 2)).toBe(false);
expect(devModeEqual(new Object(), 2)).toBe(false);
expect(devModeEqual(1, new Object())).toBe(false);
});
it('should compare primitive strings', () => {
expect(devModeEqual('one', 'one')).toBe(true);
expect(devModeEqual('one', 'two')).toBe(false);
expect(devModeEqual(new Object(), 'one')).toBe(false);
expect(devModeEqual('one', new Object())).toBe(false);
});
it('should compare primitive booleans', () => {
expect(devModeEqual(true, true)).toBe(true);
expect(devModeEqual(true, false)).toBe(false);
expect(devModeEqual(new Object(), true)).toBe(false);
expect(devModeEqual(true, new Object())).toBe(false);
});
it('should compare null', () => {
expect(devModeEqual(null, null)).toBe(true);
expect(devModeEqual(null, 1)).toBe(false);
expect(devModeEqual(new Object(), null)).toBe(false);
expect(devModeEqual(null, new Object())).toBe(false);
});
it('should return true for other objects',
() => { expect(devModeEqual(new Object(), new Object())).toBe(true); });
});
示例5: describe
describe('body serialization', () => {
const baseReq = new HttpRequest('/test/', 'POST', null);
it('handles a null body', () => { expect(baseReq.serializeBody()).toBeNull(); });
it('passes ArrayBuffers through', () => {
const body = new ArrayBuffer(4);
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('passes strings through', () => {
const body = 'hello world';
expect(baseReq.clone({body}).serializeBody()).toBe(body);
});
it('serializes arrays as json', () => {
expect(baseReq.clone({body: ['a', 'b']}).serializeBody()).toBe('["a","b"]');
});
it('handles numbers as json',
() => { expect(baseReq.clone({body: 314159}).serializeBody()).toBe('314159'); });
it('handles objects as json', () => {
const req = baseReq.clone({body: {data: 'test data'}});
expect(req.serializeBody()).toBe('{"data":"test data"}');
});
});
示例6: describe
describe('content type detection', () => {
const baseReq = new HttpRequest('POST', '/test', null);
it('handles a null body', () => { expect(baseReq.detectContentTypeHeader()).toBeNull(); });
it('doesn\'t associate a content type with ArrayBuffers', () => {
const req = baseReq.clone({body: new ArrayBuffer(4)});
expect(req.detectContentTypeHeader()).toBeNull();
});
it('handles strings as text', () => {
const req = baseReq.clone({body: 'hello world'});
expect(req.detectContentTypeHeader()).toBe('text/plain');
});
it('handles arrays as json', () => {
const req = baseReq.clone({body: ['a', 'b']});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
it('handles numbers as json', () => {
const req = baseReq.clone({body: 314159});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
it('handles objects as json', () => {
const req = baseReq.clone({body: {data: 'test data'}});
expect(req.detectContentTypeHeader()).toBe('application/json');
});
});
示例7: describe
describe('object dictionary', () => {
it('should transform a basic dictionary', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform({1: 2})).toEqual([{key: '1', value: 2}]);
});
it('should order by alpha', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform({'b': 1, 'a': 1})).toEqual([
{key: 'a', value: 1}, {key: 'b', value: 1}
]);
});
it('should order by numerical', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
expect(pipe.transform({2: 1, 1: 1})).toEqual([{key: '1', value: 1}, {key: '2', value: 1}]);
});
it('should order by numerical and alpha', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
const input = {2: 1, 1: 1, 'b': 1, 0: 1, 3: 1, 'a': 1};
expect(pipe.transform(input)).toEqual([
{key: '0', value: 1}, {key: '1', value: 1}, {key: '2', value: 1}, {key: '3', value: 1},
{key: 'a', value: 1}, {key: 'b', value: 1}
]);
});
it('should return the same ref if nothing changes', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
const transform1 = pipe.transform({1: 2});
const transform2 = pipe.transform({1: 2});
expect(transform1 === transform2).toEqual(true);
});
it('should return a new ref if something changes', () => {
const pipe = new KeyValuePipe(defaultKeyValueDiffers);
const transform1 = pipe.transform({1: 2});
const transform2 = pipe.transform({1: 3});
expect(transform1 !== transform2).toEqual(true);
});
});
示例8: describe
describe('QueryList', () => {
let queryList: QueryList<string>;
let log: string;
beforeEach(() => {
queryList = new QueryList<string>();
log = '';
});
function logAppend(item: any /** TODO #9100 */) { log += (log.length == 0 ? '' : ', ') + item; }
it('should support resetting and iterating over the new objects', () => {
queryList.reset(['one']);
queryList.reset(['two']);
iterateListLike(queryList, logAppend);
expect(log).toEqual('two');
});
it('should support length', () => {
queryList.reset(['one', 'two']);
expect(queryList.length).toEqual(2);
});
it('should support map', () => {
queryList.reset(['one', 'two']);
expect(queryList.map((x) => x)).toEqual(['one', 'two']);
});
it('should support map with index', () => {
queryList.reset(['one', 'two']);
expect(queryList.map((x, i) => `${x}_${i}`)).toEqual(['one_0', 'two_1']);
});
it('should support forEach', () => {
queryList.reset(['one', 'two']);
let join = '';
queryList.forEach((x) => join = join + x);
expect(join).toEqual('onetwo');
});
it('should support forEach with index', () => {
queryList.reset(['one', 'two']);
let join = '';
queryList.forEach((x, i) => join = join + x + i);
expect(join).toEqual('one0two1');
});
it('should support filter', () => {
queryList.reset(['one', 'two']);
expect(queryList.filter((x: string) => x == 'one')).toEqual(['one']);
});
it('should support filter with index', () => {
queryList.reset(['one', 'two']);
expect(queryList.filter((x: string, i: number) => i == 0)).toEqual(['one']);
});
it('should support find', () => {
queryList.reset(['one', 'two']);
expect(queryList.find((x: string) => x == 'two')).toEqual('two');
});
it('should support find with index', () => {
queryList.reset(['one', 'two']);
expect(queryList.find((x: string, i: number) => i == 1)).toEqual('two');
});
it('should support reduce', () => {
queryList.reset(['one', 'two']);
expect(queryList.reduce((a: string, x: string) => a + x, 'start:')).toEqual('start:onetwo');
});
it('should support reduce with index', () => {
queryList.reset(['one', 'two']);
expect(queryList.reduce((a: string, x: string, i: number) => a + x + i, 'start:'))
.toEqual('start:one0two1');
});
it('should support toArray', () => {
queryList.reset(['one', 'two']);
expect(queryList.reduce((a: string, x: string) => a + x, 'start:')).toEqual('start:onetwo');
});
it('should support toArray', () => {
queryList.reset(['one', 'two']);
expect(queryList.toArray()).toEqual(['one', 'two']);
});
it('should support toString', () => {
queryList.reset(['one', 'two']);
const listString = queryList.toString();
expect(listString.indexOf('one') != -1).toBeTruthy();
expect(listString.indexOf('two') != -1).toBeTruthy();
});
it('should support first and last', () => {
queryList.reset(['one', 'two', 'three']);
expect(queryList.first).toEqual('one');
expect(queryList.last).toEqual('three');
});
//.........这里部分代码省略.........
示例9: describe
describe('XhrBackend', () => {
let factory: MockXhrFactory = null !;
let backend: HttpXhrBackend = null !;
beforeEach(() => {
factory = new MockXhrFactory();
backend = new HttpXhrBackend(factory);
});
it('emits status immediately', () => {
const events = trackEvents(backend.handle(TEST_POST));
expect(events.length).toBe(1);
expect(events[0].type).toBe(HttpEventType.Sent);
});
it('sets method, url, and responseType correctly', () => {
backend.handle(TEST_POST).subscribe();
expect(factory.mock.method).toBe('POST');
expect(factory.mock.responseType).toBe('text');
expect(factory.mock.url).toBe('/test');
});
it('sets outgoing body correctly', () => {
backend.handle(TEST_POST).subscribe();
expect(factory.mock.body).toBe('some body');
});
it('sets outgoing headers, including default headers', () => {
const post = TEST_POST.clone({
setHeaders: {
'Test': 'Test header',
},
});
backend.handle(post).subscribe();
expect(factory.mock.mockHeaders).toEqual({
'Test': 'Test header',
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'text/plain',
});
});
it('sets outgoing headers, including overriding defaults', () => {
const setHeaders = {
'Test': 'Test header',
'Accept': 'text/html',
'Content-Type': 'text/css',
};
backend.handle(TEST_POST.clone({setHeaders})).subscribe();
expect(factory.mock.mockHeaders).toEqual(setHeaders);
});
it('passes withCredentials through', () => {
backend.handle(TEST_POST.clone({withCredentials: true})).subscribe();
expect(factory.mock.withCredentials).toBe(true);
});
it('handles a text response', () => {
const events = trackEvents(backend.handle(TEST_POST));
factory.mock.mockFlush(200, 'OK', 'some response');
expect(events.length).toBe(2);
expect(events[1].type).toBe(HttpEventType.Response);
expect(events[1] instanceof HttpResponse).toBeTruthy();
const res = events[1] as HttpResponse<string>;
expect(res.body).toBe('some response');
expect(res.status).toBe(200);
expect(res.statusText).toBe('OK');
});
it('handles a json response', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
factory.mock.mockFlush(200, 'OK', JSON.stringify({data: 'some data'}));
expect(events.length).toBe(2);
const res = events[1] as HttpResponse<{data: string}>;
expect(res.body !.data).toBe('some data');
});
it('handles a blank json response', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
factory.mock.mockFlush(200, 'OK', '');
expect(events.length).toBe(2);
const res = events[1] as HttpResponse<{data: string}>;
expect(res.body).toBeNull();
});
it('handles a json error response', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
factory.mock.mockFlush(500, 'Error', JSON.stringify({data: 'some data'}));
expect(events.length).toBe(2);
const res = events[1] as any as HttpErrorResponse;
expect(res.error !.data).toBe('some data');
});
it('handles a json error response with XSSI prefix', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
factory.mock.mockFlush(500, 'Error', XSSI_PREFIX + JSON.stringify({data: 'some data'}));
expect(events.length).toBe(2);
const res = events[1] as any as HttpErrorResponse;
expect(res.error !.data).toBe('some data');
});
it('handles a json string response', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
expect(factory.mock.responseType).toEqual('text');
factory.mock.mockFlush(200, 'OK', JSON.stringify('this is a string'));
expect(events.length).toBe(2);
const res = events[1] as HttpResponse<string>;
expect(res.body).toEqual('this is a string');
});
it('handles a json response with an XSSI prefix', () => {
const events = trackEvents(backend.handle(TEST_POST.clone({responseType: 'json'})));
factory.mock.mockFlush(200, 'OK', XSSI_PREFIX + JSON.stringify({data: 'some data'}));
expect(events.length).toBe(2);
const res = events[1] as HttpResponse<{data: string}>;
//.........这里部分代码省略.........
示例10: describe
describe('fake async', () => {
it('should run synchronous code', () => {
let ran = false;
fakeAsync(() => { ran = true; })();
expect(ran).toEqual(true);
});
it('should pass arguments to the wrapped function', () => {
fakeAsync((foo: any /** TODO #9100 */, bar: any /** TODO #9100 */) => {
expect(foo).toEqual('foo');
expect(bar).toEqual('bar');
})('foo', 'bar');
});
it('should work with inject()', fakeAsync(inject([Parser], (parser: any /** TODO #9100 */) => {
expect(parser).toBeAnInstanceOf(Parser);
})));
it('should throw on nested calls', () => {
expect(() => {
fakeAsync(() => { fakeAsync((): any /** TODO #9100 */ => null)(); })();
}).toThrowError('fakeAsync() calls can not be nested');
});
it('should flush microtasks before returning', () => {
let thenRan = false;
fakeAsync(() => { resolvedPromise.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(() => {
let thenRan = false;
resolvedPromise.then((_) => { thenRan = true; });
expect(thenRan).toEqual(false);
flushMicrotasks();
expect(thenRan).toEqual(true);
}));
it('should run chained thens', fakeAsync(() => {
const log = new Log();
resolvedPromise.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(() => {
const log = new Log();
resolvedPromise.then((_) => {
log.add(1);
resolvedPromise.then((_) => log.add(2));
});
expect(log.result()).toEqual('');
flushMicrotasks();
expect(log.result()).toEqual('1; 2');
}));
it('should complain if the test throws an exception during async calls', () => {
expect(() => {
fakeAsync(() => {
resolvedPromise.then((_) => { throw new Error('async'); });
flushMicrotasks();
})();
}).toThrowError(/Uncaught \(in promise\): Error: async/);
});
it('should complain if a test throws an exception', () => {
expect(() => { fakeAsync(() => { throw new Error('sync'); })(); }).toThrowError('sync');
});
});
describe('timers', () => {
it('should run queued zero duration timer on zero tick', fakeAsync(() => {
let ran = false;
setTimeout(() => { ran = true; }, 0);
expect(ran).toEqual(false);
tick();
expect(ran).toEqual(true);
}));
//.........这里部分代码省略.........