Skip to content

Commit

Permalink
fixed #6641
Browse files Browse the repository at this point in the history
radiobutton unit test improved.
test;
+disabled, name inputId value style styleClass label labelStyleClass tabindex checked.
+disabled click checked.
+box click and label click checked.
+focus checked.
  • Loading branch information
yigitfindikli committed Oct 5, 2018
1 parent e5a83fc commit ca318f5
Showing 1 changed file with 120 additions and 0 deletions.
120 changes: 120 additions & 0 deletions src/app/components/radiobutton/radiobutton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,31 @@ describe('RadioButton', () => {
radiobutton = fixture.componentInstance;
});

it('should change name inputId value style styleClass label labelStyleClass and tabIndex', () => {
radiobutton.name = "primeng";
radiobutton.inputId = "prime"
radiobutton.value = "Primeng";
radiobutton.style = {'primeng': 'rocks!'};
radiobutton.styleClass = "Primeng ROCKS!";
radiobutton.label = "Prime";
radiobutton.labelStyleClass = "Primeng ROCKS";
radiobutton.tabindex = 13;
fixture.detectChanges();

const radiobuttonEl = fixture.debugElement.query(By.css('div'));
const inputEl = fixture.debugElement.query(By.css('input'));
const labelEl = fixture.debugElement.query(By.css('label'));
expect(inputEl.nativeElement.name).toEqual("primeng");
expect(inputEl.nativeElement.value).toEqual("Primeng");
expect(inputEl.nativeElement.id).toEqual("prime");
expect(inputEl.nativeElement.tabIndex).toEqual(13);
expect(radiobuttonEl.nativeElement.className).toContain("Primeng ROCKS!");
expect(radiobuttonEl.nativeElement.style.primeng).toEqual("rocks!");
expect(labelEl.nativeElement.className).toContain("Primeng ROCKS");
expect(labelEl.nativeElement.textContent).toEqual("Prime");
expect(labelEl.nativeElement.htmlFor).toEqual("prime");
});

it('should display active state initially when checked by default', () => {
radiobutton.checked = true;
radiobutton.inputViewChild.nativeElement.checked=true;
Expand All @@ -30,4 +55,99 @@ describe('RadioButton', () => {
const boxEl = fixture.nativeElement.querySelector('.ui-radiobutton-box');
expect(boxEl.className).toContain('ui-state-active');
});

it('should disabled', () => {
radiobutton.disabled = true;
radiobutton.label = "prime"
fixture.detectChanges();

const handleClickSpy = spyOn(radiobutton,'handleClick').and.callThrough();
const selectSpy = spyOn(radiobutton,'select').and.callThrough();
const radiobuttonEl = fixture.debugElement.queryAll(By.css('div'))[2];
const inputEl = fixture.debugElement.query(By.css('input'));
const labelEl = fixture.debugElement.query(By.css('label'));
expect(inputEl.nativeElement.disabled).toEqual(true);
expect(radiobuttonEl.nativeElement.className).toContain("ui-state-disabled");
expect(labelEl.nativeElement.className).toContain("ui-label-disabled");

radiobuttonEl.nativeElement.click();
fixture.detectChanges();

expect(handleClickSpy).toHaveBeenCalled();
expect(selectSpy).not.toHaveBeenCalled();
expect(radiobutton.checked).toEqual(undefined);
labelEl.nativeElement.click();
fixture.detectChanges();

expect(handleClickSpy).toHaveBeenCalledTimes(1);
expect(selectSpy).toHaveBeenCalled();
expect(radiobutton.checked).toEqual(undefined);
});

it('should click checkbox', () => {
fixture.detectChanges();

let value;
radiobutton.onClick.subscribe(event => value = 5);
const handleClickSpy = spyOn(radiobutton,'handleClick').and.callThrough();
const selectSpy = spyOn(radiobutton,'select').and.callThrough();
const onFocusSpy = spyOn(radiobutton,'onFocus').and.callThrough();
const radiobuttonEl = fixture.debugElement.queryAll(By.css('div'))[2];
const inputEl = fixture.debugElement.query(By.css('input'));
const iconEl = fixture.debugElement.query(By.css('span'));
inputEl.nativeElement.dispatchEvent(new Event('focus'));
radiobuttonEl.nativeElement.click();
fixture.detectChanges();

expect(handleClickSpy).toHaveBeenCalled();
expect(selectSpy).toHaveBeenCalled();
expect(onFocusSpy).toHaveBeenCalled();
expect(radiobutton.checked).toEqual(true);
expect(value).toEqual(5);
expect(radiobutton.focused).toEqual(true);
expect(radiobuttonEl.nativeElement.className).toContain("ui-state-focus");
expect(iconEl.nativeElement.className).toContain("pi pi-circle-on");
});

it('should click label', () => {
radiobutton.label = "prime"
fixture.detectChanges();

let value;
radiobutton.onClick.subscribe(event => value = 5);
const handleClickSpy = spyOn(radiobutton,'handleClick').and.callThrough();
const selectSpy = spyOn(radiobutton,'select').and.callThrough();
const onFocusSpy = spyOn(radiobutton,'onFocus').and.callThrough();
const onBlurSpy = spyOn(radiobutton,'onBlur').and.callThrough();
const inputEl = fixture.debugElement.query(By.css('input'));
const labelEl = fixture.debugElement.query(By.css('label'));
inputEl.nativeElement.dispatchEvent(new Event('focus'));
labelEl.nativeElement.click();
fixture.detectChanges();

expect(handleClickSpy).not.toHaveBeenCalled();
expect(selectSpy).toHaveBeenCalled();
expect(onFocusSpy).toHaveBeenCalled();
expect(radiobutton.checked).toEqual(true);
expect(labelEl.nativeElement.className).toContain("ui-label-focus");
expect(value).toEqual(5);
inputEl.nativeElement.dispatchEvent(new Event('blur'));
fixture.detectChanges();

expect(radiobutton.focused).toEqual(false);
expect(onBlurSpy).toHaveBeenCalled();
});

it('should call writeValue', () => {
radiobutton.label = "prime";
radiobutton.value = "prime";
fixture.detectChanges();

const writeValueSpy = spyOn(radiobutton,'writeValue').and.callThrough();
radiobutton.writeValue("prime");
fixture.detectChanges();

expect(writeValueSpy).toHaveBeenCalled();
expect(radiobutton.checked).toEqual(true);
});
});

0 comments on commit ca318f5

Please sign in to comment.