【问题标题】:Angular Jest Test Directive with @HostLinener('window:scroll')带有 @HostLinener('window:scroll') 的 Angular Jest 测试指令
【发布时间】:2021-02-12 03:12:41
【问题描述】:

问题:我想测试一个指令,该指令应该在 'window:scroll' 事件之后更改元素的样式,但不知道如何触发该事件

指令:

@Directive({
    selector: '[directiveSelector]',
})
export class Directive {
    private breakpointOffset: number;
    private fixed = false;

    constructor(
        private readonly elementRef: ElementRef,
    ) {}

    ngAfterViewInit() {
            //fix the element when scrollTop reaches past element's offsetTop
            this.breakpointOffset = this.elementRef.nativeElement.offsetTop;
    }

    @HostListener('window:scroll')
    addScrollTrigger() {
        if (!this.fixed && window.pageYOffset > this.breakpointOffset) {
            this.elementRef.nativeElement.setAttribute(
                'style',
                'position: fixed;
            );
            this.fixed = true;
        }

        if (this.fixed && window.pageYOffset <= this.breakpointOffset) {
            this.elementRef.nativeElement.setAttribute('style', '');
            this.fixed = false;
        }
    }
}

我尝试了什么:

@Component({
    template: ` <div #container directive></div> `,
})
class TestComponent {
    @ViewChild('container') container: ElementRef;
}

describe('Directive', () => {
    let fixture: ComponentFixture<TestComponent>;
    let component: TestComponent;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [TestComponent, Directive],
        }).compileComponents();

        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
    }));

    describe('addScrollTrigger', () => {
        it('sets fixed style', () => {
            //Trigger scroll event?

            expect(component.container.nativeElement.styles).toEqual(....);
        });
    });
});

在我的测试中,我尝试创建一个带有模板的组件,该模板有一个带有该指令的 div。我不能做的是触发滚动事件,因为我必须直接访问指令并且不能调用“addScrollTrigger”函数,可以吗?

【问题讨论】:

    标签: angular testing scroll jestjs directive


    【解决方案1】:

    解决办法如下:

    @Component({
        template: ` <div #container directive></div> `,
    })
    class TestComponent {
        @ViewChild('container', { static: true }) container: ElementRef;
    }
    
    describe('Directive', () => {
        let fixture: ComponentFixture<TestComponent>;
        let component: TestComponent;
    
        beforeEach(waitForAsync(() => {
            TestBed.configureTestingModule({
                declarations: [TestComponent, Directive],
            }).compileComponents();
    
            fixture = TestBed.createComponent(TestComponent);
            component = fixture.componentInstance;
    
            // mocking the offsetParent
            Object.defineProperty(HTMLElement.prototype, 'offsetParent', {
                get: function () {
                    return { offsetTop: 100 };
                },
            });
    
            // mocking the offsetTop
            Object.defineProperty(HTMLElement.prototype, 'offsetTop', {
                get: function () {
                    return 50;
                },
            });
    
            fixture.detectChanges();
        }));
    
        describe('addScrollTrigger', () => {
            it('sets fixed style', () => {
                //Trigger window scroll event 
                window.dispatchEvent(new Event('scroll'));
                fixture.detectChanges();
                
                expect(component.container.nativeElement.styles).toEqual(....);
            });
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 2019-12-24
      • 2020-08-05
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      • 2019-11-28
      • 1970-01-01
      相关资源
      最近更新 更多