1
1
import { TestBed , ComponentFixture , fakeAsync , tick , inject } from '@angular/core/testing' ;
2
2
import { Component , ViewChild } from '@angular/core' ;
3
- import { MdRipple , MdRippleModule , MD_DISABLE_RIPPLES , RippleState } from './index' ;
4
3
import { ViewportRuler } from '../overlay/position/viewport-ruler' ;
5
4
import { RIPPLE_FADE_OUT_DURATION , RIPPLE_FADE_IN_DURATION } from './ripple-renderer' ;
6
5
import { dispatchMouseEvent } from '../testing/dispatch-events' ;
6
+ import {
7
+ MdRipple , MdRippleModule , MD_RIPPLE_GLOBAL_OPTIONS , RippleState , RippleGlobalOptions
8
+ } from './index' ;
7
9
8
10
/** Extracts the numeric value of a pixel size string like '123px'. */
9
11
const pxStringToFloat = ( s : string ) => {
@@ -346,29 +348,29 @@ describe('MdRipple', () => {
346
348
347
349
} ) ;
348
350
349
- describe ( 'with ripples disabled ' , ( ) => {
351
+ describe ( 'global ripple options ' , ( ) => {
350
352
let rippleDirective : MdRipple ;
351
353
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 .
354
356
// The testing module has been initialized in the root describe group for the ripples.
355
357
TestBed . resetTestingModule ( ) ;
356
358
TestBed . configureTestingModule ( {
357
359
imports : [ MdRippleModule ] ,
358
360
declarations : [ BasicRippleContainer ] ,
359
- providers : [ { provide : MD_DISABLE_RIPPLES , useValue : true } ]
361
+ providers : [ { provide : MD_RIPPLE_GLOBAL_OPTIONS , useValue : rippleConfig } ]
360
362
} ) ;
361
- } ) ;
362
363
363
- beforeEach ( ( ) => {
364
364
fixture = TestBed . createComponent ( BasicRippleContainer ) ;
365
365
fixture . detectChanges ( ) ;
366
366
367
367
rippleTarget = fixture . nativeElement . querySelector ( '[mat-ripple]' ) ;
368
368
rippleDirective = fixture . componentInstance . ripple ;
369
- } ) ;
369
+ }
370
+
371
+ it ( 'when disabled should not show any ripples on mousedown' , ( ) => {
372
+ createTestComponent ( { disabled : true } ) ;
370
373
371
- it ( 'should not show any ripples on mousedown' , ( ) => {
372
374
dispatchMouseEvent ( rippleTarget , 'mousedown' ) ;
373
375
dispatchMouseEvent ( rippleTarget , 'mouseup' ) ;
374
376
@@ -380,14 +382,51 @@ describe('MdRipple', () => {
380
382
expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
381
383
} ) ;
382
384
383
- it ( 'should still allow manual ripples' , ( ) => {
385
+ it ( 'when disabled should still allow manual ripples' , ( ) => {
386
+ createTestComponent ( { disabled : true } ) ;
387
+
384
388
expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 0 ) ;
385
389
386
390
rippleDirective . launch ( 0 , 0 ) ;
387
391
388
392
expect ( rippleTarget . querySelectorAll ( '.mat-ripple-element' ) . length ) . toBe ( 1 ) ;
389
393
} ) ;
390
394
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
+
391
430
} ) ;
392
431
393
432
describe ( 'configuring behavior' , ( ) => {
0 commit comments