Skip to content

Commit

Permalink
fix: Fix failing Switch test (#2673)
Browse files Browse the repository at this point in the history
* fix(core) Fix tests

* apply same idea to radio test

Co-authored-by: Mike O'Donnell <mikerodonnell89@users.noreply.github.com>
  • Loading branch information
salarenko and mikerodonnell89 committed Jun 17, 2020
1 parent 9f410fb commit e6a41f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
@@ -1,7 +1,7 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { RadioButtonComponent } from './radio-button.component';
import { Component, ViewChild } from '@angular/core';
import { ChangeDetectorRef, Component, ViewChild } from '@angular/core';
import { FormsModule, FormGroup, FormControl, ReactiveFormsModule } from '@angular/forms';

@Component({
Expand Down Expand Up @@ -34,6 +34,7 @@ class TestRadioButtonComponentReactiveForms {
describe('RadioButtonComponent reactive forms', () => {
let component: TestRadioButtonComponentReactiveForms;
let fixture: ComponentFixture<TestRadioButtonComponentReactiveForms>;
let changeDetectorRef: ChangeDetectorRef;

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -45,10 +46,12 @@ describe('RadioButtonComponent reactive forms', () => {
beforeEach(() => {
fixture = TestBed.createComponent(TestRadioButtonComponentReactiveForms);
component = fixture.componentInstance;
changeDetectorRef = fixture.componentRef.injector.get(ChangeDetectorRef);
fixture.detectChanges();
});

async function wait(componentFixture: ComponentFixture<any>) {
changeDetectorRef.markForCheck();
componentFixture.detectChanges();
await componentFixture.whenStable();
}
Expand Down
27 changes: 13 additions & 14 deletions libs/core/src/lib/switch/switch.component.spec.ts
Expand Up @@ -48,23 +48,22 @@ describe('SwitchComponent', () => {
expect(component.id).toBeTruthy();
});

it('should switch on click', fakeAsync (() => {
spyOn(component.checkedChange, 'emit');

component.checked = false;
fixture.detectChanges();
it('should switch on click', fakeAsync(() => {

const checkedChangeSpy = spyOn(component.checkedChange, 'emit');
input.click();
fixture.detectChanges();
tick(10);

expect(component.checkedChange.emit).toHaveBeenCalledWith(true);
detectChangesOnPush();
tick();

expect(checkedChangeSpy).toHaveBeenCalledWith(true);

input.click();
fixture.detectChanges();
tick(10);

expect(component.checkedChange.emit).toHaveBeenCalledWith(false);
detectChangesOnPush();
tick();

expect(checkedChangeSpy).toHaveBeenCalledWith(false);
}));

it('should focus inner input element', () => {
Expand Down Expand Up @@ -95,12 +94,12 @@ describe('SwitchComponent', () => {
expect(switchComp.classList).toContain('fd-switch--semantic');
});

it('should disable', async () => {
it('should disable', fakeAsync(() => {
spyOn(component.checkedChange, 'emit');
component.disabled = true;

detectChangesOnPush();
await fixture.whenStable();
tick();

input.click();

Expand All @@ -109,5 +108,5 @@ describe('SwitchComponent', () => {
expect(component.checkedChange.emit).not.toHaveBeenCalled();
expect(switchComp.classList).toContain('is-disabled');
expect(input.disabled).toBeTrue();
});
}));
});

0 comments on commit e6a41f6

Please sign in to comment.