@@ -33,11 +33,19 @@ describe('MdSlideToggle', () => {
33
33
beforeEach ( async ( ( ) => {
34
34
builder . createAsync ( SlideToggleTestApp ) . then ( f => {
35
35
fixture = f ;
36
+
37
+ testComponent = fixture . debugElement . componentInstance ;
38
+
39
+ // Enable jasmine spies on event functions, which may trigger at initialization
40
+ // of the slide-toggle component.
41
+ spyOn ( fixture . debugElement . componentInstance , 'onSlideChange' ) . and . callThrough ( ) ;
42
+ spyOn ( fixture . debugElement . componentInstance , 'onSlideClick' ) . and . callThrough ( ) ;
43
+
44
+ // Initialize the slide-toggle component, by triggering the first change detection cycle.
36
45
fixture . detectChanges ( ) ;
37
46
38
47
let slideToggleDebug = fixture . debugElement . query ( By . css ( 'md-slide-toggle' ) ) ;
39
48
40
- testComponent = fixture . debugElement . componentInstance ;
41
49
slideToggle = slideToggleDebug . componentInstance ;
42
50
slideToggleElement = slideToggleDebug . nativeElement ;
43
51
slideToggleControl = slideToggleDebug . injector . get ( NgControl ) ;
@@ -103,8 +111,6 @@ describe('MdSlideToggle', () => {
103
111
// Since we're using a label element and a visual hidden input, this behavior can led
104
112
// to an issue, where the click events on the slide-toggle are getting executed twice.
105
113
106
- spyOn ( testComponent , 'onSlideClick' ) ;
107
-
108
114
expect ( slideToggle . checked ) . toBe ( false ) ;
109
115
expect ( slideToggleElement . classList ) . not . toContain ( 'md-checked' ) ;
110
116
@@ -117,6 +123,42 @@ describe('MdSlideToggle', () => {
117
123
expect ( testComponent . onSlideClick ) . toHaveBeenCalledTimes ( 1 ) ;
118
124
} ) ;
119
125
126
+ it ( 'should not trigger the change event multiple times' , async ( ( ) => {
127
+ expect ( inputElement . checked ) . toBe ( false ) ;
128
+ expect ( slideToggleElement . classList ) . not . toContain ( 'md-checked' ) ;
129
+
130
+ testComponent . slideChecked = true ;
131
+ fixture . detectChanges ( ) ;
132
+
133
+ expect ( inputElement . checked ) . toBe ( true ) ;
134
+ expect ( slideToggleElement . classList ) . toContain ( 'md-checked' ) ;
135
+
136
+ // Wait for the fixture to become stable, because the EventEmitter for the change event,
137
+ // will only fire after the zone async change detection has finished.
138
+ fixture . whenStable ( ) . then ( ( ) => {
139
+ expect ( testComponent . onSlideChange ) . toHaveBeenCalledTimes ( 1 ) ;
140
+ } ) ;
141
+
142
+ } ) ) ;
143
+
144
+ it ( 'should not trigger the change event on initialization' , async ( ( ) => {
145
+ expect ( inputElement . checked ) . toBe ( false ) ;
146
+ expect ( slideToggleElement . classList ) . not . toContain ( 'md-checked' ) ;
147
+
148
+ testComponent . slideChecked = true ;
149
+ fixture . detectChanges ( ) ;
150
+
151
+ expect ( inputElement . checked ) . toBe ( true ) ;
152
+ expect ( slideToggleElement . classList ) . toContain ( 'md-checked' ) ;
153
+
154
+ // Wait for the fixture to become stable, because the EventEmitter for the change event,
155
+ // will only fire after the zone async change detection has finished.
156
+ fixture . whenStable ( ) . then ( ( ) => {
157
+ expect ( testComponent . onSlideChange ) . toHaveBeenCalledTimes ( 1 ) ;
158
+ } ) ;
159
+
160
+ } ) ) ;
161
+
120
162
it ( 'should add a suffix to the inputs id' , ( ) => {
121
163
testComponent . slideId = 'myId' ;
122
164
fixture . detectChanges ( ) ;
@@ -269,6 +311,56 @@ describe('MdSlideToggle', () => {
269
311
270
312
} ) ;
271
313
314
+ describe ( 'custom template' , ( ) => {
315
+
316
+ let testComponent : SlideToggleTestApp ;
317
+ let slideToggle : MdSlideToggle ;
318
+ let slideToggleElement : HTMLElement ;
319
+ let labelElement : HTMLLabelElement ;
320
+ let inputElement : HTMLInputElement ;
321
+
322
+ it ( 'should not trigger the change event on initialization' , async ( ( ) => {
323
+ builder
324
+ . overrideTemplate ( SlideToggleTestApp , `
325
+ <md-slide-toggle checked="true" (change)="onSlideChange($event)"></md-slide-toggle>
326
+ ` )
327
+ . createAsync ( SlideToggleTestApp )
328
+ . then ( fixture => {
329
+ // Initialize the variables for our test.
330
+ initializeTest ( fixture ) ;
331
+
332
+ // Enable jasmine spies on event functions, which may trigger at initialization
333
+ // of the slide-toggle component.
334
+ spyOn ( fixture . debugElement . componentInstance , 'onSlideChange' ) . and . callThrough ( ) ;
335
+
336
+ fixture . detectChanges ( ) ;
337
+
338
+ fixture . whenStable ( ) . then ( ( ) => {
339
+ expect ( testComponent . onSlideChange ) . not . toHaveBeenCalled ( ) ;
340
+ } ) ;
341
+ } ) ;
342
+ } ) ) ;
343
+
344
+ /**
345
+ * Initializes the suites variables, to allow developers to easily access the several variables
346
+ * without loading / querying them always again.
347
+ * @param fixture Custom fixture, which contains the slide-toggle component.
348
+ */
349
+ function initializeTest ( fixture : ComponentFixture < any > ) {
350
+ testComponent = fixture . debugElement . componentInstance ;
351
+
352
+ // Initialize the slide-toggle component, by triggering the first change detection cycle.
353
+ fixture . detectChanges ( ) ;
354
+
355
+ let slideToggleDebug = fixture . debugElement . query ( By . css ( 'md-slide-toggle' ) ) ;
356
+
357
+ slideToggle = slideToggleDebug . componentInstance ;
358
+ slideToggleElement = slideToggleDebug . nativeElement ;
359
+ inputElement = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
360
+ labelElement = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
361
+ }
362
+ } ) ;
363
+
272
364
} ) ;
273
365
274
366
/**
@@ -288,7 +380,7 @@ function dispatchFocusChangeEvent(eventName: string, element: HTMLElement): void
288
380
<md-slide-toggle [(ngModel)]="slideModel" [disabled]="isDisabled" [color]="slideColor"
289
381
[id]="slideId" [checked]="slideChecked" [name]="slideName"
290
382
[aria-label]="slideLabel" [ariaLabel]="slideLabel"
291
- [ariaLabelledby]="slideLabelledBy" (change)="lastEvent = $event"
383
+ [ariaLabelledby]="slideLabelledBy" (change)="onSlideChange( $event) "
292
384
(click)="onSlideClick($event)">
293
385
<span>Test Slide Toggle</span>
294
386
</md-slide-toggle>
@@ -307,4 +399,7 @@ class SlideToggleTestApp {
307
399
lastEvent : MdSlideToggleChange ;
308
400
309
401
onSlideClick ( event : Event ) { }
402
+ onSlideChange ( event : MdSlideToggleChange ) {
403
+ this . lastEvent = event ;
404
+ }
310
405
}
0 commit comments