@@ -6,6 +6,7 @@ import {FormControl, FormsModule, NgModel, ReactiveFormsModule} from '@angular/f
6
6
import { defaultRippleAnimationConfig } from '@angular/material/core' ;
7
7
import { By , HAMMER_GESTURE_CONFIG } from '@angular/platform-browser' ;
8
8
import { TestGestureConfig } from '../slider/test-gesture-config' ;
9
+ import { MAT_SLIDE_TOGGLE_CLICK_ACTION , MAT_SLIDE_TOGGLE_DRAG_ACTION } from './slide-toggle-config' ;
9
10
import { MatSlideToggle , MatSlideToggleChange , MatSlideToggleModule } from './index' ;
10
11
11
12
describe ( 'MatSlideToggle without forms' , ( ) => {
@@ -355,6 +356,103 @@ describe('MatSlideToggle without forms', () => {
355
356
} ) ) ;
356
357
} ) ;
357
358
359
+ describe ( 'custom action configuration' , ( ) => {
360
+ it ( 'should not change value on click when click action is noop' , fakeAsync ( ( ) => {
361
+ TestBed
362
+ . resetTestingModule ( )
363
+ . configureTestingModule ( {
364
+ imports : [ MatSlideToggleModule ] ,
365
+ declarations : [ SlideToggleBasic ] ,
366
+ providers : [
367
+ {
368
+ provide : HAMMER_GESTURE_CONFIG ,
369
+ useFactory : ( ) => gestureConfig = new TestGestureConfig ( )
370
+ } ,
371
+ { provide : MAT_SLIDE_TOGGLE_CLICK_ACTION , useValue : 'noop' } ,
372
+ {
373
+ provide : MutationObserverFactory ,
374
+ useValue : {
375
+ create : ( callback : Function ) => {
376
+ mutationObserverCallbacks . push ( callback ) ;
377
+ return { observe : ( ) => { } , disconnect : ( ) => { } } ;
378
+ }
379
+ }
380
+ }
381
+ ]
382
+ } ) ;
383
+ const fixture = TestBed . createComponent ( SlideToggleBasic ) ;
384
+ const slideToggleDebug = fixture . debugElement . query ( By . css ( 'mat-slide-toggle' ) ) ;
385
+
386
+ const slideToggle = slideToggleDebug . componentInstance ;
387
+ const inputElement = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
388
+ const labelElement = fixture . debugElement . query ( By . css ( 'label' ) ) . nativeElement ;
389
+
390
+ expect ( slideToggle . checked ) . toBe ( false , 'Expect slide toggle value not changed' ) ;
391
+
392
+ labelElement . click ( ) ;
393
+ fixture . detectChanges ( ) ;
394
+
395
+ expect ( slideToggle . checked ) . toBe ( false , 'Expect slide toggle value not changed' ) ;
396
+
397
+ inputElement . click ( ) ;
398
+ fixture . detectChanges ( ) ;
399
+
400
+ expect ( slideToggle . checked ) . toBe ( false , 'Expect slide toggle value not changed' ) ;
401
+ } ) ) ;
402
+
403
+ it ( 'should not change value on dragging when drag action is noop' , fakeAsync ( ( ) => {
404
+ TestBed
405
+ . resetTestingModule ( )
406
+ . configureTestingModule ( {
407
+ imports : [ MatSlideToggleModule ] ,
408
+ declarations : [ SlideToggleBasic ] ,
409
+ providers : [
410
+ {
411
+ provide : HAMMER_GESTURE_CONFIG ,
412
+ useFactory : ( ) => gestureConfig = new TestGestureConfig ( )
413
+ } ,
414
+ { provide : MAT_SLIDE_TOGGLE_DRAG_ACTION , useValue : 'noop' } ,
415
+ {
416
+ provide : MutationObserverFactory ,
417
+ useValue : {
418
+ create : ( callback : Function ) => {
419
+ mutationObserverCallbacks . push ( callback ) ;
420
+ return { observe : ( ) => { } , disconnect : ( ) => { } } ;
421
+ }
422
+ }
423
+ }
424
+ ]
425
+ } ) ;
426
+ const fixture = TestBed . createComponent ( SlideToggleBasic ) ;
427
+ const testComponent = fixture . debugElement . componentInstance ;
428
+ const slideToggleDebug = fixture . debugElement . query ( By . css ( 'mat-slide-toggle' ) ) ;
429
+ const thumbContainerDebug = slideToggleDebug
430
+ . query ( By . css ( '.mat-slide-toggle-thumb-container' ) ) ;
431
+
432
+ const slideThumbContainer = thumbContainerDebug . nativeElement ;
433
+ const slideToggle = slideToggleDebug . componentInstance ;
434
+
435
+ expect ( slideToggle . checked ) . toBe ( false ) ;
436
+
437
+ gestureConfig . emitEventForElement ( 'slidestart' , slideThumbContainer ) ;
438
+
439
+ expect ( slideThumbContainer . classList ) . toContain ( 'mat-dragging' ) ;
440
+
441
+ gestureConfig . emitEventForElement ( 'slide' , slideThumbContainer , {
442
+ deltaX : 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
443
+ } ) ;
444
+
445
+ gestureConfig . emitEventForElement ( 'slideend' , slideThumbContainer ) ;
446
+
447
+ // Flush the timeout for the slide ending.
448
+ tick ( ) ;
449
+
450
+ expect ( slideToggle . checked ) . toBe ( false , 'Expect slide toggle value not changed' ) ;
451
+ expect ( slideThumbContainer . classList ) . not . toContain ( 'mat-dragging' ) ;
452
+ expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
453
+ } ) ) ;
454
+ } ) ;
455
+
358
456
describe ( 'with dragging' , ( ) => {
359
457
let fixture : ComponentFixture < any > ;
360
458
0 commit comments