Skip to content

Commit a99f30c

Browse files
authored
use standalone for components
1 parent 244906c commit a99f30c

37 files changed

+91
-383
lines changed

apps/example-app/src/app/app.module.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ function reducerItems() {
2828
}
2929

3030
@NgModule({
31-
declarations: [
32-
AppComponent,
31+
declarations: [AppComponent],
32+
imports: [
3333
SingleComponent,
3434
NestedButtonComponent,
3535
NestedValueComponent,
@@ -43,8 +43,6 @@ function reducerItems() {
4343
RootComponent,
4444
DetailComponent,
4545
HiddenDetailComponent,
46-
],
47-
imports: [
4846
BrowserModule,
4947
ReactiveFormsModule,
5048
BrowserAnimationsModule,

apps/example-app/src/app/examples/00-single-component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Component } from '@angular/core';
22

33
@Component({
44
selector: 'app-fixture',
5+
standalone: true,
56
template: `
67
<button (click)="value = value - 1">Decrement</button>
78
<span data-testid="value">{{ value }}</span>

apps/example-app/src/app/examples/01-nested-component.spec.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { render, screen } from '@testing-library/angular';
22
import userEvent from '@testing-library/user-event';
33

4-
import { NestedButtonComponent, NestedValueComponent, NestedContainerComponent } from './01-nested-component';
4+
import { NestedContainerComponent } from './01-nested-component';
55

66
test('renders the current value and can increment and decrement', async () => {
77
const user = userEvent.setup();
8-
await render(NestedContainerComponent, {
9-
declarations: [NestedButtonComponent, NestedValueComponent],
10-
});
8+
await render(NestedContainerComponent);
119

1210
const incrementControl = screen.getByRole('button', { name: /increment/i });
1311
const decrementControl = screen.getByRole('button', { name: /decrement/i });

apps/example-app/src/app/examples/01-nested-component.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, Input, Output, EventEmitter } from '@angular/core';
22

33
@Component({
4+
standalone: true,
45
selector: 'app-button',
56
template: ' <button (click)="raise.emit()">{{ name }}</button> ',
67
})
@@ -10,6 +11,7 @@ export class NestedButtonComponent {
1011
}
1112

1213
@Component({
14+
standalone: true,
1315
selector: 'app-value',
1416
template: ' <span data-testid="value">{{ value }}</span> ',
1517
})
@@ -18,12 +20,14 @@ export class NestedValueComponent {
1820
}
1921

2022
@Component({
23+
standalone: true,
2124
selector: 'app-fixture',
2225
template: `
23-
<app-button (raise)="value = value - 1" name="Decrement"></app-button>
24-
<app-value [value]="value"></app-value>
25-
<app-button (raise)="value = value + 1" name="Increment"></app-button>
26+
<app-button (raise)="value = value - 1" name="Decrement" />
27+
<app-value [value]="value" />
28+
<app-button (raise)="value = value + 1" name="Increment" />
2629
`,
30+
imports: [NestedButtonComponent, NestedValueComponent],
2731
})
2832
export class NestedContainerComponent {
2933
value = 0;

apps/example-app/src/app/examples/02-input-output.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ test('is possible to set input and listen for output with the template syntax',
3838
const user = userEvent.setup();
3939
const sendSpy = jest.fn();
4040

41-
await render('<app-fixture [value]="47" (sendValue)="sendValue($event)" (clicked)="clicked()"></app-fixture>', {
42-
declarations: [InputOutputComponent],
41+
await render('<app-fixture [value]="47" (sendValue)="sendValue($event)" />', {
42+
imports: [InputOutputComponent],
4343
componentProperties: {
4444
sendValue: sendSpy,
4545
},

apps/example-app/src/app/examples/02-input-output.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, EventEmitter, Input, Output } from '@angular/core';
22

33
@Component({
4+
standalone: true,
45
selector: 'app-fixture',
56
template: `
67
<button (click)="value = value - 1">Decrement</button>

apps/example-app/src/app/examples/03-forms.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { NgForOf, NgIf } from '@angular/common';
12
import { Component } from '@angular/core';
2-
import { FormBuilder, Validators } from '@angular/forms';
3+
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
34

45
@Component({
6+
standalone: true,
57
selector: 'app-fixture',
8+
imports: [ReactiveFormsModule, NgForOf, NgIf],
69
template: `
710
<form [formGroup]="form" name="form">
811
<div>

apps/example-app/src/app/examples/04-forms-with-material.spec.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { render, screen } from '@testing-library/angular';
22
import userEvent from '@testing-library/user-event';
33

4-
import { MaterialModule } from '../material.module';
54
import { MaterialFormsComponent } from './04-forms-with-material';
65

76
test('is possible to fill in a form and verify error messages (with the help of jest-dom https://testing-library.com/docs/ecosystem-jest-dom)', async () => {
87
const user = userEvent.setup();
98

10-
const { fixture } = await render(MaterialFormsComponent, {
11-
imports: [MaterialModule],
12-
});
9+
const { fixture } = await render(MaterialFormsComponent);
1310

1411
const nameControl = screen.getByLabelText(/name/i);
1512
const scoreControl = screen.getByRole('spinbutton', { name: /score/i });
@@ -69,9 +66,7 @@ test('is possible to fill in a form and verify error messages (with the help of
6966
test('set and show pre-set form values', async () => {
7067
const user = userEvent.setup();
7168

72-
const { fixture, detectChanges } = await render(MaterialFormsComponent, {
73-
imports: [MaterialModule],
74-
});
69+
const { fixture, detectChanges } = await render(MaterialFormsComponent);
7570

7671
fixture.componentInstance.form.setValue({
7772
name: 'Max',

apps/example-app/src/app/examples/04-forms-with-material.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { Component } from '@angular/core';
2-
import { FormBuilder, Validators } from '@angular/forms';
2+
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
3+
import { MaterialModule } from '../material.module';
4+
import { NgForOf, NgIf } from '@angular/common';
35

46
@Component({
7+
standalone: true,
8+
imports: [MaterialModule, ReactiveFormsModule, NgForOf, NgIf],
59
selector: 'app-fixture',
610
template: `
711
<form [formGroup]="form" name="form">

apps/example-app/src/app/examples/05-component-provider.ts

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class CounterService {
2020
}
2121

2222
@Component({
23+
standalone: true,
2324
selector: 'app-fixture',
2425
template: `
2526
<button (click)="counter.decrement()">Decrement</button>

apps/example-app/src/app/examples/06-with-ngrx-store.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsyncPipe } from '@angular/common';
12
import { Component } from '@angular/core';
23
import { createSelector, Store, createAction, createReducer, on, select } from '@ngrx/store';
34

@@ -15,6 +16,8 @@ const selectValue = createSelector(
1516
);
1617

1718
@Component({
19+
standalone: true,
20+
imports: [AsyncPipe],
1821
selector: 'app-fixture',
1922
template: `
2023
<button (click)="decrement()">Decrement</button>

apps/example-app/src/app/examples/07-with-ngrx-mock-store.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsyncPipe, NgForOf } from '@angular/common';
12
import { Component } from '@angular/core';
23
import { createSelector, Store, select } from '@ngrx/store';
34

@@ -7,6 +8,8 @@ export const selectItems = createSelector(
78
);
89

910
@Component({
11+
standalone: true,
12+
imports: [AsyncPipe, NgForOf],
1013
selector: 'app-fixture',
1114
template: `
1215
<ul>

apps/example-app/src/app/examples/08-directive.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('it is possible to test directives', async () => {
77
const user = userEvent.setup();
88

99
await render('<div appSpoiler data-testid="dir"></div>', {
10-
declarations: [SpoilerDirective],
10+
imports: [SpoilerDirective],
1111
});
1212

1313
const directive = screen.getByTestId('dir');
@@ -30,7 +30,7 @@ test('it is possible to test directives with props', async () => {
3030
const visible = 'There is nothing to see here ...';
3131

3232
await render('<div appSpoiler [hidden]="hidden" [visible]="visible"></div>', {
33-
declarations: [SpoilerDirective],
33+
imports: [SpoilerDirective],
3434
componentProperties: {
3535
hidden,
3636
visible,
@@ -55,7 +55,7 @@ test('it is possible to test directives with props in template', async () => {
5555
const visible = 'There is nothing to see here ...';
5656

5757
await render(`<div appSpoiler hidden="${hidden}" visible="${visible}"></div>`, {
58-
declarations: [SpoilerDirective],
58+
imports: [SpoilerDirective],
5959
});
6060

6161
expect(screen.queryByText(visible)).not.toBeInTheDocument();

apps/example-app/src/app/examples/08-directive.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Directive, HostListener, ElementRef, Input, OnInit } from '@angular/core';
22

33
@Directive({
4+
standalone: true,
45
selector: '[appSpoiler]',
56
})
67
export class SpoilerDirective implements OnInit {

apps/example-app/src/app/examples/09-router.spec.ts

-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { DetailComponent, RootComponent, HiddenDetailComponent } from './09-rout
66
test('it can navigate to routes', async () => {
77
const user = userEvent.setup();
88
await render(RootComponent, {
9-
declarations: [DetailComponent, HiddenDetailComponent],
109
routes: [
1110
{
1211
path: '',
@@ -45,7 +44,6 @@ test('it can navigate to routes', async () => {
4544

4645
test('it can navigate to routes - workaround', async () => {
4746
const { navigate } = await render(RootComponent, {
48-
declarations: [DetailComponent, HiddenDetailComponent],
4947
routes: [
5048
{
5149
path: '',
@@ -84,7 +82,6 @@ test('it can navigate to routes - workaround', async () => {
8482
test('it can navigate to routes with a base path', async () => {
8583
const basePath = 'base';
8684
const { navigate } = await render(RootComponent, {
87-
declarations: [DetailComponent, HiddenDetailComponent],
8885
routes: [
8986
{
9087
path: basePath,

apps/example-app/src/app/examples/09-router.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
1+
import { AsyncPipe } from '@angular/common';
12
import { Component } from '@angular/core';
2-
import { ActivatedRoute } from '@angular/router';
3+
import { ActivatedRoute, RouterLink, RouterOutlet } from '@angular/router';
34
import { map } from 'rxjs/operators';
45

56
@Component({
7+
standalone: true,
8+
imports: [RouterLink, RouterOutlet],
69
selector: 'app-main',
710
template: `
8-
<a [routerLink]="'./detail/one'">Load one</a> | <a [routerLink]="'./detail/two'">Load two</a> |
9-
<a [routerLink]="'./detail/three'">Load three</a> |
11+
<a routerLink="./detail/one">Load one</a> | <a routerLink="./detail/two">Load two</a> |
12+
<a routerLink="./detail/three">Load three</a> |
1013
1114
<hr />
1215
13-
<router-outlet></router-outlet>
16+
<router-outlet />
1417
`,
1518
})
1619
export class RootComponent {}
1720

1821
@Component({
22+
standalone: true,
23+
imports: [RouterLink, AsyncPipe],
1924
selector: 'app-detail',
2025
template: `
2126
<h2>Detail {{ id | async }}</h2>
2227
2328
<p>{{ text | async }} {{ subtext | async }}</p>
2429
25-
<a [routerLink]="'../..'">Back to parent</a>
30+
<a routerLink="../..">Back to parent</a>
2631
<a routerLink="/hidden-detail">hidden x</a>
2732
`,
2833
})
@@ -34,6 +39,7 @@ export class DetailComponent {
3439
}
3540

3641
@Component({
42+
standalone: true,
3743
selector: 'app-detail-hidden',
3844
template: ' You found the treasure! ',
3945
})

apps/example-app/src/app/examples/10-inject-token-dependency.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Component, InjectionToken, Inject } from '@angular/core';
33
export const DATA = new InjectionToken<{ text: string }>('Components Data');
44

55
@Component({
6+
standalone: true,
67
selector: 'app-fixture',
78
template: ' {{ data.text }} ',
89
})

apps/example-app/src/app/examples/11-ng-content.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { CellComponent } from './11-ng-content';
44

55
test('it is possible to test ng-content without selector', async () => {
66
const projection = 'it should be showed into a p element!';
7-
7+
88
await render(`<app-fixture data-testid="one-cell-with-ng-content">${projection}</app-fixture>`, {
9-
declarations: [CellComponent]
9+
imports: [CellComponent],
1010
});
1111

1212
expect(screen.getByText(projection)).toBeInTheDocument();

apps/example-app/src/app/examples/11-ng-content.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, ChangeDetectionStrategy } from '@angular/core';
22

33
@Component({
4+
standalone: true,
45
selector: 'app-fixture',
56
template: `
67
<p>

apps/example-app/src/app/examples/12-service-component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AsyncPipe, NgForOf } from '@angular/common';
12
import { Component, Injectable } from '@angular/core';
23
import { Observable, of } from 'rxjs';
34

@@ -16,6 +17,8 @@ export class CustomersService {
1617
}
1718

1819
@Component({
20+
standalone: true,
21+
imports: [AsyncPipe, NgForOf],
1922
selector: 'app-fixture',
2023
template: `
2124
<ul>

apps/example-app/src/app/examples/13-scrolling.component.spec.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { render, screen, waitForElementToBeRemoved } from '@testing-library/angular';
22

33
import { CdkVirtualScrollOverviewExampleComponent } from './13-scrolling.component';
4-
import { ScrollingModule } from '@angular/cdk/scrolling';
54

65
test('should scroll to load more items', async () => {
7-
await render(CdkVirtualScrollOverviewExampleComponent, {
8-
imports: [ScrollingModule],
9-
});
6+
await render(CdkVirtualScrollOverviewExampleComponent);
107

118
const item0 = await screen.findByText(/Item #0/i);
129
expect(item0).toBeVisible();

apps/example-app/src/app/examples/13-scrolling.component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { ChangeDetectionStrategy, Component } from '@angular/core';
2+
import { ScrollingModule } from '@angular/cdk/scrolling';
23

34
@Component({
5+
standalone: true,
6+
imports: [ScrollingModule],
47
selector: 'app-cdk-virtual-scroll-overview-example',
58
template: `
69
<cdk-virtual-scroll-viewport itemSize="50" class="example-viewport" data-testid="scroll-viewport">

apps/example-app/src/app/examples/14-async-component.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { AsyncPipe, NgIf } from '@angular/common';
12
import { Component, OnDestroy } from '@angular/core';
23
import { Subject } from 'rxjs';
34
import { delay, filter, mapTo } from 'rxjs/operators';
45

56
@Component({
7+
standalone: true,
8+
imports: [AsyncPipe, NgIf],
69
selector: 'app-fixture',
710
template: `
811
<button (click)="load()">Load</button>

0 commit comments

Comments
 (0)