tonivj5 / spectator

πŸ‘» Angular Tests Made Easy πŸ€“

Home Page:https://netbasal.gitbook.io/spectator/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Downloads All Contributors spectator MIT commitizen PRs styled with prettier Build Status

Angular Tests Made Easy

😎 Spectator

Spectator is written on top of the Angular Testing Framework and provides two things:

  • A cleaner API for testing.
  • A set of custom matchers that will help you to test DOM elements more easily.

Spectator helps you get rid of all the boilerplate grunt work, leaving you with readable, sleek and streamlined unit tests.

Installation

npm install @netbasal/spectator --save-dev

Documentation

Learn about it on the docs site

Spectator CLI

Auto generate specs with the CLI

A Taste of Spectator

// zippy.component.ts

@Component({
  selector: 'zippy',
  template: `
    <div class="zippy">
      <div (click)="toggle()" class="zippy__title">
        <span class="arrow">{{ visible ? 'Close' : 'Open' }}</span> {{title}}
      </div>
      <div *ngIf="visible" class="zippy__content">
        <ng-content></ng-content>
      </div>
    </div>
  `
})
export class ZippyComponent {

  @Input() title;
  visible = false;

  toggle() {
    this.visible = !this.visible;
  }
}

// zippy.component.spec.ts
import { ZippyComponent } from './zippy.component';
import { createHostComponentFactory, SpectatorWithHost } from '@netbasal/spectator';
import { Component } from '@angular/core';

describe('ZippyComponent', () => {
  let host: SpectatorWithHost<ZippyComponent>;

  const createHost = createHostComponentFactory(ZippyComponent);

  it('should display the title', () => {
    host = createHost(`<zippy title="Zippy title"></zippy>`);

    expect(host.query('.zippy__title')).toHaveText(( text ) => 'Zippy title');
  });

  it('should display the content', () => {
    host = createHost(`<zippy title="Zippy title">Zippy content</zippy>`);

    host.click('.zippy__title');

    expect(host.query('.zippy__content')).toHaveText('Zippy content');
  });

  it('should display the "Open" word if closed', () => {
    host = createHost(`<zippy title="Zippy title">Zippy content</zippy>`);

    expect(host.query('.arrow')).toHaveText('Open');
    expect(host.query('.arrow')).not.toHaveText('Close');
  });

  it('should display the "Close" word if open', () => {
      host = createHost(`<zippy title="Zippy title">Zippy content</zippy>`);

      host.click('.zippy__title');

      expect(host.query('.arrow')).toHaveText('Close');
      expect(host.query('.arrow')).not.toHaveText('Open');
    }
  );

  it('should be closed by default', () => {
    host = createHost(`<zippy title="Zippy title"></zippy>`);

    expect('.zippy__content').not.toExist();
  });

  it('should toggle the content', () => {
    host = createHost(`<zippy title="Zippy title"></zippy>`);

    host.click('.zippy__title');
    expect(host.query('.zippy__content')).toExist();

    host.click('.zippy__title');
    expect('.zippy__content').not.toExist();
  });

});

Datorama

Spectator is regularly used and maintained by Datorama.

Contributors

Thanks goes to these wonderful people (emoji key):


I. Sinai

πŸ“– πŸ‘€ 🎨

Valentin Buryakov

πŸ’» πŸ€”

Netanel Basal

πŸ’» πŸ”§

Ben Grynhaus

πŸ› πŸ’»

Ben Elliott

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

About

πŸ‘» Angular Tests Made Easy πŸ€“

https://netbasal.gitbook.io/spectator/

License:MIT License


Languages

Language:TypeScript 97.8%Language:JavaScript 1.8%Language:HTML 0.4%Language:CSS 0.1%