Skip to content

Commit

Permalink
feat(radio): added unit test for disable state
Browse files Browse the repository at this point in the history
  • Loading branch information
pimenovoleg committed May 7, 2018
1 parent 43e0bf5 commit 8aeb5ac
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 108 deletions.
27 changes: 19 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion src/lib/radio/radio.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,13 @@ export class McRadioButton extends _McRadioButtonMixinBase
return this._disabled || (this.radioGroup != null && this.radioGroup.disabled);
}
set disabled(value: boolean) {
this._disabled = toBoolean(value);
const newDisabledState = toBoolean(value);

if (this._disabled !== newDisabledState) {

this._disabled = newDisabledState;
this._changeDetector.markForCheck();
}
}

/** Whether the radio button is required. */
Expand Down
65 changes: 65 additions & 0 deletions src/lib/radio/radio.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Component, DebugElement, ViewChild } from '@angular/core';
import { async, ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing';
import { FormControl, FormsModule, NgModel, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { McRadioButton, McRadioChange, McRadioGroup, McRadioModule } from './index';


describe('MсRadio', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [McRadioModule, FormsModule, ReactiveFormsModule],
declarations: [
DisableableRadioButton
]
});

TestBed.compileComponents();
}));

describe('disableable', () => {
let fixture: ComponentFixture<DisableableRadioButton>;
let radioInstance: McRadioButton;
let radioNativeElement: HTMLInputElement;
let testComponent: DisableableRadioButton;

beforeEach(() => {
fixture = TestBed.createComponent(DisableableRadioButton);
fixture.detectChanges();

testComponent = fixture.debugElement.componentInstance;

const radioDebugElement = fixture.debugElement.query(By.directive(McRadioButton));
radioInstance = radioDebugElement.injector.get<McRadioButton>(McRadioButton);
radioNativeElement = radioDebugElement.nativeElement.querySelector('input');
});

it('should toggle the disabled state', () => {
expect(radioInstance.disabled).toBeFalsy();
expect(radioNativeElement.disabled).toBeFalsy();

testComponent.disabled = true;
fixture.detectChanges();
expect(radioInstance.disabled).toBeTruthy();
expect(radioNativeElement.disabled).toBeTruthy();

testComponent.disabled = false;
fixture.detectChanges();
expect(radioInstance.disabled).toBeFalsy();
expect(radioNativeElement.disabled).toBeFalsy();
});
});
});

@Component({
template: `<mc-radio-button>One</mc-radio-button>`
})
class DisableableRadioButton {
@ViewChild(McRadioButton) mcRadioButton;

set disabled(value: boolean) {
this.mcRadioButton.disabled = value;
}
}
Loading

0 comments on commit 8aeb5ac

Please sign in to comment.