Skip to content

Commit

Permalink
feat(ng-dev): AngularContext mocks performance.now
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Nov 13, 2020
1 parent 7f29e68 commit 0ae92d6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion projects/ng-dev/README.md
Expand Up @@ -10,7 +10,7 @@ yarn add -D @s-libs/ng-dev @s-libs/ng-core @s-libs/rxjs-core @s-libs/js-core @s-

## TSLint Config

This library comes with a predefined `tslint.json` that track's the angular cli's config with these changes that we have found useful:
This library comes with a predefined `tslint.json` that tracks the angular cli's config with these changes that we have found useful:

- Disables rules that conflict with Prettier (via [tslint-config-prettier](https://github.com/prettier/tslint-config-prettier))
- Allows using the `Function` type. Some of our libraries deal a lot with utilities that operate on functions, and using this type is very handy.
Expand Down
8 changes: 8 additions & 0 deletions projects/ng-dev/src/lib/test-context/angular-context.spec.ts
Expand Up @@ -201,6 +201,14 @@ describe('AngularContext', () => {
expect(promiseResolvedBeforeChangeDetection).toBe(true);
});
});

it('advances `performance.now()` as well', () => {
ctx.run(() => {
const start = performance.now();
ctx.tick(10);
expect(performance.now()).toBe(start + 10);
});
});
});

describe('.init()', () => {
Expand Down
22 changes: 17 additions & 5 deletions projects/ng-dev/src/lib/test-context/angular-context.ts
Expand Up @@ -128,9 +128,7 @@ export class AngularContext<InitOptions = {}> {
options = optionsOrTest;
}

jasmine.clock().install();
fakeAsync(() => {
jasmine.clock().mockDate(this.startTime);
this.runWithMockedTime(() => {
assert(test);

this.init(options);
Expand All @@ -140,8 +138,7 @@ export class AngularContext<InitOptions = {}> {
} finally {
this.cleanUp();
}
})();
jasmine.clock().uninstall();
});
}

/**
Expand Down Expand Up @@ -216,4 +213,19 @@ export class AngularContext<InitOptions = {}> {
protected cleanUp(): void {
discardPeriodicTasks();
}

private runWithMockedTime(test: VoidFunction): void {
// https://github.com/angular/angular/issues/31677#issuecomment-573139551
const now = performance.now;
spyOn(performance, 'now').and.callFake(() => Date.now());

jasmine.clock().install();
fakeAsync(() => {
jasmine.clock().mockDate(this.startTime);
test();
})();
jasmine.clock().uninstall();

performance.now = now;
}
}
2 changes: 1 addition & 1 deletion projects/ng-dev/src/lib/test-context/index.ts
@@ -1,3 +1,3 @@
export { AngularContext } from './angular-context';
export { ComponentContext } from './component-context';
export { ComponentContext, ComponentContextInit } from './component-context';
export { Synchronized } from './synchronize';

0 comments on commit 0ae92d6

Please sign in to comment.