Skip to content

Commit 5cb6ad0

Browse files
committed
test: add unit tests for example
Add unit tests for the InjectionToken example in packages/examples/injection-token/src/main.ts. Tests cover basic token injection, error cases, factory providers, and TestBed integration. Fixes #TODO
1 parent bfc2afd commit 5cb6ad0

4 files changed

Lines changed: 115 additions & 4 deletions

File tree

adev/shared-docs/components/viewers/docs-viewer/docs-viewer.component.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import {ComponentFixture, TestBed} from '@angular/core/testing';
9+
import {TestBed} from '@angular/core/testing';
1010
import {By} from '@angular/platform-browser';
1111
import {provideRouter} from '@angular/router';
1212
import {ExampleViewerContentLoader} from '../../../interfaces';

packages/examples/injection-token/src/main.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
// TODO: Add unit tests for this file.
109
/* eslint-disable @angular-eslint/no-output-native */
1110
// #docregion
1211
import {Injector, InjectionToken} from '@angular/core';
1312

14-
interface MyInterface {
13+
export interface MyInterface {
1514
someProperty: string;
1615
}
1716

1817
// #docregion InjectionToken
1918

20-
const TOKEN = new InjectionToken<MyInterface>('SomeToken');
19+
export const TOKEN = new InjectionToken<MyInterface>('SomeToken');
2120

2221
// Setting up the provider using the same token instance
2322
const providers = [
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
load("//tools:defaults.bzl", "angular_jasmine_test", "ng_web_test_suite", "ts_project")
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
ts_project(
6+
name = "test_lib",
7+
testonly = True,
8+
srcs = glob(["**/*.spec.ts"]),
9+
deps = [
10+
"//packages/core",
11+
"//packages/core/testing",
12+
],
13+
)
14+
15+
angular_jasmine_test(
16+
name = "test",
17+
data = [
18+
":test_lib",
19+
],
20+
)
21+
22+
ng_web_test_suite(
23+
name = "test_web",
24+
deps = [
25+
":test_lib",
26+
],
27+
)
28+
29+
filegroup(
30+
name = "files_for_docgen",
31+
srcs = glob([
32+
"**/*.ts",
33+
]),
34+
)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.dev/license
7+
*/
8+
9+
import {TestBed} from '@angular/core/testing';
10+
import {InjectionToken, Injector} from '@angular/core';
11+
import {MyInterface, TOKEN} from '../main';
12+
13+
describe('InjectionToken Example', () => {
14+
it('should create and inject token with correct type', () => {
15+
const providers = [{provide: TOKEN, useValue: {someProperty: 'exampleValue'}}];
16+
const injector = Injector.create({providers});
17+
const myInterface = injector.get(TOKEN);
18+
19+
expect(myInterface).toBeDefined();
20+
expect(myInterface.someProperty).toBe('exampleValue');
21+
expect(myInterface.someProperty).toEqual(jasmine.any(String));
22+
});
23+
24+
it('should throw error when token is not provided', () => {
25+
const injector = Injector.create({providers: []});
26+
27+
expect(() => {
28+
injector.get(TOKEN);
29+
}).toThrowError(/NG0201.*No provider found for.*InjectionToken SomeToken/);
30+
});
31+
32+
it('should return null when token is optional and not provided', () => {
33+
const injector = Injector.create({providers: []});
34+
35+
const result = injector.get(TOKEN, null);
36+
expect(result).toBeNull();
37+
});
38+
39+
it('should work with multiple providers', () => {
40+
const TOKEN1 = new InjectionToken<string>('Token1');
41+
const TOKEN2 = new InjectionToken<number>('Token2');
42+
43+
const providers = [
44+
{provide: TOKEN1, useValue: 'string value'},
45+
{provide: TOKEN2, useValue: 42},
46+
];
47+
48+
const injector = Injector.create({providers});
49+
50+
expect(injector.get(TOKEN1)).toBe('string value');
51+
expect(injector.get(TOKEN2)).toBe(42);
52+
});
53+
54+
it('should use factory provider', () => {
55+
const FACTORY_TOKEN = new InjectionToken<MyInterface>('FactoryToken');
56+
57+
const providers = [
58+
{
59+
provide: FACTORY_TOKEN,
60+
useFactory: () => ({someProperty: 'from factory'}),
61+
},
62+
];
63+
64+
const injector = Injector.create({providers});
65+
const result = injector.get(FACTORY_TOKEN);
66+
67+
expect(result.someProperty).toBe('from factory');
68+
});
69+
70+
it('should work with TestBed', () => {
71+
TestBed.configureTestingModule({
72+
providers: [{provide: TOKEN, useValue: {someProperty: 'testBed value'}}],
73+
});
74+
75+
const result = TestBed.inject(TOKEN);
76+
expect(result.someProperty).toBe('testBed value');
77+
});
78+
});

0 commit comments

Comments
 (0)