diff --git a/adev-ja/src/app/features/update/update.component.en.html b/adev-ja/src/app/features/update/update.component.en.html index 13d9667e58..5ebb3dcbb8 100644 --- a/adev-ja/src/app/features/update/update.component.en.html +++ b/adev-ja/src/app/features/update/update.component.en.html @@ -19,7 +19,7 @@
Warning: @@ -86,7 +86,7 @@
警告: @@ -86,7 +86,7 @@
-HELPFUL: The `` case binds the `HighlightDirective` to the name of a color value in the input box.
+HELPFUL: The `` case binds the `Highlight` to the name of a color value in the input box.
The initial value is the word "cyan" which should be the background color of the input box.
Here are some tests of this component:
-
-HELPFUL: ``のケースでは、`HighlightDirective`を、入力ボックス内の色の値の名前とバインドしています。
+HELPFUL: ``のケースでは、`Highlight`を、入力ボックス内の色の値の名前とバインドしています。
初期値は単語"cyan"であり、これは入力ボックスの背景色になります。
このコンポーネントのテストをいくつか紹介します。
- with "banner works!"', () => {
+ const bannerElement: HTMLElement = fixture.nativeElement;
+ const p = bannerElement.querySelector('p')!;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
### `DebugElement`
The Angular _fixture_ provides the component's element directly through the `fixture.nativeElement`.
-
with fixture.debugElement.nativeElement', () => {
+ const bannerDe: DebugElement = fixture.debugElement;
+ const bannerEl: HTMLElement = bannerDe.nativeElement;
+ const p = bannerEl.querySelector('p')!;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
The `DebugElement` has other methods and properties that are useful in tests, as you'll see elsewhere in this guide.
You import the `DebugElement` symbol from the Angular core library.
-
with fixture.debugElement.query(By.css)', () => {
+ const bannerDe: DebugElement = fixture.debugElement;
+ const paragraphDe = bannerDe.query(By.css('p'));
+ const p: HTMLElement = paragraphDe.nativeElement;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
Some noteworthy observations:
diff --git a/adev-ja/src/content/guide/testing/components-basics.md b/adev-ja/src/content/guide/testing/components-basics.md
index 833d620f05..550c14cf83 100644
--- a/adev-ja/src/content/guide/testing/components-basics.md
+++ b/adev-ja/src/content/guide/testing/components-basics.md
@@ -17,9 +17,9 @@ Angularの `TestBed` は、次のセクションで説明するように、こ
- `Lightswitch.clicked()` はユーザーが呼び出せるように何かとバインドされていますか?
- `Lightswitch.message` は表示されますか?
-- ユーザーは `DashboardHeroComponent` によって表示されるヒーローを実際に選択できますか?
+- ユーザーは `DashboardHero` コンポーネントによって表示されるヒーローを実際に選択できますか?
- ヒーローの名前は期待通り(たとえば、大文字)に表示されますか?
-- `WelcomeComponent` のテンプレートによってウェルカムメッセージは表示されますか?
+- `Welcome` コンポーネントのテンプレートによってウェルカムメッセージは表示されますか?
これらの質問は、説明した前の簡単なコンポーネントにとっては問題ないかもしれません。
しかし、多くのコンポーネントは、そのテンプレートで記述されているDOM要素との複雑な対話を持ち、コンポーネントの状態が変わるとHTMLが表示および非表示になります。
@@ -32,19 +32,37 @@ Angularの `TestBed` は、次のセクションで説明するように、こ
CLIは、新しいコンポーネントの生成を要求するときに、デフォルトで初期テストファイルを自動的に生成します。
-たとえば、次のCLIコマンドは `app/banner` フォルダーに `BannerComponent` を生成します(インラインテンプレートとスタイル付き)。
+たとえば、次のCLIコマンドは `app/banner` フォルダーに `Banner` コンポーネントを生成します(インラインテンプレートとスタイル付き)。
```shell
-ng generate component banner --inline-template --inline-style --module app
+ng generate component banner --inline-template --inline-style
```
-また、コンポーネントの初期テストファイル `banner-external.component.spec.ts` も生成し、次のようになります。
+また、コンポーネントの初期テストファイル `banner.spec.ts` も生成し、次のようになります。
- with "banner works!"', () => {
+ const bannerElement: HTMLElement = fixture.nativeElement;
+ const p = bannerElement.querySelector('p')!;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
### `DebugElement`
Angularの _fixture_ は、`fixture.nativeElement` を介してコンポーネントの要素を直接提供します。
- with fixture.debugElement.nativeElement', () => {
+ const bannerDe: DebugElement = fixture.debugElement;
+ const bannerEl: HTMLElement = bannerDe.nativeElement;
+ const p = bannerEl.querySelector('p')!;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
`DebugElement` には、このガイドの他の場所で説明されているように、テストで役立つ他のメソッドとプロパティがあります。
Angularコアライブラリから `DebugElement` シンボルをインポートします。
- with fixture.debugElement.query(By.css)', () => {
+ const bannerDe: DebugElement = fixture.debugElement;
+ const paragraphDe = bannerDe.query(By.css('p'));
+ const p: HTMLElement = paragraphDe.nativeElement;
+ expect(p.textContent).toEqual('banner works!');
+});
+```
注目すべき観察結果をいくつか紹介します。
diff --git a/adev-ja/src/content/guide/testing/components-scenarios.en.md b/adev-ja/src/content/guide/testing/components-scenarios.en.md
index 8cc4023f01..a2699bd4b3 100644
--- a/adev-ja/src/content/guide/testing/components-scenarios.en.md
+++ b/adev-ja/src/content/guide/testing/components-scenarios.en.md
@@ -4,11 +4,22 @@ This guide explores common component testing use cases.
## Component binding
-In the example application, the `BannerComponent` presents static title text in the HTML template.
+In the example application, the `Banner` component presents static title text in the HTML template.
-After a few changes, the `BannerComponent` presents a dynamic title by binding to the component's `title` property like this.
+After a few changes, the `Banner` component presents a dynamic title by binding to the component's `title` property like this.
-
+ {{ quote | async }}
+ {{ errorMessage() }}{{ title() }}
',
+ styles: ['h1 { color: green; font-size: 350%}'],
+})
+export class Banner {
+ title = signal('Test Tour of Heroes');
+}
+```
As minimal as this is, you decide to add a test to confirm that component actually displays the right content where you think it should.
@@ -18,75 +29,86 @@ You'll write a sequence of tests that inspect the value of the `` element th
You update the `beforeEach` to find that element with a standard HTML `querySelector` and assign it to the `h1` variable.
-
` like this:
-
` have the expected title.
-
{{ welcome() }}
',
+})
+export class Welcome {
+ private userAuth = inject(UserAuthentication);
+ welcome = signal(
+ this.userAuth.isLoggedIn() ? `Welcome, ${this.userAuth.user().name}` : 'Please log in.',
+ );
+}
+```
-The `WelcomeComponent` has decision logic that interacts with the service, logic that makes this component worth testing.
+The `Welcome` component has decision logic that interacts with the service, logic that makes this component worth testing.
### Provide service test doubles
A _component-under-test_ doesn't have to be provided with real services.
-Injecting the real `UserService` could be difficult.
+Injecting the real `UserAuthentication` could be difficult.
The real service might ask the user for login credentials and attempt to reach an authentication server.
These behaviors can be hard to intercept. Be aware that using test doubles makes the test behave differently from production so use them sparingly.
### Get injected services
-The tests need access to the `UserService` injected into the `WelcomeComponent`.
+The tests need access to the `UserAuthentication` injected into the `Welcome` component.
Angular has a hierarchical injection system.
There can be injectors at multiple levels, from the root injector created by the `TestBed` down through the component tree.
@@ -158,7 +184,10 @@ The safest way to get the injected service, the way that **_always works_**,
is to **get it from the injector of the _component-under-test_**.
The component injector is a property of the fixture's `DebugElement`.
-
+