git checkout ut-05-spies
yarn
yarn test farmicode --watch
yarn ng g @wishtack/schematics:scam animal-search/animal-search
yarn ng g @wishtack/schematics:scam animal-search/animal-list
yarn ng g service animal-search/animal-search
Copy this ugly XML
<templateSet group="Marmicode">
<template name="sfam" value="import { CommonModule } from '@angular/common'; import { ChangeDetectionStrategy, Component, NgModule } from '@angular/core'; @Component({ changeDetection: ChangeDetectionStrategy.OnPush, selector: 'mc-$selector$', template: `🚧 $selector$` }) export class $cmp$Component {} @NgModule({ declarations: [$cmp$Component], exports: [$cmp$Component], imports: [CommonModule] }) export class $module$Module {}" description="" toReformat="true" toShortenFQNames="true">
<variable name="cmp" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="selector" expression="lowercaseAndDash(cmp)" defaultValue="" alwaysStopAt="true" />
<variable name="module" expression="cmp" defaultValue="" alwaysStopAt="true" />
<context>
<option name="JS_STATEMENT" value="true" />
<option name="TS_STATEMENT" value="true" />
</context>
</template>
</templateSet>
into ~/Library/Application\ Support/JetBrains/IntelliJIdea2020.2/jba_config/ templates/Marmicode.xml
on OS X
or ~\AppData\Roaming\JetBrains\IntelliJIdea2020.1\jba_config\templates/Marmicode.xml
on windows
-
⌘ + P => Configure User Snippets => typescript.json
-
Add the following configuration
{
"SFAM": {
"prefix": "sfam",
"body": [
"import { CommonModule } from '@angular/common';",
"import { ChangeDetectionStrategy, Component, NgModule } from '@angular/core';",
"@Component({",
" changeDetection: ChangeDetectionStrategy.OnPush,",
" selector: '$2',",
" template: `🚧 <$2>`",
"})",
"export class $1Component {}",
"",
"@NgModule({",
" declarations: [$1Component],",
" exports: [$1Component],",
" imports: [CommonModule]",
"})",
"export class $1Module {}"
]
}
}
git checkout ut-06-component-shallow-testing-boilerplate
- Inject
AnimalSearch
withTestBed.inject
. - Mock
AnimalSearch.search
method and return a fake result. - Call
component.search(keywords)
. - Check that child component
<fz-animal-list>
'sanimals
input contains the returned animals. ⚠️ Don't forget to check that the mock has been called properly!- Don't use the
async
pipe (for the moment).
/* Query a child element. */
const animalListEl = fixture.debugElement.query(By.css('fz-animal-list'));
/* Read element properties. */
console.log(animalListEl.properties);
Now that the test is working, you can use the async
pipe to pass the animals as an input to the child component.