Skip to content

Commit 8ce5b10

Browse files
docs: add example to inject a token (#53)
1 parent ced7046 commit 8ce5b10

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { render } from '@testing-library/angular';
2+
3+
import { DataInjectedComponent, DATA } from './10-inject-token-dependency';
4+
5+
test('injects data into the component', async () => {
6+
const component = await render(DataInjectedComponent, {
7+
providers: [
8+
{
9+
provide: DATA,
10+
useValue: { text: 'Hello boys and girls' },
11+
},
12+
],
13+
});
14+
15+
expect(component.getByText(/Hello boys and girls/i)).toBeInTheDocument();
16+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Component, InjectionToken, Inject } from '@angular/core';
2+
3+
export const DATA = new InjectionToken<{ text: string }>('Components Data');
4+
5+
@Component({
6+
selector: 'app-fixture',
7+
template: `
8+
{{ data.text }}
9+
`,
10+
})
11+
export class DataInjectedComponent {
12+
constructor(@Inject(DATA) public data: { text: string }) {}
13+
}

0 commit comments

Comments
 (0)