11import {
2+ it ,
3+ iit ,
24 describe ,
3- inject
5+ beforeEach ,
6+ beforeEachProviders ,
7+ inject ,
8+ async ,
9+ fakeAsync ,
10+ tick ,
411} from '@angular/core/testing' ;
5- import { TestComponentBuilder } from '@angular/compiler/testing' ;
6- import { Component } from '@angular/core' ;
12+ import { TestComponentBuilder , ComponentFixture } from '@angular/compiler/testing' ;
13+ import { Component , DebugElement , provide } from '@angular/core' ;
14+ import { By } from '@angular/platform-browser' ;
715import { MdSlider } from './slider' ;
16+ import { HAMMER_GESTURE_CONFIG } from '@angular/platform-browser' ;
17+ import { TestGestureConfig } from './test-gesture-config' ;
818
919describe ( 'MdSlider' , ( ) => {
1020 let builder : TestComponentBuilder ;
21+ let gestureConfig : TestGestureConfig ;
22+
23+ beforeEachProviders ( ( ) => [
24+ provide ( HAMMER_GESTURE_CONFIG , { useFactory : ( ) => {
25+ gestureConfig = new TestGestureConfig ( ) ;
26+ return gestureConfig ;
27+ } } )
28+ ] ) ;
1129
1230 beforeEach ( inject ( [ TestComponentBuilder ] , ( tcb : TestComponentBuilder ) => {
1331 builder = tcb ;
1432 } ) ) ;
1533
1634 describe ( 'standard slider' , ( ) => {
35+ let fixture : ComponentFixture < StandardSlider > ;
36+ let sliderDebugElement : DebugElement ;
37+ let sliderNativeElement : HTMLElement ;
38+ let sliderInstance : MdSlider ;
39+ let testComponent : StandardSlider ;
40+
41+ beforeEach ( async ( ( ) => {
42+ builder . createAsync ( StandardSlider ) . then ( f => {
43+ fixture = f ;
44+ fixture . detectChanges ( ) ;
45+
46+ testComponent = fixture . debugElement . componentInstance ;
47+
48+ sliderDebugElement = fixture . debugElement . query ( By . directive ( MdSlider ) ) ;
49+ sliderNativeElement = sliderDebugElement . nativeElement ;
50+ sliderInstance = sliderDebugElement . injector . get ( MdSlider ) ;
51+ } ) ;
52+ } ) ) ;
53+
54+ it ( 'should set the default values' , ( ) => {
55+ expect ( sliderInstance . value ) . toBe ( 0 ) ;
56+ expect ( sliderInstance . min ) . toBe ( 0 ) ;
57+ expect ( sliderInstance . max ) . toBe ( 100 ) ;
58+ } ) ;
59+
60+ it ( 'should update the value on a click' , ( ) => {
61+ expect ( sliderInstance . value ) . toBe ( 0 ) ;
62+ dispatchClickEvent ( sliderNativeElement , 0.5 ) ;
63+ // 50% is the same as the value 50 in this case (0 -> 100, middle = 50).
64+ expect ( sliderInstance . value ) . toBe ( 50 ) ;
65+ } ) ;
66+
67+ it ( 'should update the value on a drag' , ( ) => {
68+ expect ( sliderInstance . value ) . toBe ( 0 ) ;
69+ dispatchDragEvent ( sliderNativeElement , 0 , 0.5 , gestureConfig ) ;
70+ expect ( sliderInstance . value ) . toBe ( 50 ) ;
71+ } ) ;
72+
73+ iit ( 'should update the track fill on click' , fakeAsync ( ( ) => {
74+ let trackFillDimensions =
75+ sliderNativeElement . querySelector ( '.md-slider-track-fill' ) . getBoundingClientRect ( ) ;
76+ let sliderDimensions =
77+ sliderNativeElement . querySelector ( '.md-slider-track' ) . getBoundingClientRect ( ) ;
78+ expect ( trackFillDimensions . width ) . toBe ( 0 ) ;
79+ dispatchClickEvent ( sliderNativeElement , 0.5 ) ;
80+ fixture . detectChanges ( ) ;
81+ tick ( ) ;
82+ tick ( ) ;
83+
84+ let test = sliderNativeElement . querySelector ( '.md-slider-track-fill' ) ;
85+ let testDimensions = test . getBoundingClientRect ( ) ;
86+
87+ console . log ( testDimensions . width ) ;
88+ expect ( testDimensions . width ) . toBe ( sliderDimensions . width * 0.5 ) ;
89+ } ) ) ;
90+ } ) ;
91+
92+ describe ( 'min max disabled slider' , ( ) => {
93+ let fixture : ComponentFixture < MinMaxDisabledSlider > ;
94+ let sliderDebugElement : DebugElement ;
95+ let sliderNativeElement : HTMLElement ;
96+ let sliderInstance : MdSlider ;
97+ let testComponent : MinMaxDisabledSlider ;
98+
99+ beforeEach ( async ( ( ) => {
100+ builder . createAsync ( MinMaxDisabledSlider ) . then ( f => {
101+ fixture = f ;
102+ fixture . detectChanges ( ) ;
103+
104+ testComponent = fixture . debugElement . componentInstance ;
105+
106+ sliderDebugElement = fixture . debugElement . query ( By . directive ( MdSlider ) ) ;
107+ sliderNativeElement = sliderDebugElement . nativeElement ;
108+ sliderInstance = sliderDebugElement . injector . get ( MdSlider ) ;
109+ } ) ;
110+ } ) ) ;
111+
112+ it ( 'should set the default values from the attributes' , ( ) => {
113+ expect ( sliderInstance . value ) . toBe ( 5 ) ;
114+ expect ( sliderInstance . min ) . toBe ( 5 ) ;
115+ expect ( sliderInstance . max ) . toBe ( 15 ) ;
116+ expect ( sliderInstance . disabled ) . toBeTruthy ( ) ;
117+ } ) ;
118+
119+ it ( 'should not change the value on click when disabled' , ( ) => {
120+ expect ( sliderInstance . value ) . toBe ( 5 ) ;
121+ dispatchClickEvent ( sliderNativeElement , 0.5 ) ;
122+ expect ( sliderInstance . value ) . toBe ( 5 ) ;
123+ } ) ;
124+
125+ it ( 'should not change the value on drag when disabled' , ( ) => {
126+ expect ( sliderInstance . value ) . toBe ( 5 ) ;
127+ dispatchDragEvent ( sliderNativeElement , 0 , 0.5 , gestureConfig ) ;
128+ expect ( sliderInstance . value ) . toBe ( 5 ) ;
129+ } ) ;
17130 } ) ;
18131} ) ;
19132
@@ -25,3 +138,58 @@ describe('MdSlider', () => {
25138 `
26139} )
27140class StandardSlider { }
141+
142+ @Component ( {
143+ directives : [ [ MdSlider ] ] ,
144+ template : `
145+ <md-slider min="5" max="15" disabled></md-slider>
146+ `
147+ } )
148+ class MinMaxDisabledSlider { }
149+
150+ /**
151+ * Dispatches a click event from an element.
152+ * @param element The element from which the event will be dispatched.
153+ * @param percentage The percentage of the slider where the click should occur. Used to find the
154+ * physical location of the click.
155+ */
156+ function dispatchClickEvent ( element : HTMLElement , percentage : number ) : void {
157+ let dimensions = element . getBoundingClientRect ( ) ;
158+ let width = dimensions . width ;
159+ let left = dimensions . left ;
160+ let y = dimensions . top ;
161+ let x = left + ( width * percentage ) ;
162+
163+ let event = new MouseEvent ( 'click' , {
164+ clientX : x ,
165+ clientY : y
166+ } ) ;
167+ element . dispatchEvent ( event ) ;
168+ }
169+
170+ /**
171+ * Dispatches a drag event from an element.
172+ * @param element The element from which the event will be dispatched.
173+ * @param startPercent The percentage of the slider where the drag will begin.
174+ * @param endPercent The percentage of the slider where the drag will end.
175+ * @param gestureConfig The gesture config for the test to handle emitting the drag events.
176+ */
177+ function dispatchDragEvent ( element : HTMLElement , startPercent : number , endPercent : number ,
178+ gestureConfig : TestGestureConfig ) : void {
179+ let dimensions = element . getBoundingClientRect ( ) ;
180+ let width = dimensions . width ;
181+ let left = dimensions . left ;
182+ let startX = left + ( width * startPercent ) ;
183+ let endX = left + ( width * endPercent ) ;
184+
185+ gestureConfig . emitEventForElement ( 'dragstart' , element , {
186+ // The actual event has a center with an x value that the drag listener is looking for.
187+ center : { x : startX } ,
188+ // The event needs a source event with a prevent default so we fake one.
189+ srcEvent : { preventDefault : jasmine . createSpy ( 'preventDefault' ) }
190+ } ) ;
191+ gestureConfig . emitEventForElement ( 'drag' , element , {
192+ center : { x : endX } ,
193+ srcEvent : { preventDefault : jasmine . createSpy ( 'preventDefault' ) }
194+ } ) ;
195+ }
0 commit comments