11import { TestBed , ComponentFixture , fakeAsync , tick , inject } from '@angular/core/testing' ;
22import { Component , ViewChild } from '@angular/core' ;
3- import { MdRipple , MdRippleModule , MD_DISABLE_RIPPLES , RippleState } from './index' ;
43import { ViewportRuler } from '../overlay/position/viewport-ruler' ;
54import { RIPPLE_FADE_OUT_DURATION , RIPPLE_FADE_IN_DURATION } from './ripple-renderer' ;
65import { dispatchMouseEvent } from '../testing/dispatch-events' ;
6+ import {
7+ MdRipple , MdRippleModule , MD_RIPPLE_GLOBAL_OPTIONS , RippleState , RippleGlobalOptions
8+ } from './index' ;
79
810/** Extracts the numeric value of a pixel size string like '123px'. */
911const pxStringToFloat = ( s : string ) => {
@@ -346,29 +348,29 @@ describe('MdRipple', () => {
346348
347349 } ) ;
348350
349- describe ( 'with ripples disabled ' , ( ) => {
351+ describe ( 'global ripple options ' , ( ) => {
350352 let rippleDirective : MdRipple ;
351353
352- beforeEach ( ( ) => {
353- // Reset the previously configured testing module to be able to disable ripples globally .
354+ function createTestComponent ( rippleConfig : RippleGlobalOptions ) {
355+ // Reset the previously configured testing module to be able set new providers .
354356 // The testing module has been initialized in the root describe group for the ripples.
355357 TestBed . resetTestingModule ( ) ;
356358 TestBed . configureTestingModule ( {
357359 imports : [ MdRippleModule ] ,
358360 declarations : [ BasicRippleContainer ] ,
359- providers : [ { provide : MD_DISABLE_RIPPLES , useValue : true } ]
361+ providers : [ { provide : MD_RIPPLE_GLOBAL_OPTIONS , useValue : rippleConfig } ]
360362 } ) ;
361- } ) ;
362363
363- beforeEach ( ( ) => {
364364 fixture = TestBed . createComponent ( BasicRippleContainer ) ;
365365 fixture . detectChanges ( ) ;
366366
367367 rippleTarget = fixture . nativeElement . querySelector ( '[mat-ripple]' ) ;
368368 rippleDirective = fixture . componentInstance . ripple ;
369- } ) ;
369+ }
370+
371+ it ( 'when disabled should not show any ripples on mousedown' , ( ) => {
372+ createTestComponent ( { disabled : true } ) ;
370373
371- it ( 'should not show any ripples on mousedown' , ( ) => {
372374 dispatchMouseEvent ( rippleTarget , 'mousedown' ) ;
373375 dispatchMouseEvent ( rippleTarget , 'mouseup' ) ;
374376
@@ -380,14 +382,51 @@ describe('MdRipple', () => {
380382 expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
381383 } ) ;
382384
383- it ( 'should still allow manual ripples' , ( ) => {
385+ it ( 'when disabled should still allow manual ripples' , ( ) => {
386+ createTestComponent ( { disabled : true } ) ;
387+
384388 expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
385389
386390 rippleDirective . launch ( 0 , 0 ) ;
387391
388392 expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
389393 } ) ;
390394
395+ it ( 'should support changing the baseSpeedFactor' , fakeAsync ( ( ) => {
396+ createTestComponent ( { baseSpeedFactor : 0.5 } ) ;
397+
398+ dispatchMouseEvent ( rippleTarget , 'mousedown' ) ;
399+ dispatchMouseEvent ( rippleTarget , 'mouseup' ) ;
400+
401+ expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
402+
403+ // Calculates the speedFactor for the duration. Those factors needs to be inverted, because
404+ // a lower speed factor, will make the duration longer. For example: 0.5 => 2x duration.
405+ let fadeInFactor = 1 / 0.5 ;
406+
407+ // Calculates the duration for fading-in and fading-out the ripple.
408+ tick ( RIPPLE_FADE_IN_DURATION * fadeInFactor + RIPPLE_FADE_OUT_DURATION ) ;
409+
410+ expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
411+ } ) ) ;
412+
413+ it ( 'should combine individual speed factor with baseSpeedFactor' , fakeAsync ( ( ) => {
414+ createTestComponent ( { baseSpeedFactor : 0.5 } ) ;
415+
416+ rippleDirective . launch ( 0 , 0 , { speedFactor : 1.5 } ) ;
417+
418+ expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
419+
420+ // Calculates the speedFactor for the duration. Those factors needs to be inverted, because
421+ // a lower speed factor, will make the duration longer. For example: 0.5 => 2x duration.
422+ let fadeInFactor = 1 / ( 0.5 * 1.5 ) ;
423+
424+ // Calculates the duration for fading-in and fading-out the ripple.
425+ tick ( RIPPLE_FADE_IN_DURATION * fadeInFactor + RIPPLE_FADE_OUT_DURATION ) ;
426+
427+ expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
428+ } ) ) ;
429+
391430 } ) ;
392431
393432 describe ( 'configuring behavior' , ( ) => {
0 commit comments