Skip to content

Commit

Permalink
fix(slide-toggle): slide-toggle module depends on forms module (#5523)
Browse files Browse the repository at this point in the history
* Currently the slide-toggle module always imports the `FormsModule` and therefore when treeshaking the `FormsModule` would be always included (even if FormsModule is not used at all)
  • Loading branch information
devversion authored and mmalerba committed Jul 9, 2017
1 parent 173cddd commit d716b00
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/lib/slide-toggle/index.ts
Expand Up @@ -7,7 +7,6 @@
*/

import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
import {MdSlideToggle} from './slide-toggle';
import {
Expand All @@ -19,7 +18,7 @@ import {
} from '../core';

@NgModule({
imports: [FormsModule, MdRippleModule, MdCommonModule, PlatformModule],
imports: [MdRippleModule, MdCommonModule, PlatformModule],
exports: [MdSlideToggle, MdCommonModule],
declarations: [MdSlideToggle],
providers: [
Expand Down
54 changes: 54 additions & 0 deletions src/lib/slide-toggle/slide-toggle.spec.ts
Expand Up @@ -26,6 +26,53 @@ describe('MdSlideToggle', () => {
TestBed.compileComponents();
}));

describe('without form modules', () => {
let fixture: ComponentFixture<SlideToggleWithoutForms>;
let slideToggleInstance: MdSlideToggle;
let labelElement: HTMLLabelElement;

beforeEach(async(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
imports: [MdSlideToggleModule],
declarations: [SlideToggleWithoutForms]
});
}));

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

const slideToggleDebug = fixture.debugElement.query(By.directive(MdSlideToggle));

slideToggleInstance = slideToggleDebug.componentInstance;
labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
});

it('should update the checked state on click', () => {
expect(slideToggleInstance.checked)
.toBe(false, 'Expected the slide-toggle not to be checked initially.');

labelElement.click();
fixture.detectChanges();

expect(slideToggleInstance.checked)
.toBe(true, 'Expected the slide-toggle to be checked after click.');
});

it('should update the checked state from binding', () => {
expect(slideToggleInstance.checked)
.toBe(false, 'Expected the slide-toggle not to be checked initially.');

fixture.componentInstance.isChecked = true;
fixture.detectChanges();

expect(slideToggleInstance.checked)
.toBe(true, 'Expected the slide-toggle to be checked after click.');
});

});

describe('basic behavior', () => {
let fixture: ComponentFixture<any>;

Expand Down Expand Up @@ -720,3 +767,10 @@ class SlideToggleFormsTestApp {
class SlideToggleWithFormControl {
formControl = new FormControl();
}

@Component({
template: `<md-slide-toggle [checked]="isChecked"></md-slide-toggle>`
})
class SlideToggleWithoutForms {
isChecked = false;
}

0 comments on commit d716b00

Please sign in to comment.