Helpers and examples for unit testing on Angular applications and libraries.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { DebugElement } from '@angular/core'; | |
import { ComponentFixture } from '@angular/core/testing'; | |
import { By } from '@angular/platform-browser'; | |
export interface ComponentSuiteElements<H, N = any> { | |
host: ComponentTestingElement<H>; | |
nested?: ComponentTestingElement<N>; | |
} | |
export interface ComponentTestingElement<T> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OnDestroy } from '@angular/core'; | |
import { Constructor } from '@my-scope/my-ng-lib/constructor'; | |
import { Subject } from 'rxjs'; | |
// WARNING: THIS DOES NOT WORK IN ANGULAR 8 WITH AOT! | |
// HOWEVER, IT DOES WORK IN ANGULAR 9! | |
/** | |
* Mixin class to automatically unsubscribe in component classes. | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component, OnInit } from '@angular/core'; | |
import { interval, Observable } from 'rxjs'; | |
import { takeUntil } from 'rxjs/operators'; | |
import { subscribedContainerMixin } from '@my-scope/my-ng-lib/subscribed-container.mixin'; | |
@Component({ | |
selector: 'subscribed-container', | |
templateUrl: './subscribed-container.component.html' | |
}) | |
export class SubscribedContainerComponent extends subscribedContainerMixin() implements OnInit { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OnDestroy } from '@angular/core'; | |
import { Subject } from 'rxjs'; | |
export abstract class SubscribedContainer implements OnDestroy { | |
destroyed$ = new Subject<void>(); | |
/** | |
* DO NOT this.destroyed$.complete(); | |
* It is not necessary: | |
* https://stackoverflow.com/questions/44289859/do-i-need-to-complete-a-subject-for-it-to-be-garbage-collected | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Location } from '@angular/common'; | |
import { Injectable } from '@angular/core'; | |
import { NavigationExtras, Router } from '@angular/router'; | |
import * as fromRootModels from '@first-app/models'; | |
import * as routerActions from '@first-app/store/actions/router.action'; | |
import { Actions, Effect, ofType } from '@ngrx/effects'; | |
import { map, tap } from 'rxjs/operators'; | |
@Injectable() | |
export class RouterEffects { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Bubbling Example</title> | |
<style> | |
.div-style { | |
display: table-cell; | |
padding: 20px; | |
text-align: center; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function fibonacci(size) { | |
var result = [0]; | |
var counter; | |
for (counter = 1; counter < size; counter++) { | |
result[counter] = (result[counter-1] || 1) + result[Math.max(0, counter-2)]; | |
} | |
return result; | |
} | |
// fibonacci(8) returns [ 0, 1, 1, 2, 3, 5, 8, 13 ] |
NewerOlder