path | title |
---|---|
/learnings/learning_angular |
Learnings: Angular |
- General Angular Information
- Architecture / Organization
- Directives
- RxJS
- Testing Angular Applications
- End To End / Integration Tests
Will only do the filter when the observable has some value.
Gives you a large object that you can route the value from the subscription into variable
- Constructor
ngOnChanges
ngOnInit
ngDoCheck
<-- each pass of change detectorngAfterContextInit
<-- after child component state inited and projection completengAfterContentChecked
ngAfterViewInit
ngAfterViewChecked
ngDestroy
to pull those in inherit from interface that is the name without the Ng implements DoCheck
for example
Can separate project out into root Application and modules. (The add the component to the root application ‘s manifest and use the directives from the module(s) you imported)
Gyou could organize your modules by feature, or by component, maybe even by domain in a domain driven design approach.
- event emmitter
- Subject <-- RxJS
- BehaviorSubject <-- rxjs
- [BOOKNOTE]:
You’ll notify the parent about the latest prices by emitting custom events via the @Output property of the component.
- Angular Component with Typescript
@Input
: lets you speciff essentially HTML attributes when you instantiate your compontent in an HTML template
@Output
: way to send events up, from child -> parent (). (Repmember, one way data flow. To send data up you need to use events.)
NOTE: Angular doesn’t offer an API to support event bubbling. (Have to use native DOM events for that)
(Mediator handles @Ouput events from one component and sends them as @Input attributes into another).
(Could make this an injectable service too...)
It’s redux but for Angular
And this time with RxJS backing it - subscribe the the store.
- action
- Effect
- Reducer
- (The Store)
- Selectors
Routes are defined on a component / module level. export routes with forChild
to do this.
By default / generator routes live in routing.module.ts
- [BOOKQUOTE]:
If a module is loaded eagerly, its providers can be used in the entire app, but each lazy-loaded module has its own injector that doesn’t expose providers. Providers declared in the @NgModule() decorator of a lazy-loaded module are available within such a module, but not to the entire application. L
- Angular Development with Typescript (Ed 2)
Usually you'll stash all your HttpClient stuff into a service.
Can use Http Interceptors to add special things to the request, etc etc.
- Componets
- Structural Hirectives
- Attribute Directives
(the "new / fancy" Angular 7+ providedIn
syntax doesn't work here, for reasons...)
@Component( {
selector: 'foobar',
providers: [
{ provide: ContactService, useValue: aContactService }
]
})
export class ContactEditComponent {
constructor(contactIn: ContactService) {
this.contact = contactIn
}
}
Because types son’t really exist / are removed by the compiler. use abstract classes instead (really) and your classes that conform to that interface should just inherit from that base class.
First: tools:
- unittests: jasmine / Karma
- integration tests: Protractor
Official Angular documentation on testing
add karma-chrome-launcher to devDependencies
$ npm run karma run
tost component one level deep: don't render child componets
import { By } from '@amgular/platform-broswera
By.css('#hero_text') // by directive too! .directive function
RouterTestingModule
TestBed.configureTestingModule({
declarations: [ContactEditComponent, FavoriteIconDirective],
// ^^^^ components you are using go here
imports: [
AppMaterialModule,
FormsModule
],
// ^^^^ array of modules that the component you are testing requires
providers: [{provide: ContactService, useValue: contactServiceStub}]
// ^^^ override DI. (here we inject a mock - using the same provider mechansim as our component declared
});
- an excellent article explaining both how all these options work AND how tests work with it
- more on testing and DI in Angular
You can use fakeAsyc which enables you to write async code without having to deal with Jasmine's done
callback.
Angular builds in a module for mocking HTTP requests
this also automatically does fakeAsync
work, so you don't have to call done
.
(Also note HttpClient
is an RxJS Obserable, so those rules apply.)
Protractor (which bundles up Chrome, Firefox by way of WebDriver and/or Selenium). Firefox and and Chrome can support direct connections, without going though Selenium server, but all the other browsers you’ll have to use that.
protractor tests on headless chrome
- does Protractor end to end tests require the angular server running? (would potentially require a backend running) <-- how to replicate that???
- if it does, how to have port isolation to allow multiple end to end tests to run on the same Jenkins build agent (assume no build agent in separate Docker container isolation fanciness)
Because you need Firefox, Protractor, etc.
See how Grubhub does it with Docker containers.
Their setup got me thinking an interesting thought: do we use Docker compose or something to run the angular project in "one" container or along side the container to achieve isolation???
also points to their headless chrome container they use
A: It seems so:
A: yes. set multiCapabilities
Very easy: just point protractor towards the exec path from Puppetter?