-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng-dev):
AngularContext
supports component harnesses with fake…
…Async BREAKING CHANGE: requires @angular/cdk as a peer dependency
- Loading branch information
Showing
10 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
projects/ng-dev/src/lib/test-context/fake-async-harness-environment.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { ContentContainerComponentHarness } from '@angular/cdk/testing'; | ||
import { Component } from '@angular/core'; | ||
import { MatButtonHarness } from '@angular/material/button/testing'; | ||
import { ComponentContext } from './component-context'; | ||
import { Synchronized } from './synchronize'; | ||
|
||
@Component({ | ||
selector: 's-test-component', | ||
template: ` | ||
<div class="wrapper"> | ||
<button mat-button (click)="clicked = true">{{ clicked }}</button> | ||
</div> | ||
`, | ||
}) | ||
class TestComponent { | ||
clicked = false; | ||
} | ||
|
||
class WrapperHarness extends ContentContainerComponentHarness { | ||
static hostSelector = '.wrapper'; | ||
} | ||
|
||
class TestContext extends ComponentContext { | ||
protected componentType = TestComponent; | ||
} | ||
|
||
describe('FakeAsyncHarnessEnvironment', () => { | ||
let ctx: TestContext; | ||
beforeEach(() => { | ||
ctx = new TestContext(); | ||
}); | ||
|
||
it('runs asynchronous events that are due automatically', () => { | ||
ctx.run(() => { | ||
const button = ctx.getHarness(MatButtonHarness); | ||
button.click(); | ||
expect(button.getText()).toBe('true'); | ||
}); | ||
}); | ||
|
||
it('locates sub harnesses that are also synchronized', () => { | ||
ctx.run(() => { | ||
const button = ctx | ||
.getHarness(WrapperHarness) | ||
.getHarness(MatButtonHarness) as Synchronized<MatButtonHarness>; | ||
button.click(); | ||
expect(button.getText()).toBe('true'); | ||
}); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
projects/ng-dev/src/lib/test-context/fake-async-harness-environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { HarnessEnvironment } from '@angular/cdk/testing'; | ||
import { UnitTestElement } from '@angular/cdk/testing/testbed'; | ||
import { flush } from '@angular/core/testing'; | ||
import { bindKey } from '@s-libs/micro-dash'; | ||
import { AngularContext } from './angular-context'; | ||
import { synchronize, Synchronized } from './synchronize'; | ||
|
||
/** @hidden */ | ||
export class FakeAsyncHarnessEnvironment extends HarnessEnvironment<Element> { | ||
static documentRootLoader( | ||
ctx: AngularContext, | ||
): Synchronized<FakeAsyncHarnessEnvironment> { | ||
return synchronize(new FakeAsyncHarnessEnvironment(document.body, ctx)); | ||
} | ||
|
||
protected constructor(rawRootElement: Element, private ctx: AngularContext) { | ||
super(rawRootElement); | ||
} | ||
|
||
async waitForTasksOutsideAngular(): Promise<void> { | ||
flush(); | ||
} | ||
|
||
async forceStabilize(): Promise<void> { | ||
this.ctx.tick(); | ||
} | ||
|
||
protected createEnvironment(element: Element): HarnessEnvironment<Element> { | ||
return new FakeAsyncHarnessEnvironment(element, this.ctx); | ||
} | ||
|
||
protected createTestElement(element: Element): UnitTestElement { | ||
return new UnitTestElement(element, bindKey(this, 'forceStabilize')); | ||
} | ||
|
||
protected async getAllRawElements(selector: string): Promise<Element[]> { | ||
return Array.from(this.rawRootElement.querySelectorAll(selector)); | ||
} | ||
|
||
protected getDocumentRoot(): Element { | ||
return document.body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { AngularContext } from './angular-context'; | ||
export { ComponentContext } from './component-context'; | ||
export { Synchronized } from './synchronize'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { tick } from '@angular/core/testing'; | ||
import { isPromiseLike } from '@s-libs/js-core'; | ||
import { get, isObject } from '@s-libs/micro-dash'; | ||
|
||
/** | ||
* The type for component harness used in an `AngularContext`, which wraps the original harness in a proxy meant to be used in a `fakeAsync` test where one cannot `await` the results of an asynchronous call. The functions that would normally return a promise instead return their result immediately, calling `tick()` to flush it first. | ||
*/ | ||
export type Synchronized<O extends object> = { | ||
[K in keyof O]: O[K] extends (...args: any[]) => any | ||
? SynchronizedFunction<O[K]> | ||
: O[K]; | ||
}; | ||
|
||
/** @hidden */ | ||
type SynchronizedFunction<F extends (...args: any[]) => any> = ( | ||
...args: Parameters<F> | ||
) => SynchronizedResult<ReturnType<F>>; | ||
/** @hidden */ | ||
type SynchronizedResult<V> = V extends PromiseLike<any> | ||
? SynchronizedPromise<V> | ||
: V extends object | ||
? Synchronized<V> | ||
: V; | ||
/** @hidden */ | ||
type SynchronizedPromise<P> = P extends PromiseLike<infer R> | ||
? R extends object | ||
? Synchronized<R> | ||
: R | ||
: 'assertion error'; | ||
|
||
/** @hidden */ | ||
const proxyTarget = Symbol('proxy target'); // trick from https://stackoverflow.com/a/53431924/1836506 | ||
/** @hidden */ | ||
export function synchronize<T extends object>(obj: T): Synchronized<T> { | ||
return new Proxy(obj, { | ||
get(target, p, receiver): any { | ||
if (p === proxyTarget) { | ||
return obj; | ||
} else { | ||
return synchronizeAny(Reflect.get(target, p, receiver)); | ||
} | ||
}, | ||
apply(target, thisArg, argArray): any { | ||
thisArg = get(thisArg, [proxyTarget], thisArg); | ||
return synchronizeAny( | ||
Reflect.apply(target as Function, thisArg, argArray), | ||
); | ||
}, | ||
}) as Synchronized<T>; | ||
} | ||
|
||
/** @hidden */ | ||
function synchronizeAny(value: any): any { | ||
if (isPromiseLike(value)) { | ||
return synchronizePromise(value); | ||
} else if (isObject(value)) { | ||
return synchronize(value); | ||
} else { | ||
return value; | ||
} | ||
} | ||
|
||
/** @hidden */ | ||
function synchronizePromise<T extends PromiseLike<any>>(promise: T): any { | ||
let awaited: any; | ||
promise.then((value) => (awaited = value)); | ||
tick(); | ||
// TODO: a nice error if the promise didn't resolve | ||
return synchronizeAny(awaited); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters