-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make `ComponentTestBed` extend to `CommonTestBed` and update tests * Refactor * Refactor demo * Rename folder * Move shared.model.ts * Rename folders * Move resolver model * Add lib index * Add missing exports * Update `InjectionStore` import path * Rename `CommonTestBedFactory` to `CustomTestBedFactory`
- Loading branch information
Showing
81 changed files
with
200 additions
and
295 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
6 changes: 2 additions & 4 deletions
6
projects/ngx-testing-tools-demo/src/app/guards/load-lazy.guard.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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { CanMatchFn, Route, UrlSegment } from '@angular/router'; | ||
import { CanMatchFn } from '@angular/router'; | ||
import { of } from 'rxjs'; | ||
|
||
export function loadLazyGuard(): CanMatchFn { | ||
return (route: Route, segments: UrlSegment[]) => { | ||
return of(true); | ||
}; | ||
return () => of(true); | ||
} |
6 changes: 2 additions & 4 deletions
6
projects/ngx-testing-tools-demo/src/app/guards/login.guard.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 |
---|---|---|
@@ -1,8 +1,6 @@ | ||
import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from '@angular/router'; | ||
import { CanActivateFn } from '@angular/router'; | ||
import { of } from 'rxjs'; | ||
|
||
export function loginGuard(): CanActivateFn { | ||
return (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => { | ||
return of(true); | ||
}; | ||
return () => of(true); | ||
} |
11 changes: 2 additions & 9 deletions
11
projects/ngx-testing-tools-demo/src/app/guards/no-back.guard.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 |
---|---|---|
@@ -1,13 +1,6 @@ | ||
import { ActivatedRouteSnapshot, CanDeactivateFn, RouterStateSnapshot } from '@angular/router'; | ||
import { CanDeactivateFn } from '@angular/router'; | ||
import { of } from 'rxjs'; | ||
|
||
export function noBackGuard<T>(): CanDeactivateFn<T> { | ||
return ( | ||
component: T, | ||
currentRoute: ActivatedRouteSnapshot, | ||
currentState: RouterStateSnapshot, | ||
nextState: RouterStateSnapshot, | ||
) => { | ||
return of(true); | ||
}; | ||
return () => of(true); | ||
} |
6 changes: 0 additions & 6 deletions
6
projects/ngx-testing-tools-demo/src/app/tokens/window.token.ts
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/annotation.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
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/component-annotation.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
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/directive-annotation.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
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/injectable-annotation.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
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/ng-module-annotation.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
2 changes: 1 addition & 1 deletion
2
projects/ngx-testing-tools/src/lib/common/annotation/pipe-annotation.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
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
4 changes: 2 additions & 2 deletions
4
projects/ngx-testing-tools/src/lib/common/test-bed/merge-factory.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
9 changes: 0 additions & 9 deletions
9
projects/ngx-testing-tools/src/lib/common/test-bed/models/common-tools.model.ts
This file was deleted.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
projects/ngx-testing-tools/src/lib/common/test-bed/models/custom-test-bed.model.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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { PrettyMerge } from '../../../models/shared.model'; | ||
import { CommonTestBedFactory } from '../common-test-bed-factory'; | ||
import { PrettyMerge } from '../../../shared.model'; | ||
import { CustomTestBedFactory } from '../custom-test-bed-factory'; | ||
|
||
export type CustomTestBed< | ||
Fn extends (...args: any[]) => jasmine.ImplementationCallback, | ||
Factory extends CommonTestBedFactory<any> | ||
Factory extends CustomTestBedFactory<any> | ||
> = PrettyMerge<Fn & Factory> | ||
|
9 changes: 9 additions & 0 deletions
9
projects/ngx-testing-tools/src/lib/common/test-bed/models/custom-tools.model.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,9 @@ | ||
import { Injector } from '@angular/core'; | ||
import { InjectionStore } from '../store/models/injected-store.model'; | ||
|
||
export interface CustomTools<I extends {} = {}> extends InjectionStore<I> { | ||
/** | ||
* The root injector. | ||
*/ | ||
injector: Injector; | ||
} |
4 changes: 2 additions & 2 deletions
4
projects/ngx-testing-tools/src/lib/common/test-bed/models/enhanced-jasmine-callback.model.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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { CommonTools } from './common-tools.model'; | ||
import { CustomTools } from './custom-tools.model'; | ||
|
||
export type EnhancedJasmineCallback<Tools extends CommonTools> = (tools: Tools, done: DoneFn) => ReturnType<jasmine.ImplementationCallback> | ||
export type EnhancedJasmineCallback<Tools extends CustomTools> = (tools: Tools, done: DoneFn) => ReturnType<jasmine.ImplementationCallback> |
1 change: 0 additions & 1 deletion
1
projects/ngx-testing-tools/src/lib/common/test-bed/store/index.ts
This file was deleted.
Oops, something went wrong.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...g-tools/src/lib/components/event/event.ts β ...g-tools/src/lib/component/action/event.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
File renamed without changes.
File renamed without changes.
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,3 @@ | ||
export * from './query'; | ||
export * from './action'; | ||
export * from './test-bed'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
.../components/element/find-debug-element.ts β ...lib/component/query/find-debug-element.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
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions
9
projects/ngx-testing-tools/src/lib/component/test-bed/assertions/assert-component.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,9 @@ | ||
import { Component, Type } from '@angular/core'; | ||
import { getComponentAnnotation, isComponentAnnotation } from '../../../common/annotation/component-annotation'; | ||
import { Nullable } from '../../../shared.model'; | ||
|
||
export function assertComponent(ComponentCtor: Type<any>): void { | ||
const annotation: Nullable<Component> = getComponentAnnotation(ComponentCtor); | ||
if (!annotation || !isComponentAnnotation(annotation)) | ||
throw new Error(`The provided "${ComponentCtor.name ?? ComponentCtor}" is not a Component. The ComponentTestBed cannot be created.`); | ||
} |
2 changes: 1 addition & 1 deletion
2
...nts/test-bed/assertions/assert-fixture.ts β ...ent/test-bed/assertions/assert-fixture.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export function assertComponentFixture(fixture: unknown): void { | ||
if (!fixture) | ||
throw new Error('ComponentFixture is falsy. You need to use `testBed.compile()` before running expectation.'); | ||
throw new Error('ComponentFixture is falsy. You need to use `beforeEach(() => tb.compile());` of `tb.compileEach();` before running expectations.'); | ||
} |
2 changes: 1 addition & 1 deletion
2
...onents/test-bed/component-action-tools.ts β ...ponent/test-bed/component-action-tools.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
2 changes: 1 addition & 1 deletion
2
...ponents/test-bed/component-query-tools.ts β ...mponent/test-bed/component-query-tools.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
68 changes: 68 additions & 0 deletions
68
projects/ngx-testing-tools/src/lib/component/test-bed/component-test-bed-factory.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,68 @@ | ||
import { ProviderToken, Type } from '@angular/core'; | ||
import { ComponentFixture } from '@angular/core/testing'; | ||
import { getComponentAnnotation } from '../../common/annotation/component-annotation'; | ||
import { shouldCreate } from '../../common/expectation/should-create'; | ||
import { CustomTestBedFactory } from '../../common/test-bed/custom-test-bed-factory'; | ||
import { InjectionStore } from '../../common/test-bed/store/models/injected-store.model'; | ||
import { MaybeArray, NonEmptyString, PrettyMerge } from '../../shared.model'; | ||
import { makeArray } from '../../util/array.util'; | ||
import { assertComponent } from './assertions/assert-component'; | ||
import { assertComponentFixture } from './assertions/assert-fixture'; | ||
import { buildComponentTools } from './component-tools'; | ||
import { ComponentTestBed } from './models'; | ||
import { ComponentSetup } from './models/component-setup.model'; | ||
import { Declaration } from './models/metadata-type.model'; | ||
|
||
export class ComponentTestBedFactory<ComponentType, Store extends InjectionStore = InjectionStore> extends CustomTestBedFactory<ComponentType, Store> { | ||
|
||
public constructor(rootComponent: Type<ComponentType>) { | ||
assertComponent(rootComponent); | ||
super(rootComponent); | ||
(getComponentAnnotation(rootComponent)?.standalone) | ||
? this.import(this.described) | ||
: this.declare(this.described); | ||
} | ||
|
||
private fixture: ComponentFixture<ComponentType> = null!; | ||
|
||
private declarations: Set<Declaration> = new Set(); | ||
|
||
/** | ||
* Declares one non-standalone component, directive or pipe into the `ComponentTestBed`. | ||
*/ | ||
public declare(declaration: Declaration): this | ||
/** | ||
* Declares many non-standalone components, directives and pipes into `ComponentTestBed`. | ||
*/ | ||
public declare(declarations: Declaration[]): this | ||
public declare(oneOrManyDeclarations: MaybeArray<Declaration>): this { | ||
makeArray(oneOrManyDeclarations).forEach(v => this.declarations.add(v)); | ||
return this; | ||
} | ||
|
||
public override inject<S extends string, T>(name: NonEmptyString<S>, token: ProviderToken<T>): ComponentTestBed<ComponentType, InjectionStore<PrettyMerge<Store['injected'] & { [K in S]: T }>>> { | ||
return super.inject(name, token) as any; | ||
} | ||
|
||
public override async compile(): Promise<void> { | ||
await super.compile(); | ||
this.testBed.configureTestingModule({ | ||
declarations: [...this.declarations.values()], | ||
}); | ||
await this.testBed.compileComponents(); | ||
this.fixture = this.testBed.createComponent(this.described); | ||
} | ||
|
||
public override setup(action: ComponentSetup<ComponentType, Store['injected']>): jasmine.ImplementationCallback { | ||
return (action.length > 1) | ||
? (done: DoneFn) => action(buildComponentTools(this), done) | ||
: () => action(buildComponentTools(this), null!); | ||
} | ||
|
||
public override shouldCreate(): void { | ||
shouldCreate(() => { | ||
assertComponentFixture(this.fixture); | ||
return this.fixture.componentInstance; | ||
}); | ||
} | ||
} |
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
...ib/components/test-bed/component-tools.ts β ...lib/component/test-bed/component-tools.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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.