From 9b45681fcd014318b343f9bed13b70169416c440 Mon Sep 17 00:00:00 2001 From: Paul Gschwendtner Date: Mon, 31 Jul 2017 20:06:04 +0200 Subject: [PATCH] test(slide-toggle): verify that NgControl stays pristine if initial value is set (#4970) * If a developer sets an initial value for the slide-toggle the NgControl of the slide-toggle can turn dirty. The slide-toggle should only turn dirty if the value changed after initialization. This adds a test to confirm that this is not the case. --- src/lib/slide-toggle/slide-toggle.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/lib/slide-toggle/slide-toggle.spec.ts b/src/lib/slide-toggle/slide-toggle.spec.ts index 9dab8dee493..981aaa5c27a 100644 --- a/src/lib/slide-toggle/slide-toggle.spec.ts +++ b/src/lib/slide-toggle/slide-toggle.spec.ts @@ -631,6 +631,21 @@ describe('MdSlideToggle with forms', () => { expect(slideToggle.checked) .toBe(false, 'Expected slide-toggle to be no longer checked after label click.'); })); + + it('should be pristine if initial value is set from NgModel', fakeAsync(() => { + fixture = TestBed.createComponent(SlideToggleWithModel); + + fixture.componentInstance.modelValue = true; + fixture.detectChanges(); + + const debugElement = fixture.debugElement.query(By.directive(MdSlideToggle)); + const modelInstance = debugElement.injector.get(NgModel); + + // Flush the microtasks because the forms module updates the model state asynchronously. + flushMicrotasks(); + + expect(modelInstance.pristine).toBe(true); + })); }); describe('with a FormControl', () => {