|
| 1 | +import { Component, ViewChild } from '@angular/core'; |
| 2 | +import { TestBed, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { By } from '@angular/platform-browser'; |
| 4 | +import { IgxRippleDirective } from './ripple.directive'; |
| 5 | +import { IgxButtonDirective } from '../button/button.directive'; |
| 6 | + |
| 7 | +describe('IgxRipple', () => { |
| 8 | + beforeEach(waitForAsync(() => { |
| 9 | + TestBed.configureTestingModule({ |
| 10 | + imports: [ |
| 11 | + RippleButtonComponent, |
| 12 | + RippleDisabledComponent, |
| 13 | + RippleCenteredComponent, |
| 14 | + RippleColorComponent, |
| 15 | + RippleTargetComponent |
| 16 | + ] |
| 17 | + }).compileComponents(); |
| 18 | + })); |
| 19 | + |
| 20 | + it('Should initialize ripple directive on button', () => { |
| 21 | + const fixture = TestBed.createComponent(RippleButtonComponent); |
| 22 | + fixture.detectChanges(); |
| 23 | + |
| 24 | + const button = fixture.debugElement.query(By.css('button')); |
| 25 | + const rippleDirective = button.injector.get(IgxRippleDirective); |
| 26 | + |
| 27 | + expect(rippleDirective).toBeTruthy(); |
| 28 | + expect(button.nativeElement).toBeTruthy(); |
| 29 | + }); |
| 30 | + |
| 31 | + it('Should not affect host element size when ripple is triggered without CSS styles', () => { |
| 32 | + const fixture = TestBed.createComponent(RippleButtonComponent); |
| 33 | + fixture.detectChanges(); |
| 34 | + |
| 35 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 36 | + const button = buttonDebug.nativeElement; |
| 37 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 38 | + |
| 39 | + // Set explicit dimensions to ensure we can measure them |
| 40 | + button.style.width = '100px'; |
| 41 | + button.style.height = '40px'; |
| 42 | + button.style.padding = '10px'; |
| 43 | + fixture.detectChanges(); |
| 44 | + |
| 45 | + const initialWidth = button.offsetWidth; |
| 46 | + const initialHeight = button.offsetHeight; |
| 47 | + |
| 48 | + expect(initialWidth).toBeGreaterThan(0); |
| 49 | + expect(initialHeight).toBeGreaterThan(0); |
| 50 | + |
| 51 | + const setStylesSpy = spyOn<any>(rippleDirective, 'setStyles').and.callThrough(); |
| 52 | + const rect = button.getBoundingClientRect(); |
| 53 | + const mouseEvent = new MouseEvent('mousedown', { |
| 54 | + clientX: rect.left + 50, |
| 55 | + clientY: rect.top + 20, |
| 56 | + bubbles: true |
| 57 | + }); |
| 58 | + |
| 59 | + rippleDirective.onMouseDown(mouseEvent); |
| 60 | + |
| 61 | + expect(setStylesSpy).toHaveBeenCalled(); |
| 62 | + |
| 63 | + const rippleElement = setStylesSpy.calls.mostRecent().args[0] as HTMLElement; |
| 64 | + |
| 65 | + expect(rippleElement.style.position).toBe('absolute'); |
| 66 | + |
| 67 | + const afterWidth = button.offsetWidth; |
| 68 | + const afterHeight = button.offsetHeight; |
| 69 | + |
| 70 | + expect(afterWidth).toBe(initialWidth); |
| 71 | + expect(afterHeight).toBe(initialHeight); |
| 72 | + }); |
| 73 | + |
| 74 | + it('Should create ripple element with position absolute style', () => { |
| 75 | + const fixture = TestBed.createComponent(RippleButtonComponent); |
| 76 | + fixture.detectChanges(); |
| 77 | + |
| 78 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 79 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 80 | + const setStyleSpy = spyOn(rippleDirective['renderer'], 'setStyle').and.callThrough(); |
| 81 | + const button = buttonDebug.nativeElement; |
| 82 | + const rect = button.getBoundingClientRect(); |
| 83 | + const mouseEvent = new MouseEvent('mousedown', { |
| 84 | + clientX: rect.left + 10, |
| 85 | + clientY: rect.top + 10, |
| 86 | + bubbles: true |
| 87 | + }); |
| 88 | + |
| 89 | + rippleDirective.onMouseDown(mouseEvent); |
| 90 | + |
| 91 | + const positionStyleCall = setStyleSpy.calls.all().find(call => |
| 92 | + call.args[1] === 'position' && call.args[2] === 'absolute' |
| 93 | + ); |
| 94 | + expect(positionStyleCall).toBeTruthy(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('Should not create ripple when disabled', () => { |
| 98 | + const fixture = TestBed.createComponent(RippleDisabledComponent); |
| 99 | + fixture.detectChanges(); |
| 100 | + |
| 101 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 102 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 103 | + const setStylesSpy = spyOn<any>(rippleDirective, 'setStyles'); |
| 104 | + const button = buttonDebug.nativeElement; |
| 105 | + const rect = button.getBoundingClientRect(); |
| 106 | + const mouseEvent = new MouseEvent('mousedown', { |
| 107 | + clientX: rect.left + 10, |
| 108 | + clientY: rect.top + 10, |
| 109 | + bubbles: true |
| 110 | + }); |
| 111 | + |
| 112 | + rippleDirective.onMouseDown(mouseEvent); |
| 113 | + expect(setStylesSpy).not.toHaveBeenCalled(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('Should apply custom ripple color as background style', () => { |
| 117 | + const fixture = TestBed.createComponent(RippleColorComponent); |
| 118 | + fixture.detectChanges(); |
| 119 | + |
| 120 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 121 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 122 | + const setStyleSpy = spyOn(rippleDirective['renderer'], 'setStyle').and.callThrough(); |
| 123 | + const button = buttonDebug.nativeElement; |
| 124 | + const rect = button.getBoundingClientRect(); |
| 125 | + const mouseEvent = new MouseEvent('mousedown', { |
| 126 | + clientX: rect.left + 10, |
| 127 | + clientY: rect.top + 10, |
| 128 | + bubbles: true |
| 129 | + }); |
| 130 | + |
| 131 | + rippleDirective.onMouseDown(mouseEvent); |
| 132 | + |
| 133 | + const backgroundStyleCall = setStyleSpy.calls.all().find(call => |
| 134 | + call.args[1] === 'background' && call.args[2] === 'red' |
| 135 | + ); |
| 136 | + expect(backgroundStyleCall).toBeTruthy(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('Should center ripple when igxRippleCentered is true', () => { |
| 140 | + const fixture = TestBed.createComponent(RippleCenteredComponent); |
| 141 | + fixture.detectChanges(); |
| 142 | + |
| 143 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 144 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 145 | + const setStyleSpy = spyOn(rippleDirective['renderer'], 'setStyle').and.callThrough(); |
| 146 | + const button = buttonDebug.nativeElement; |
| 147 | + const rect = button.getBoundingClientRect(); |
| 148 | + const mouseEvent = new MouseEvent('mousedown', { |
| 149 | + clientX: rect.left + 50, |
| 150 | + clientY: rect.top + 50, |
| 151 | + bubbles: true |
| 152 | + }); |
| 153 | + |
| 154 | + rippleDirective.onMouseDown(mouseEvent); |
| 155 | + |
| 156 | + const topStyleCall = setStyleSpy.calls.all().find(call => |
| 157 | + call.args[1] === 'top' && call.args[2] === '0px' |
| 158 | + ); |
| 159 | + const leftStyleCall = setStyleSpy.calls.all().find(call => |
| 160 | + call.args[1] === 'left' && call.args[2] === '0px' |
| 161 | + ); |
| 162 | + |
| 163 | + expect(topStyleCall).toBeTruthy(); |
| 164 | + expect(leftStyleCall).toBeTruthy(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('Should apply ripple to target element when igxRippleTarget is specified', () => { |
| 168 | + const fixture = TestBed.createComponent(RippleTargetComponent); |
| 169 | + fixture.detectChanges(); |
| 170 | + |
| 171 | + const containerDebug = fixture.debugElement.query(By.css('.container')); |
| 172 | + const rippleDirective = containerDebug.injector.get(IgxRippleDirective); |
| 173 | + const targetButton = fixture.debugElement.query(By.css('#target')).nativeElement; |
| 174 | + const appendChildSpy = spyOn(rippleDirective['renderer'], 'appendChild').and.callThrough(); |
| 175 | + const container = containerDebug.nativeElement; |
| 176 | + const rect = container.getBoundingClientRect(); |
| 177 | + const mouseEvent = new MouseEvent('mousedown', { |
| 178 | + clientX: rect.left + 10, |
| 179 | + clientY: rect.top + 10, |
| 180 | + bubbles: true |
| 181 | + }); |
| 182 | + rippleDirective.onMouseDown(mouseEvent); |
| 183 | + |
| 184 | + const appendCall = appendChildSpy.calls.mostRecent(); |
| 185 | + expect(appendCall.args[0]).toBe(targetButton); |
| 186 | + }); |
| 187 | + |
| 188 | + it('Should set all required ripple element styles including position absolute', () => { |
| 189 | + const fixture = TestBed.createComponent(RippleButtonComponent); |
| 190 | + fixture.detectChanges(); |
| 191 | + |
| 192 | + const buttonDebug = fixture.debugElement.query(By.css('button')); |
| 193 | + const button = buttonDebug.nativeElement; |
| 194 | + const rippleDirective = buttonDebug.injector.get(IgxRippleDirective); |
| 195 | + |
| 196 | + button.style.width = '100px'; |
| 197 | + button.style.height = '50px'; |
| 198 | + |
| 199 | + const setStyleSpy = spyOn(rippleDirective['renderer'], 'setStyle').and.callThrough(); |
| 200 | + const rect = button.getBoundingClientRect(); |
| 201 | + const mouseEvent = new MouseEvent('mousedown', { |
| 202 | + clientX: rect.left + 25, |
| 203 | + clientY: rect.top + 25, |
| 204 | + bubbles: true |
| 205 | + }); |
| 206 | + rippleDirective.onMouseDown(mouseEvent); |
| 207 | + |
| 208 | + const styleCalls = setStyleSpy.calls.all(); |
| 209 | + const styles = styleCalls.map(call => call.args[1]); |
| 210 | + |
| 211 | + expect(styles).toContain('position'); |
| 212 | + expect(styles).toContain('width'); |
| 213 | + expect(styles).toContain('height'); |
| 214 | + expect(styles).toContain('top'); |
| 215 | + expect(styles).toContain('left'); |
| 216 | + |
| 217 | + const positionCall = styleCalls.find(call => call.args[1] === 'position'); |
| 218 | + expect(positionCall.args[2]).toBe('absolute'); |
| 219 | + }); |
| 220 | +}); |
| 221 | + |
| 222 | +@Component({ |
| 223 | + template: `<button igxButton igxRipple>Test Button</button>`, |
| 224 | + imports: [IgxButtonDirective, IgxRippleDirective], |
| 225 | + standalone: true |
| 226 | +}) |
| 227 | +class RippleButtonComponent { |
| 228 | + @ViewChild(IgxRippleDirective, { static: true }) |
| 229 | + public ripple: IgxRippleDirective; |
| 230 | +} |
| 231 | + |
| 232 | +@Component({ |
| 233 | + template: `<button igxButton igxRipple [igxRippleDisabled]="true">Disabled Ripple</button>`, |
| 234 | + imports: [IgxButtonDirective, IgxRippleDirective], |
| 235 | + standalone: true |
| 236 | +}) |
| 237 | +class RippleDisabledComponent { } |
| 238 | + |
| 239 | +@Component({ |
| 240 | + template: `<button igxButton igxRipple [igxRippleCentered]="true">Centered Ripple</button>`, |
| 241 | + imports: [IgxButtonDirective, IgxRippleDirective], |
| 242 | + standalone: true |
| 243 | +}) |
| 244 | +class RippleCenteredComponent { } |
| 245 | + |
| 246 | +@Component({ |
| 247 | + template: `<button igxButton [igxRipple]="'red'">Colored Ripple</button>`, |
| 248 | + imports: [IgxButtonDirective, IgxRippleDirective], |
| 249 | + standalone: true |
| 250 | +}) |
| 251 | +class RippleColorComponent { } |
| 252 | + |
| 253 | +@Component({ |
| 254 | + template: ` |
| 255 | + <div class="container" igxRipple [igxRippleTarget]="'#target'"> |
| 256 | + <button id="target" igxButton>Target Button</button> |
| 257 | + </div> |
| 258 | + `, |
| 259 | + imports: [IgxButtonDirective, IgxRippleDirective], |
| 260 | + standalone: true |
| 261 | +}) |
| 262 | +class RippleTargetComponent { } |
0 commit comments