GithubHelp home page GithubHelp logo

isabella232 / ngentest Goto Github PK

View Code? Open in Web Editor NEW

This project forked from accenture/ngentest

0.0 0.0 0.0 76 KB

Angular5+ Unit Test Generator For Components, Directive, Services, and Pipes

License: MIT License

JavaScript 68.80% TypeScript 23.85% HTML 7.35%

ngentest's Introduction

ngentest

Angular5+ Unit Test Generator For Components, Directive, Services, and Pipes

How It Works

  1. Parse component/directive/service, then prepare the following data.
    • className
    • imports
    • input/output attributes and properties
    • mocks
    • providers for TestBed
    • list of functions to test
  2. Generate unit test from prepared data with .ejs template

Install & Run

$ npm install ngentest -g # to run this command anywhere
$ ngentest my.component.ts # node_modules/.bin/gentest
$ ngentest my.directive.ts -s # write unit test to my.directive.spec.ts
$ ngentest my.pipe.ts > my.pipe.test.ts 
$ ngentest my.service.ts

Examples

comopent unit test generated

my.component.ts

$ gentest my.component.ts > my.component.spec.ts

my.component.spec.ts

// tslint:disble
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import {MyComponent} from './src/examples/my.component';
import {Directive, ElementRef, Renderer2, Inject, PLATFORM_ID} from '@angular/core';

@Injectable()
class MockElementRef  {
  // constructor() { super(undefined); }
  nativeElement = {}
}
(<any>window).IntersectionObserver = jest.fn();

describe('MyComponent', () => {
  let fixture;
  let component;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [
        MyComponent
      ],
      providers: [
        {provide: ElementRef, useClass: MockElementRef},
        Renderer2,
        {provide: PLATFORM_ID,useValue: 'browser'},
      ]
    }).compileComponents();
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.debugElement.componentInstance;
  });

  it('should create a component', async () => {
    expect(component).toBeTruthy();
  });


  it('should run #ngOnInit()', async () => {
    // ngOnInit();
  });

  it('should run #handleIntersect()', async () => {
    // handleIntersect(entries, observer);
  });

  it('should run #defaultInviewHandler()', async () => {
    // const result = defaultInviewHandler(entry);
  });

});

directive unit test generated

my.directive.ts

$ gentest my.directive.ts > my.directrive.spec.ts

my.directive.spec.ts

// tslint:disable
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import {By} from '@angular/platform-browser';

import {MyDirective} from './src/examples/my.directive';
import {Directive, ElementRef, Renderer2, Inject, PLATFORM_ID} from '@angular/core';

@Injectable()
class MockElementRef {
  // constructor() { super(undefined); }
  nativeElement = {}
}
(<any>window).IntersectionObserver = jest.fn();

@Component({
  template: `
    <div [options]="options" (nguiInview)="onNguiInview($event)" (nguiOutview)="onNguiOutview($event)"></div>
  `
})
class DirectiveTestComponent {
  options: any;

  onNguiInview(event): void { /* */ }
  onNguiOutview(event): void { /* */ }
}

describe('MyDirective', () => {
  let fixture: ComponentFixture<TestComponent>;
  let component: DirectiveTestComponent;
  let directiveEl;
  let directive;

  beforeEach(async () => {
    TestBed.configureTestingModule({
      declarations: [MyDirective, DirectiveTestComponent],
      providers: [
        {provide: ElementRef, useClass: MockElementRef},
        Renderer2,
        {provide: PLATFORM_ID,useValue: 'browser'},
      ]
    }).compileComponents();
    fixture = TestBed.createComponent(TestComponent);
    component = fixture.componentInstance;
    directiveEl = fixture.debugElement.query(By.directive(MyDirective));
    directive = directiveEl.injector.get(MyDirective);
  });

  it("should run a directive", async () => {
    expect(component).toBeTruthy();
    expect(directive).toBeTruthy();
  });


  it('should run #ngOnInit()', async () => {
    // ngOnInit();
  });

  it('should run #ngOnDestroy()', async () => {
    // ngOnDestroy();
  });

  it('should run #handleIntersect()', async () => {
    // handleIntersect(entries, observer);
  });

});

service unit test generated

my.service.ts

$ gentest my.service.ts > my.service.spec.ts

my.directive.spec.ts

import {DynamicComponentService} from './src/examples/my.service';

describe('DynamicComponentService', () => {
  let service;


  const factoryResolver = {
    // mock properties here
  }

  beforeEach(() => {
    service = new DynamicComponentService(factoryResolver);
  });


  it('should run #createComponent()', async () => {
    // const result = createComponent(component, into);
  });

  it('should run #insertComponent()', async () => {
    // const result = insertComponent(componentRef);
  });

});

pipe unit test generated

my.pipe.ts

$ gentest my.pipe.ts > my.pipe.spec.ts

my.pipe.spec.ts

import {NguiHighlightPipe} from './src/examples/my.pipe';

describe('NguiHighlightPipe', () => {

  it('should run #transform', () => {
    // const pipe = new NguiHighlightPipe();
    // const result = pipe.transform(text, search);
    // expect(result).toBe('<<EXPECTED>>');
  });

});

ngentest's People

Contributors

allenhwkim avatar bbadrinath avatar kumar-rajeev avatar mithunsaha19 avatar navinvijaykumar avatar stamminator avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.