Skip to content

Commit

Permalink
feat(ng-div): add AngularContext.isRunning()
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Aug 12, 2023
1 parent 81b4822 commit ce1a8bb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
11 changes: 11 additions & 0 deletions projects/ng-dev/src/lib/angular-context/angular-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,17 @@ describe('AngularContext', () => {
});
});

describe('.isRunning()', () => {
it('works', () => {
const ctx = new AngularContext();
expect(ctx.isRunning()).toBe(false);
ctx.run(() => {
expect(ctx.isRunning()).toBe(true);
});
expect(ctx.isRunning()).toBe(false);
});
});

describe('.inject()', () => {
it('fetches from the root injector', () => {
const ctx = new AngularContext();
Expand Down
14 changes: 11 additions & 3 deletions projects/ng-dev/src/lib/angular-context/angular-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export class AngularContext {
*/
startTime = new Date();

#isRunning = false;
#loader = FakeAsyncHarnessEnvironment.documentRootLoader(this);

/**
Expand Down Expand Up @@ -140,6 +141,7 @@ export class AngularContext {
*/
run(test: () => Promise<void> | void): void {
try {
this.#isRunning = true;
this.#runWithMockedTime(() => {
this.init();
try {
Expand All @@ -153,9 +155,17 @@ export class AngularContext {
});
} finally {
AngularContext.#current = undefined;
this.#isRunning = false;
}
}

/**
* Returns whether this context is currently executing the {@linkcode #run} callback.
*/
isRunning(): boolean {
return this.#isRunning;
}

/**
* Gets a service or other injectable from the root injector.
*
Expand Down Expand Up @@ -207,9 +217,7 @@ export class AngularContext {
* @param unit The unit of time `amount` represents. Accepts anything described in `@s-libs/s-core`'s [TimeUnit]{@linkcode https://simontonsoftware.github.io/s-js-utils/typedoc/enums/timeunit.html} enum.
*/
tick(amount = 0, unit = 'ms'): void {
if (
isUndefined((window as any).Zone.current.get('FakeAsyncTestZoneSpec'))
) {
if (!this.#isRunning) {
throw new Error(
'.tick() only works inside the .run() callback (because it needs to be in a fakeAsync zone)',
);
Expand Down

0 comments on commit ce1a8bb

Please sign in to comment.