Skip to content

Commit

Permalink
test(Observable.from): add coverage matrix for arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisprice committed Aug 9, 2016
1 parent 3c35b94 commit bdf9094
Showing 1 changed file with 138 additions and 198 deletions.
336 changes: 138 additions & 198 deletions spec/observables/from-spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<number> = {
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<number> = {
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<number> = {
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 = <any>{};

observablesque[Symbol.observable] = () => {
return {
subscribe: (observer: Rx.Observer<string>) => {
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 = <any>{};

observablesque[Symbol.observable] = () => {
return {
subscribe: (observer: Rx.Observer<string>) => {
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 = <any>{};
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(<any>{}).subscribe();
Expand All @@ -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 */
Expand All @@ -239,4 +41,142 @@ describe('Observable.from', () => {
/* tslint:enable:no-unused-variable */
});
});

const fakerator = (...values) => ({
[<symbol>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);
});
}
});

0 comments on commit bdf9094

Please sign in to comment.