Skip to content

Instantly share code, notes, and snippets.

View jkyoutsey's full-sized avatar
📐

Jared Youtsey jkyoutsey

📐
View GitHub Profile
@jkyoutsey
jkyoutsey / ng2.class.decorator.pattern.ts
Created June 8, 2016 17:38
Angular2 Class Decorator Pattern
export function MyDecorator(target: any) {
// save a reference to the original constructor
var original = target;
// a utility function to generate instances of a class
function construct(constructor, args) {
let c: any = () => {
return constructor.apply(this, args);
};
c.prototype = constructor.prototype;
@jkyoutsey
jkyoutsey / logger.ts
Last active August 29, 2021 01:52
ngx logger class for consistent logging. Based on angular/cli's use of environments.
import { Injectable } from '@angular/core';
import { environment } from '@environments/environment';
/**
* Class of static methods to allow for consistent console logging.
* @export
*/
@Injectable({
providedIn: 'root'
@jkyoutsey
jkyoutsey / logger.spec.ts
Last active October 15, 2020 02:01
Unit test for logger.ts
import { Logger } from './logger';
import { environment } from '@environments/environment';
describe('Logger Spec', () => {
let debug;
let info;
let warn;
let err;
let log;
@jkyoutsey
jkyoutsey / can-deactivate-guard.service.spec.ts
Last active August 9, 2018 03:14
MockComponent for Angular CanDeactivate Unit Testing
import { TestBed } from '@angular/core/testing';
import { CanDeactivateGuardService } from './can-deactivate-guard.service';
import { CanDeactivate } from '@angular/router';
import { Observable, Subject } from 'rxjs';
import { CanDeactivateGuarded } from './can-deactivate-guarded';
class MockComponent implements CanDeactivateGuarded {
// Set this to the value you want to mock being returned from GuardedComponent
returnValue: boolean | Observable<boolean>;
@jkyoutsey
jkyoutsey / can-deactivate-guarded.ts
Last active August 9, 2018 03:16
Angular CanDeactivate Guard Interface
import { CanDeactivate } from '@angular/router';
import { Component } from '@angular/core';
import { Observable } from 'rxjs';
export interface CanDeactivateGuarded {
canDeactivate: () => boolean | Observable<boolean>;
}
@jkyoutsey
jkyoutsey / can-deactivate-guard.service.spec-mock-excerpt.ts
Created August 9, 2018 03:18
Angular CanDeactivate Guard MockComponent
class MockComponent implements CanDeactivateGuarded {
// Set this to the value you want to mock being returned from GuardedComponent
returnValue: boolean | Observable<boolean>;
canDeactivate(): boolean | Observable<boolean> {
return this.returnValue;
}
}
@jkyoutsey
jkyoutsey / can-deactivate-guard.service.ts
Created August 9, 2018 03:22
Angular CanDeactivate Guard Service
import { Injectable } from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { GuardedComponent } from './guarded/guarded.component';
import { CanDeactivateGuarded } from './can-deactivate-guarded';
@Injectable({
providedIn: 'root'
})
export class CanDeactivateGuardService implements CanDeactivate<CanDeactivateGuarded> {
canDeactivate(component: CanDeactivateGuarded) {
@jkyoutsey
jkyoutsey / can-deactivate-guard.service.spec-config-excerpt.ts
Created August 9, 2018 03:31
Angular CanDeactivate Guard Unit Test Configuration
describe('CanDeactivateGuardService', () => {
let mockComponent: MockComponent;
let service: CanDeactivateGuardService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
CanDeactivateGuardService,
MockComponent
]
@jkyoutsey
jkyoutsey / can-deactivate-guard.service.spec-tests.ts
Created August 9, 2018 03:34
Angular CanDeactivate Guard Unit Tests
it('can route if unguarded', () => {
// Mock GuardedComponent.isGuarded = false, which returns true from canActivate()
mockComponent.returnValue = true;
expect(service.canDeactivate(mockComponent)).toBeTruthy();
});
it('will route if guarded and user accepted the dialog', () => {
// Mock the behavior of the GuardedComponent:
const subject$ = new Subject<boolean>();
mockComponent.returnValue = subject$.asObservable();
@jkyoutsey
jkyoutsey / styles.scss
Last active January 3, 2019 19:30
CSS Grid for Angular global styles
app-root {
padding: 1em;
box-sizing: border-box;
height: 100vh;
width: 100vw;
display: grid;
grid-gap: 1em;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr;
grid-template-areas: 'blue green purple';