Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add test sample with zone-testing #3701

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 42 additions & 1 deletion modules/core/internal/create/combineLatest-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { of } from "./of";
import { combineLatest } from "./combineLatest";
import { Observable } from "../Observable";

function createIntervalObservable(interval: number) {
return new Observable<number>(subscriber => {
let i = 0;
const id = setInterval(() => {
subscriber.next(i++);
}, interval);
return () => clearInterval(id);
});
}

// TODO: write a zone.js test to cover this with a timer
describe('combineLatest', () => {
it('should combine the latest values of multiple observables', () => {
const results: any[] = [];
Expand All @@ -14,5 +24,36 @@ describe('combineLatest', () => {
next(value) { results.push(value); },
complete() { results.push('done'); },
});
expect(results).toEqual([[1, 2, 3]]);
});

it('should combine the latest values of multiple observables with fakeAsync', fakeAsyncTest(() => {
const results: any[] = [];
const s1 = createIntervalObservable(1000);
const s2 = createIntervalObservable(1500);
const s3 = createIntervalObservable(1800);
combineLatest(s1, s2, s3).subscribe({
next(value) { results.push(value); },
complete() { results.push('done'); },
});
expect(results).toEqual([]);
tick(1000);
expect(results).toEqual([]);
tick(500);
expect(results).toEqual([]);
tick(300);
expect(results).toEqual([[0, 0, 0]]);
tick(300);
expect(results).toEqual([[0, 0, 0], [1, 0, 0]]);
clearAllMacrotasks();
}));

it('Date.now should be advanced by fakeAsyncTest', fakeAsyncTest(() => {
const start = Date.now();
tick(100);
const end = Date.now();
expect(end - start).toBe(100);
}));

// TODO: Add a marble test with zone-testing.
});
3 changes: 2 additions & 1 deletion modules/core/internal/create/combineLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export function combineLatestSource<T>(sources: ObservableInput<T>[]): Source<T>
src(FOType.SUBSCRIBE, (t: FOType, v: SinkArg<T>, subs: Subscription) => {
if(t === FOType.NEXT) {
values[s] = v;
emittedOnce[s] = true;
if (hasValues || (hasValues = emittedOnce.every(identity))) {
sink(FOType.NEXT, values.slice(0), subs);
sink(FOType.NEXT, values.slice(), subs);
}
}
}, subs);
Expand Down
10 changes: 4 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
"watch": "Watch codebase, trigger compile when source code changes"
},
"scripts": {
"test": "mocha -r ts-node/register modules/**/*-spec.ts",
"test": "npm run prepare-zone && mocha -r ts-node/register -r ./node_modules/zone.js/dist/zone-testing-node-bundle modules/**/*-spec.ts",
"watch": "watch 'npm run test' modules",
"prepare-zone": "mkdir -p ./node_modules/@types/zone.js && cp ./node_modules/zone.js/lib/testing/zone-testing.typing.ts ./node_modules/@types/zone.js/index.d.ts",
"build": "tsc"
},
"repository": {
Expand Down Expand Up @@ -55,7 +56,6 @@
},
"devDependencies": {
"@types/chai": "^4.1.2",
"@types/mocha": "^5.0.0",
"chai": "^4.1.2",
"commitizen": "^2.9.6",
"cz-conventional-changelog": "^2.1.0",
Expand All @@ -64,6 +64,7 @@
"ts-node": "^5.0.1",
"tslint": "^5.9.1",
"typescript": "^2.8.1",
"watch": "^1.0.2"
"watch": "^1.0.2",
"zone.js": "github:JiaLiPassion/zone.js#rxjs-test-dist"
}
}