-
-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spectator): improve type inference even more
- Loading branch information
Showing
12 changed files
with
359 additions
and
26 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
/** | ||
* Type for classes that can be used as tokens for injection. We might be inclined to use Angular's `Type<T>` interface for this, however, | ||
* that interface cannot be used for abstract classes, which are also valid tokens. The `InjectableType<T>` supports both abstract and | ||
* concrete classes and thus is a more accurate definition of a class type which can be used for injection. | ||
*/ | ||
export type InjectableType<T> = Function & { prototype: T }; | ||
|
||
/** Type representing valid typesafe token types for provider binding. */ | ||
export type Token<T> = InjectableType<T> | InjectionToken<T>; |
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,153 @@ | ||
import { ConsumerService } from './consumer.service'; | ||
import { createHostComponentFactory, createService, createTestComponentFactory, Spectator } from '@netbasal/spectator/jest'; | ||
import { AbstractQueryService, QueryService } from './query.service'; | ||
import { InjectionToken } from '@angular/core'; | ||
import { ZippyComponent } from './zippy/zippy.component'; | ||
import { WidgetService } from './widget.service'; | ||
|
||
const MY_TOKEN = new InjectionToken<AbstractQueryService>('some-token'); | ||
|
||
describe('Injection tokens', () => { | ||
describe('with Spectator', () => { | ||
const createComponent = createTestComponentFactory({ | ||
component: ZippyComponent, | ||
mocks: [WidgetService], | ||
providers: [ | ||
QueryService, | ||
{ | ||
provide: AbstractQueryService, | ||
useExisting: QueryService | ||
}, | ||
{ | ||
provide: MY_TOKEN, | ||
useExisting: QueryService | ||
} | ||
] | ||
}); | ||
|
||
let spectator: Spectator<ZippyComponent>; | ||
|
||
beforeEach(() => (spectator = createComponent())); | ||
|
||
it('should get by concrete class', () => { | ||
const service = spectator.get(QueryService); | ||
service.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by abstract class as token', () => { | ||
const service = spectator.get(AbstractQueryService); | ||
service.select(); // should compile | ||
|
||
const service2 = spectator.get<QueryService>(AbstractQueryService); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by injection token', () => { | ||
const service = spectator.get(MY_TOKEN); | ||
service.select(); // should compile | ||
|
||
const service2 = spectator.get<QueryService>(MY_TOKEN); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
}); | ||
|
||
describe('with SpectatorWithHost', () => { | ||
const createHost = createHostComponentFactory({ | ||
component: ZippyComponent, | ||
mocks: [WidgetService], | ||
providers: [ | ||
QueryService, | ||
{ | ||
provide: AbstractQueryService, | ||
useExisting: QueryService | ||
}, | ||
{ | ||
provide: MY_TOKEN, | ||
useExisting: QueryService | ||
} | ||
] | ||
}); | ||
|
||
let host: Spectator<ZippyComponent>; | ||
|
||
beforeEach(() => (host = createHost('<zippy></zippy>'))); | ||
|
||
it('should get by concrete class', () => { | ||
const service = host.get(QueryService); | ||
service.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by abstract class as token', () => { | ||
const service = host.get(AbstractQueryService); | ||
service.select(); // should compile | ||
|
||
const service2 = host.get<QueryService>(AbstractQueryService); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by injection token', () => { | ||
const service = host.get(MY_TOKEN); | ||
service.select(); // should compile | ||
|
||
const service2 = host.get<QueryService>(MY_TOKEN); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
}); | ||
|
||
describe('with Service', () => { | ||
const spectator = createService({ | ||
service: ConsumerService, | ||
mocks: [WidgetService], | ||
providers: [ | ||
QueryService, | ||
{ | ||
provide: AbstractQueryService, | ||
useExisting: QueryService | ||
}, | ||
{ | ||
provide: MY_TOKEN, | ||
useExisting: QueryService | ||
} | ||
] | ||
}); | ||
|
||
it('should get by concrete class', () => { | ||
const service = spectator.get(QueryService); | ||
service.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by abstract class as token', () => { | ||
const service = spectator.get(AbstractQueryService); | ||
service.select(); // should compile | ||
|
||
const service2 = spectator.get<QueryService>(AbstractQueryService); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
|
||
it('should get by injection token', () => { | ||
const service = spectator.get(MY_TOKEN); | ||
service.select(); // should compile | ||
|
||
const service2 = spectator.get<QueryService>(MY_TOKEN); | ||
service2.selectName(); // should compile | ||
|
||
expect(service).toBeInstanceOf(QueryService); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.