|
| 1 | +/** |
| 2 | + * Use of this source code is governed by an MIT-style license that can be |
| 3 | + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE |
| 4 | + */ |
| 5 | + |
| 6 | +import { ANIMATION_MODULE_TYPE, Component, signal } from '@angular/core'; |
| 7 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 8 | +import { provideNoopAnimations } from '@angular/platform-browser/animations'; |
| 9 | + |
| 10 | +import { |
| 11 | + NZ_NO_ANIMATION_CLASS, |
| 12 | + NzNoAnimationDirective, |
| 13 | + provideNzNoAnimation, |
| 14 | + withAnimationCheck |
| 15 | +} from './no-animation'; |
| 16 | + |
| 17 | +@Component({ |
| 18 | + template: `<div #element [nzNoAnimation]="noAnimation()" data-testid="test-element"></div>`, |
| 19 | + imports: [NzNoAnimationDirective] |
| 20 | +}) |
| 21 | +class TestComponent { |
| 22 | + noAnimation = signal(false); |
| 23 | + animationClass = withAnimationCheck(() => 'animation-class'); |
| 24 | +} |
| 25 | + |
| 26 | +describe('NzNoAnimationDirective', () => { |
| 27 | + let fixture: ComponentFixture<TestComponent>; |
| 28 | + let component: TestComponent; |
| 29 | + |
| 30 | + describe('without NoopAnimations', () => { |
| 31 | + beforeEach(() => { |
| 32 | + TestBed.configureTestingModule({}); |
| 33 | + fixture = TestBed.createComponent(TestComponent); |
| 34 | + component = fixture.componentInstance; |
| 35 | + fixture.detectChanges(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should not add animation disabled class when nzNoAnimation is false', () => { |
| 39 | + const element = fixture.nativeElement.querySelector('[data-testid="test-element"]'); |
| 40 | + expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeFalse(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should add animation disabled class when nzNoAnimation is true', async () => { |
| 44 | + component.noAnimation.set(true); |
| 45 | + await fixture.whenStable(); |
| 46 | + const element = fixture.nativeElement.querySelector('[data-testid="test-element"]'); |
| 47 | + expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue(); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('with NoopAnimations', () => { |
| 52 | + beforeEach(() => { |
| 53 | + TestBed.configureTestingModule({ |
| 54 | + providers: [provideNoopAnimations()] |
| 55 | + }); |
| 56 | + fixture = TestBed.createComponent(TestComponent); |
| 57 | + component = fixture.componentInstance; |
| 58 | + fixture.detectChanges(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should add animation disabled class even when nzNoAnimation is false', () => { |
| 62 | + const element = fixture.nativeElement.querySelector('[data-testid="test-element"]'); |
| 63 | + expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should keep animation disabled class when nzNoAnimation is true', async () => { |
| 67 | + component.noAnimation.set(true); |
| 68 | + await fixture.whenStable(); |
| 69 | + const element = fixture.nativeElement.querySelector('[data-testid="test-element"]'); |
| 70 | + expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue(); |
| 71 | + }); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('provideNzNoAnimation', () => { |
| 76 | + beforeEach(() => { |
| 77 | + TestBed.configureTestingModule({ |
| 78 | + providers: [provideNzNoAnimation()] |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should provide NoopAnimations animation type', () => { |
| 83 | + const animationType = TestBed.inject(ANIMATION_MODULE_TYPE); |
| 84 | + expect(animationType).toBe('NoopAnimations'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should disable animations globally when used', () => { |
| 88 | + const fixture = TestBed.createComponent(TestComponent); |
| 89 | + fixture.detectChanges(); |
| 90 | + |
| 91 | + const element = fixture.nativeElement.querySelector('[data-testid="test-element"]'); |
| 92 | + expect(element.classList.contains(NZ_NO_ANIMATION_CLASS)).toBeTrue(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('withAnimationCheck', () => { |
| 97 | + it('should return dynamic class when animations are enabled', () => { |
| 98 | + TestBed.configureTestingModule({}); |
| 99 | + const fixture = TestBed.createComponent(TestComponent); |
| 100 | + fixture.detectChanges(); |
| 101 | + |
| 102 | + expect(fixture.componentInstance.animationClass()).toBe('animation-class'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should return no-animation class with provideNoopAnimations', () => { |
| 106 | + TestBed.configureTestingModule({ |
| 107 | + providers: [provideNoopAnimations()] |
| 108 | + }); |
| 109 | + const fixture = TestBed.createComponent(TestComponent); |
| 110 | + fixture.detectChanges(); |
| 111 | + |
| 112 | + expect(fixture.componentInstance.animationClass()).toBe(NZ_NO_ANIMATION_CLASS); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should return no-animation class with provideNzNoAnimation', () => { |
| 116 | + TestBed.configureTestingModule({ |
| 117 | + providers: [provideNzNoAnimation()] |
| 118 | + }); |
| 119 | + const fixture = TestBed.createComponent(TestComponent); |
| 120 | + fixture.detectChanges(); |
| 121 | + |
| 122 | + expect(fixture.componentInstance.animationClass()).toBe(NZ_NO_ANIMATION_CLASS); |
| 123 | + }); |
| 124 | + |
| 125 | + describe('injection context validation', () => { |
| 126 | + it('should throw error when called outside injection context in dev mode', () => { |
| 127 | + const originalDevMode = (globalThis as { ngDevMode?: boolean }).ngDevMode; |
| 128 | + (globalThis as { ngDevMode?: boolean }).ngDevMode = true; |
| 129 | + |
| 130 | + expect(() => { |
| 131 | + withAnimationCheck(() => 'test-class'); |
| 132 | + }).toThrow(); |
| 133 | + |
| 134 | + (globalThis as { ngDevMode?: boolean }).ngDevMode = originalDevMode; |
| 135 | + }); |
| 136 | + }); |
| 137 | +}); |
0 commit comments