Skip to content

Instantly share code, notes, and snippets.

@dherges
dherges / function-works.component.ts
Created October 11, 2017 18:29
Angular: Componentful Functions
function MyComponent () {
this.message = 'foo';
}
MyComponent.prototype.ngOnInit = function () {
this.message = 'bar';
};
MyComponent.prototype.sayHello = function () {
this.message = 'Hello World';
@dherges
dherges / componify.ts
Created October 11, 2017 18:23
Angular: Componentful Functions (componify)
import { Component, Type } from '@angular/core';
const compσηιψ = (μ: Component, λ: Function): Type<any> =>
Component(μ).Class(Object.assign({ constructor: λ, λ }));
@dherges
dherges / redux-lite.ts
Last active September 28, 2017 19:49
Redux Lite for local states
/*
* Minimal redux for local states
* @link https://spin.atomicobject.com/2017/07/24/redux-action-pattern-typescript/
*/
export interface ReducerFn<S, A> {
(state: S, action: A): S
}
export interface Action<T> {
@dherges
dherges / http-client-feature.service.spec.ts
Last active January 23, 2018 12:26
Angular HttpClient (6)
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientModule, HttpRequest, HttpParams } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HttpClientFeatureService } from './http-client-feature.service';
describe(`HttpClientFeatureService`, () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
@dherges
dherges / http-client-feature.service.ts
Last active March 12, 2023 14:50
Angular HttpClient (5)
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
/** This class implements some features that should be tested. */
@Injectable()
export class HttpClientFeatureService {
constructor(
private http: HttpClient
@dherges
dherges / fake-http-client.spec.ts
Created August 17, 2017 19:00
Angular HttpClient (4)
describe(`FakeHttpClientResponses`, () => {
it(`should respond with fake data`, async(inject([HttpClient, HttpTestingController],
(http: HttpClient, backend: HttpTestingController) => {
http.get('/foo/bar').subscribe((next) => {
expect(next).toEqual({ baz: '123' });
});
backend.match({
url: '/foo/bar',
@dherges
dherges / fake-http-client.spec.ts
Last active December 13, 2017 15:40
Angular HttpClient (3)
describe(`FakeHttpClientResponses`, () => {
it(`should expect a GET /foo/bar`, async(inject([HttpClient, HttpTestingController],
(http: HttpClient, backend: HttpTestingController) => {
http.get('/foo/bar').subscribe();
backend.expectOne({
url: '/foo/bar',
method: 'GET'
});
@dherges
dherges / foo-bar.module.ts
Last active August 17, 2017 10:06
Angular HttpClient (1)
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FooBarService } from './foo-bar.service';
@NgModule({
imports: [ HttpClientModule ],
providers: [ FooBarService ]
})
export class FooBarModule {}
@dherges
dherges / fake-http-client.spec.ts
Last active June 1, 2018 07:09
Angular HttpClient (2)
import { TestBed, async, inject } from '@angular/core/testing';
import { HttpClientModule, HttpClient } from '@angular/common/http';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe(`FakeHttpClientResponses`, () => {
beforeEach(() => {
// 0. set up the test environment
TestBed.configureTestingModule({
imports: [
@dherges
dherges / app.po.ts
Created August 4, 2017 07:55
Angular, Protractor, Cucumber
import { browser, by, element, until } from 'protractor';
export class AppPage {
public navigateTo() {
return browser.get('/');
}
public enterSearchInput(text: string) {
return element(by.css('input[aria-label="search"]'))