简体   繁体   English

如何测试使用primeng组件的angular组件

[英]How to test an angular component that uses primeng component

I'll try to keep the code as minimal as possible.我会尽量减少代码。 I've just started watching lectures on Pluralsight on Unit testing.我刚刚开始观看关于单元测试的 Pluralsight 讲座。 I've a TimeselectorComponent , on click it should open an overlay with some hard coded text.我有一个TimeselectorComponent ,点击它应该打开一个带有一些硬编码文本的覆盖。 That overlay is actually p-overlayPanel from primeng .该叠加层实际上是来自primengp-overlayPanel overlayPanel 。 I just want to write a unit test using Jasmine and Karma that when I click on TimeselectorComponent then the overlay should come up.我只想使用 Jasmine 和 Karma 编写一个单元测试,当我单击TimeselectorComponent时,应该会出现覆盖。 Here's the code:这是代码:

timeselector.component.html timeselector.component.html

<div (click)="op.toggle($event)">
    Click to see the text
</div>

<div class="timeselector">
  <p-overlayPanel #op>
    <div>
      <p>Hello world!</p>
    </div>
   </p-overlayPanel>
</div>

Here's my spec file:这是我的规格文件:

import { TestBed, ComponentFixture, async } from "@angular/core/testing"
import { TimeselectorComponent } from './timeselector.component'
import { Component, Input } from '@angular/core';
import { FormsModule } from '@angular/forms';
...

describe('TimeselectorComponent', () => {
    let fixture: ComponentFixture<TimeselectorComponent>;

    @Component({
        selector: 'p-overlayPanel',
        template: '<div></div>'
    })
    class FakePOverlayPanel {
    }

    beforeEach(async(()=>{
        TestBed.configureTestingModule({
            imports: [
                FormsModule
            ],
            providers: [

            ],
            declarations: [
                TimeselectorComponent,
                FakePOverlayPanel
            ]
        });
        fixture = TestBed.createComponent(TimeselectorComponent);

    }))
    it('should create', ()=> {
        expect(fixture.componentInstance).toBeTruthy();
    })

    it('should apply filters across dashboards', () => {
        // assert

        // expect
    })
})

Please correct me.请纠正我。 Is it even testable?它甚至可以测试吗? Please help me.请帮我。

  1. In your TestBed.configureTestingModule you have to import:在您的TestBed.configureTestingModule中,您必须导入:
BrowserAnimationsModule,
OverlayPanelModule,
  1. In your test:在您的测试中:
let debugEl: DebugElement = fixture.debugElement;
debugEl.nativeElement.click();

fixture.detectChanges();
  
let overlayPanel = debugEl.query(By.directive(OverlayPanel));
expect(overlayPanel).toBeTruthy();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM