Created
March 28, 2018 19:07
-
-
Save allenhwkim/6fa9dd9431b561f5c7b8bbda666b7bf5 to your computer and use it in GitHub Desktop.
Angular5+ test mock
This file contains hidden or 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 {Pipe, PipeTransform, Type, Component, Directive, EventEmitter } from '@angular/core'; | |
| /** | |
| * Examples: | |
| * MockPipe('some-pipe'); | |
| * MockPipe('some-pipe', () => 'foo'); | |
| */ | |
| export function MockPipe(name: string, transform?: any): Pipe { | |
| class Mock implements PipeTransform { | |
| transform = transform || () => undefined; | |
| } | |
| return Pipe({name:name})(Mock as any); | |
| } | |
| /** | |
| * Examples: | |
| * MockComponent({selector: 'some-component'}); | |
| * MockComponent({selector: 'some-component', inputs: ['some-input', 'some-other-input']}); | |
| */ | |
| export function MockComponent(options: Component): Component { | |
| const metadata: Component = { | |
| selector: options.selector, | |
| template: options.template || '', | |
| inputs: options.inputs, | |
| outputs: options.outputs || [], | |
| exportAs: options.exportAs || '' | |
| }; | |
| class Mock { | |
| constructor() { | |
| metadata.outputs.forEach(method => { | |
| this[method] = new EventEmitter<any>(); | |
| }); | |
| } | |
| } | |
| return Component(metadata)(Mock as any); | |
| } | |
| /** | |
| * Examples: | |
| * MockDirective({selector: '[some-directive]'}); | |
| * MockDirective({selector: '[some-directive]', inputs: ['some-input', 'some-other-input']}); | |
| */ | |
| export function MockDirective(options: Directive): Directive { | |
| const metadata: Directive = { | |
| selector: options.selector, | |
| inputs: options.inputs, | |
| outputs: options.outputs || [], | |
| providers: options.providers || [], | |
| exportAs: options.exportAs || '' | |
| }; | |
| class Mock { | |
| constructor() { | |
| metadata.outputs.forEach(method => { | |
| this[method] = new EventEmitter<any>(); | |
| }); | |
| } | |
| } | |
| return Directive(metadata)(Mock as any); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment