Skip to content

Commit 295b1f7

Browse files
committed
fix(radio): tidy up tests and fix ngmodel propagation
1 parent fa2db68 commit 295b1f7

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

src/radio/radio-group.component.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,10 @@ export class RadioGroup implements AfterContentInit, AfterViewInit, ControlValue
227227
/**
228228
* Creates a class of `RadioChange` to emit the change in the `RadioGroup`.
229229
*/
230-
emitChangeEvent(event) {
230+
emitChangeEvent(event: RadioChange) {
231231
this.change.emit(event);
232+
this.propagateChange(event.value);
233+
this.onTouched();
232234
}
233235

234236
/**
@@ -249,15 +251,6 @@ export class RadioGroup implements AfterContentInit, AfterViewInit, ControlValue
249251
this.value = value;
250252
}
251253

252-
/**
253-
* Callback triggered when a `Radio` within the `RadioGroup` is changed.
254-
*/
255-
touch() {
256-
if (this.onTouched) {
257-
this.onTouched();
258-
}
259-
}
260-
261254
ngAfterContentInit() {
262255
this.radios.changes.subscribe(() => {
263256
this.updateRadioNames();
@@ -276,10 +269,7 @@ export class RadioGroup implements AfterContentInit, AfterViewInit, ControlValue
276269
* Used to set method to propagate changes back to the form.
277270
*/
278271
public registerOnChange(fn: any) {
279-
this.propagateChange = value => {
280-
this.value = value;
281-
fn(value);
282-
};
272+
this.propagateChange = fn;
283273
}
284274

285275
/**

src/radio/radio.component.spec.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
2-
import {
3-
ComponentFixture,
4-
TestBed,
5-
fakeAsync,
6-
tick,
7-
async
8-
} from "@angular/core/testing";
1+
import { ComponentFixture, TestBed } from "@angular/core/testing";
92
import { By } from "@angular/platform-browser";
103
import { FormsModule } from "@angular/forms";
114
import { DebugElement, Component } from "@angular/core";
@@ -14,12 +7,25 @@ import { StaticIconModule } from "../icon/static-icon.module";
147
import { Radio } from "./radio.component";
158
import { RadioGroup } from "./radio-group.component";
169

10+
@Component({
11+
selector: "test-cmp",
12+
template: `
13+
<ibm-radio-group [(ngModel)]="radio">
14+
<ibm-radio *ngFor="let one of manyRadios" [value]="one"
15+
class="indent">Radio {{one}}
16+
</ibm-radio>
17+
</ibm-radio-group>`
18+
})
19+
class RadioTest {
20+
manyRadios = ["one", "two", "three", "four", "five", "six"];
21+
radio: string;
22+
}
23+
1724
describe("RadioGroup", () => {
1825
beforeEach(() => {
1926
TestBed.configureTestingModule({
2027
declarations: [Radio, RadioGroup, RadioTest],
21-
imports: [BrowserAnimationsModule, FormsModule, StaticIconModule],
22-
providers: []
28+
imports: [FormsModule, StaticIconModule]
2329
});
2430
});
2531

@@ -35,31 +41,15 @@ describe("RadioGroup", () => {
3541
const fixture = TestBed.createComponent(RadioTest);
3642
fixture.detectChanges();
3743

38-
const directiveEl = fixture.debugElement.query(By.directive(RadioGroup));
3944
const radioOne = fixture.debugElement.query(By.directive(Radio));
45+
radioOne.triggerEventHandler("click", null);
4046
radioOne.nativeElement.querySelector("input").click();
4147
fixture.detectChanges();
4248

4349
expect(fixture.componentInstance.radio).toBe("one");
4450
});
4551
});
4652

47-
@Component({
48-
selector: "test-cmp",
49-
template: `
50-
<ibm-radio-group [(ngModel)]="radio">
51-
<ibm-radio *ngFor="let one of manyRadios" [value]="one"
52-
class="indent">Radio {{one}}
53-
</ibm-radio>
54-
</ibm-radio-group>`,
55-
entryComponents: [Radio]
56-
})
57-
class RadioTest {
58-
manyRadios = ["one", "two", "three", "four", "five", "six"];
59-
radio: string;
60-
}
61-
62-
6353
describe("RadioComponent", () => {
6454
let component: Radio;
6555
let fixture: ComponentFixture<Radio>;
@@ -68,9 +58,7 @@ describe("RadioComponent", () => {
6858

6959
beforeEach(() => {
7060
TestBed.configureTestingModule({
71-
declarations: [Radio],
72-
imports: [BrowserAnimationsModule],
73-
providers: []
61+
declarations: [Radio]
7462
});
7563

7664
fixture = TestBed.createComponent(Radio);

0 commit comments

Comments
 (0)