From bdf9094e88e485e03b7af1aeccafa95200bd4f7b Mon Sep 17 00:00:00 2001 From: Chris Price Date: Tue, 28 Jun 2016 13:56:53 +0100 Subject: [PATCH] test(Observable.from): add coverage matrix for arguments --- spec/observables/from-spec.ts | 336 ++++++++++++++-------------------- 1 file changed, 138 insertions(+), 198 deletions(-) diff --git a/spec/observables/from-spec.ts b/spec/observables/from-spec.ts index b58a89792b..2f60af3092 100644 --- a/spec/observables/from-spec.ts +++ b/spec/observables/from-spec.ts @@ -1,6 +1,5 @@ import {expect} from 'chai'; import * as Rx from '../../dist/cjs/Rx'; -import {$$iterator} from '../../dist/cjs/symbol/iterator'; declare const {asDiagram, expectObservable, Symbol, type}; declare const rxTestScheduler: Rx.TestScheduler; @@ -17,191 +16,6 @@ describe('Observable.from', () => { expectObservable(e1).toBe(expected, {x: 10, y: 20, z: 30}); }); - it('should enumerate an Array', function (done: MochaDone) { - this.timeout(300); - - const expected = [1, 2, 3]; - let i = 0; - - Observable.from(expected).subscribe((x: number) => { - expect(x).to.equal(expected[i++]); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle an ArrayLike', function (done: MochaDone) { - this.timeout(300); - - const arrayLike: ArrayLike = { - length: 3, - 0: 1, - 1: 2, - 2: 3 - }; - const expected = [1, 2, 3]; - - Observable.from(arrayLike).subscribe((x: number) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle an ArrayLike from arguments', function (done: MochaDone) { - this.timeout(300); - - function makeArrayLike(...args) { - const expected = [1, 2, 3]; - - Observable.from(arguments).subscribe((x: number) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - } - - makeArrayLike(1, 2, 3); - }); - - it('should handle an ArrayLike with a mapFn', function (done: MochaDone) { - this.timeout(300); - - const arrayLike: ArrayLike = { - length: 3, - 0: 1, - 1: 2, - 2: 3 - }; - const expected = [1, 1, 1]; - const mapFn = (v, k) => v - k; - - Observable.from(arrayLike, mapFn).subscribe((x: number) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle an ArrayLike with a thisArg', (done: MochaDone) => { - const arrayLike: ArrayLike = { - length: 3, - 0: 1, - 1: 2, - 2: 3 - }; - const expected = [123, 123, 123]; - const mapFn = function (x, y) { - return this.thing; - }; - - Observable.from(arrayLike, mapFn, {thing: 123}).subscribe((x: number) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle a promise', (done: MochaDone) => { - const promise = Promise.resolve('pinky swear'); - - Observable.from(promise).subscribe((x: string) => { - expect(x).to.equal('pinky swear'); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle an "observableque" object', (done: MochaDone) => { - const observablesque = {}; - - observablesque[Symbol.observable] = () => { - return { - subscribe: (observer: Rx.Observer) => { - observer.next('test'); - observer.complete(); - } - }; - }; - - Observable.from(observablesque).subscribe((x: string) => { - expect(x).to.equal('test'); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should accept scheduler for observableque object', () => { - const observablesque = {}; - - observablesque[Symbol.observable] = () => { - return { - subscribe: (observer: Rx.Observer) => { - observer.next('x'); - observer.complete(); - } - }; - }; - - const e1 = Observable.from(observablesque, rxTestScheduler); - const expected = '(x|)'; - - expectObservable(e1).toBe(expected); - }); - - it('should handle a string', (done: MochaDone) => { - const expected = ['a', 'b', 'c']; - Observable.from('abc').subscribe((x: string) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - - it('should handle any iterable thing', (done: MochaDone) => { - const iterable = {}; - const iteratorResults = [ - { value: 'one', done: false }, - { value: 'two', done: false }, - { done: true } - ]; - const expected = ['one', 'two']; - - expect($$iterator).to.equal(Symbol.iterator); - - iterable[Symbol.iterator] = () => { - return { - next: () => { - return iteratorResults.shift(); - } - }; - }; - - Observable.from(iterable).subscribe((x: string) => { - expect(x).to.equal(expected.shift()); - }, (x) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - it('should throw for non observable object', () => { const r = () => { Observable.from({}).subscribe(); @@ -210,18 +24,6 @@ describe('Observable.from', () => { expect(r).to.throw(); }); - it('should handle object has observable symbol', (done: MochaDone) => { - const value = 'x'; - - Observable.from(Observable.of(value)).subscribe((x: string) => { - expect(x).to.equal(value); - }, (err: any) => { - done(new Error('should not be called')); - }, () => { - done(); - }); - }); - it('should return T for ObservableLike objects', () => { type(() => { /* tslint:disable:no-unused-variable */ @@ -239,4 +41,142 @@ describe('Observable.from', () => { /* tslint:enable:no-unused-variable */ }); }); + + const fakerator = (...values) => ({ + [Symbol.iterator]: () => { + const clone = [...values]; + return { + next: () => ({ + done: clone.length <= 0, + value: clone.shift() + }) + }; + } + }); + + const sources: { name: string, value: any }[] = [ + { name: 'observable', value: Observable.of('x') }, + { name: 'array', value: ['x'] }, + { name: 'promise', value: Promise.resolve('x') }, + { name: 'iterator', value: fakerator('x') }, + { name: 'array-like', value: { [0]: 'x', length: 1 }}, + { name: 'string', value: 'x'}, + { name: 'arguments', value: function(x) { return arguments; }('x') }, + ]; + + for (const source of sources) { + it(`should accept ${source.name}`, (done: MochaDone) => { + let nextInvoked = false; + Observable.from(source.value) + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('x'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + }); + it(`should accept ${source.name} and scheduler`, (done: MochaDone) => { + let nextInvoked = false; + Observable.from(source.value, Rx.Scheduler.async) + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('x'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + expect(nextInvoked).to.equal(false); + }); + it(`should accept ${source.name} and projection`, (done: MochaDone) => { + let nextInvoked = false; + Observable.from(source.value, x => x + 'x') + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('xx'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + }); + it(`should accept ${source.name}, projection and scheduler`, (done: MochaDone) => { + let nextInvoked = false; + Observable.from(source.value, x => x + 'x', Rx.Scheduler.async) + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('xx'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + expect(nextInvoked).to.equal(false); + }); + it(`should accept ${source.name}, projection and context`, (done: MochaDone) => { + const projection = function(x) { + expect(this.foo).to.equal('bar'); + return x + 'x'; + }; + let nextInvoked = false; + Observable.from(source.value, projection, { foo: 'bar' }) + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('xx'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + }); + it(`should accept ${source.name}, projection, context and scheduler`, (done: MochaDone) => { + const projection = function(x) { + expect(this.foo).to.equal('bar'); + return x + 'x'; + }; + let nextInvoked = false; + Observable.from(source.value, projection, { foo: 'bar' }, Rx.Scheduler.async) + .subscribe( + (x: string) => { + nextInvoked = true; + expect(x).to.equal('xx'); + }, + (x) => { + done(new Error('should not be called')); + }, + () => { + expect(nextInvoked).to.equal(true); + done(); + } + ); + expect(nextInvoked).to.equal(false); + }); + } });