Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Radio): Decouple the radio from the radio-group component. #669

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 13 additions & 20 deletions src/components/radio/radio.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {FORM_DIRECTIVES, NgControl} from '@angular/common';
import {TestComponentBuilder, ComponentFixture} from '@angular/compiler/testing';
import {Component, DebugElement, provide} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MD_RADIO_DIRECTIVES, MdRadioGroup, MdRadioButton, MdRadioChange} from './radio';
import {MD_RADIO_DIRECTIVES, MdRadioGroup, MdRadioButton} from './radio';
import {
MdUniqueSelectionDispatcher
} from '@angular2-material/core/coordination/unique-selection-dispatcher';
Expand Down Expand Up @@ -65,6 +65,11 @@ describe('MdRadio', () => {
for (let radio of radioInstances) {
expect(radio.name).toBe(groupInstance.name);
}

groupInstance.name = 'new name';
for (let radio of radioInstances) {
expect(radio.name).toBe(groupInstance.name);
}
});

it('should disable click interaction when the group is disabled', () => {
Expand Down Expand Up @@ -107,7 +112,6 @@ describe('MdRadio', () => {
fixture.detectChanges();

expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
});

it('should update the group and radios when one of the radios is clicked', () => {
Expand All @@ -117,15 +121,13 @@ describe('MdRadio', () => {
fixture.detectChanges();

expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
expect(radioInstances[0].checked).toBe(true);
expect(radioInstances[1].checked).toBe(false);

radioNativeElements[1].click();
fixture.detectChanges();

expect(groupInstance.value).toBe('water');
expect(groupInstance.selected).toBe(radioInstances[1]);
expect(radioInstances[0].checked).toBe(false);
expect(radioInstances[1].checked).toBe(true);
});
Expand All @@ -138,14 +140,13 @@ describe('MdRadio', () => {

expect(radioInstances[0].checked).toBe(true);
expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
});

it('should emit a change event from radio buttons', fakeAsync(() => {
expect(radioInstances[0].checked).toBe(false);

let changeSpy = jasmine.createSpy('radio change listener');
radioInstances[0].change.subscribe(changeSpy);
radioInstances[0].valueChange.subscribe(changeSpy);

radioInstances[0].checked = true;
fixture.detectChanges();
Expand All @@ -162,7 +163,7 @@ describe('MdRadio', () => {
expect(groupInstance.value).toBeFalsy();

let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
groupInstance.valueChange.subscribe(changeSpy);

groupInstance.value = 'fire';
fixture.detectChanges();
Expand Down Expand Up @@ -200,15 +201,13 @@ describe('MdRadio', () => {
fixture.detectChanges();

expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
expect(radioInstances[0].checked).toBe(true);
expect(radioInstances[1].checked).toBe(false);

testComponent.groupValue = 'water';
fixture.detectChanges();

expect(groupInstance.value).toBe('water');
expect(groupInstance.selected).toBe(radioInstances[1]);
expect(radioInstances[0].checked).toBe(false);
expect(radioInstances[1].checked).toBe(true);
});
Expand Down Expand Up @@ -270,12 +269,6 @@ describe('MdRadio', () => {
for (let radio of radioInstances) {
expect(radio.checked).toBeFalsy();
}

groupInstance.value = 'vanilla';
for (let radio of radioInstances) {
expect(radio.checked).toBe(groupInstance.value === radio.value);
}
expect(groupInstance.selected.value).toBe(groupInstance.value);
});

it('should have the correct ngControl state initially and after interaction', fakeAsync(() => {
Expand Down Expand Up @@ -354,7 +347,7 @@ describe('MdRadio', () => {

tick();
expect(testComponent.modelValue).toBe('chocolate');
expect(testComponent.lastEvent.value).toBe('chocolate');
expect(testComponent.lastEvent).toBe('chocolate');
}));
});

Expand Down Expand Up @@ -491,11 +484,11 @@ class RadiosInsideRadioGroup {
<md-radio-button name="season" value="spring">Spring</md-radio-button>
<md-radio-button name="season" value="summer">Summer</md-radio-button>
<md-radio-button name="season" value="autum">Autumn</md-radio-button>

<md-radio-button name="weather" value="warm">Spring</md-radio-button>
<md-radio-button name="weather" value="hot">Summer</md-radio-button>
<md-radio-button name="weather" value="cool">Autumn</md-radio-button>

<span id="xyz">Baby Banana<span>
<md-radio-button name="fruit" value="banana" aria-label="Banana" aria-labelledby="xyz">
</md-radio-button>
Expand All @@ -508,7 +501,7 @@ class StandaloneRadioButtons { }
@Component({
directives: [MD_RADIO_DIRECTIVES, FORM_DIRECTIVES],
template: `
<md-radio-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
<md-radio-group [(ngModel)]="modelValue" (valueChange)="lastEvent = $event">
<md-radio-button *ngFor="let option of options" [value]="option.value">
{{option.label}}
</md-radio-button>
Expand All @@ -522,7 +515,7 @@ class RadioGroupWithNgModel {
{label: 'Chocolate', value: 'chocolate'},
{label: 'Strawberry', value: 'strawberry'},
];
lastEvent: MdRadioChange;
lastEvent: any;
}

// TODO(jelbourn): remove eveything below when Angular supports faking events.
Expand Down
Loading