Skip to content

Commit

Permalink
feat(buttons): Fixed disabled buttons module tests (#1604)
Browse files Browse the repository at this point in the history
* added fakeAsync and tick calls to force change detection work in tests

* fixed karma config

* enabled code coverage

* chore(build): fixed karma config (#1606)

* added fakeAsync and tick calls to force change detection work in tests
  • Loading branch information
Le0Michine authored and valorkin committed Feb 28, 2017
1 parent 29dceba commit 4df17c7
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions src/spec/button.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ComponentFixture, TestBed, fakeAsync, tick, ComponentFixtureAutoDetect } from '@angular/core/testing';
import { FormsModule } from '@angular/forms';

import { ButtonsModule } from '../buttons/buttons.module';
Expand Down Expand Up @@ -46,32 +46,36 @@ describe('Directive: Buttons', () => {
let context:any;
let element:any;

beforeEach(() => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
declarations: [TestButtonsComponent],
imports: [ButtonsModule, FormsModule]
imports: [ButtonsModule, FormsModule],
providers: [{ provide: ComponentFixtureAutoDetect, useValue: true }]
});
TestBed.overrideComponent(TestButtonsComponent, {set: {template: html}});
fixture = TestBed.createComponent(TestButtonsComponent);
context = fixture.componentInstance;
element = fixture.nativeElement;
fixture.detectChanges();
});
tick();
}));

describe('checkbox', () => {
xit('should work correctly with default model values', () => {
it('should work correctly with default model values', fakeAsync(() => {
expect(element.querySelector('#default').classList).not.toContain('active');
context.singleModel = true;
fixture.detectChanges();
tick();
expect(element.querySelector('#default').classList).toContain('active');
});
}));

xit('should bind custom model values', () => {
it('should bind custom model values', fakeAsync(() => {
expect(element.querySelector('#custom').classList).not.toContain('active');
context.singleModel = '1';
fixture.detectChanges();
tick();
expect(element.querySelector('#custom').classList).toContain('active');
});
}));

it('should toggle default model values on click', () => {
context.singleModel = false;
Expand Down Expand Up @@ -119,47 +123,50 @@ describe('Directive: Buttons', () => {
expect(btn.classList).not.toContain('active');
});

xit('should work for btn-group', () => {
it('should work for btn-group', () => {
let btn = element.querySelector('.btn-group.checkbox');
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).toContain('active');
expect(btn.children[2].classList).not.toContain('active');
});
});

xdescribe('radio', () => {
it('should set active class based on model', () => {
describe('radio', () => {
it('should set active class based on model', fakeAsync(() => {
let btn = element.querySelector('.btn-group.radio');
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).toContain('active');
expect(btn.children[2].classList).not.toContain('active');

context.radioModel = 'Left';
fixture.detectChanges();
tick();
expect(btn.children[0].classList).toContain('active');
expect(btn.children[1].classList).not.toContain('active');
expect(btn.children[2].classList).not.toContain('active');
});
}));

it('should set active class via click', () => {
it('should set active class via click', fakeAsync(() => {
let btn = element.querySelector('.btn-group.radio');
delete context.radioModel;
expect(context.radioModel).toBeUndefined();

(btn.children[2] as HTMLElement).click();
fixture.detectChanges();
tick();
expect(context.radioModel).toEqual('Right');
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).not.toContain('active');
expect(btn.children[2].classList).toContain('active');

(btn.children[1] as HTMLElement).click();
fixture.detectChanges();
tick();
expect(context.radioModel).toEqual('Middle');
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).toContain('active');
expect(btn.children[2].classList).not.toContain('active');
});
}));

it('should do nothing when clicking an active radio', () => {
let btn = element.querySelector('.btn-group.radio');
Expand All @@ -176,7 +183,7 @@ describe('Directive: Buttons', () => {
expect(btn.children[2].classList).not.toContain('active');
});

xit('should not toggle when disabled', () => {
it('should not toggle when disabled', () => {
let btn = element.querySelector('.btn-group.radio');
expect(context.radioModel).toEqual('Middle');
expect(btn.children[1].classList).toContain('active');
Expand All @@ -193,7 +200,7 @@ describe('Directive: Buttons', () => {
expect(btn.children[3].classList).not.toContain('active');
});

xit('should unset active class via click', () => {
it('should unset active class via click', () => {
let btn = element.querySelector('.btn-group.radioUncheckable');
expect(context.radioUncheckableModel).toBeUndefined();

Expand All @@ -206,7 +213,7 @@ describe('Directive: Buttons', () => {

(btn.children[0] as HTMLElement).click();
fixture.detectChanges();
expect(context.radioUncheckableModel).toBeNull();
expect(context.radioUncheckableModel).toBeUndefined();
expect(btn.children[0].classList).not.toContain('active');
expect(btn.children[1].classList).not.toContain('active');
expect(btn.children[2].classList).not.toContain('active');
Expand Down

0 comments on commit 4df17c7

Please sign in to comment.