简体   繁体   English

为什么在 Angular 打字稿应用程序的 Jasmine 测试期间,primeng 对话框为空

[英]Why is primeng dialog empty during Jasmine testing of Angular typescript app

I am trying to test a component by clicking on the actual elements.我正在尝试通过单击实际元素来测试组件。 However, the elements don't seem to render properly in test.但是,这些元素在测试中似乎无法正确呈现。

I have the following component.我有以下组件。

import {ChangeDetectionStrategy, Component, EventEmitter, Input, Output} from '@angular/core';

@Component({
    selector: 'app-help',
    changeDetection: ChangeDetectionStrategy.OnPush,
    templateUrl: './help.component.html',
    styleUrls: ['./help.component.scss']
})
export class HelpComponent {
    @Input() showHelp = false;
    @Input() version = '0';
    @Input() versionCheck = false;
    @Output() emitCloseHelpDialog = new EventEmitter<any>();
    @Output() emitSubmitHelpForm = new EventEmitter<any>();

    additionalHelpInformationStr = '';

    closeHelpDialog(evt) {
        this.emitCloseHelpDialog.emit();
    }

    submitHelpForm(evt) {
        const formData = {
            additionalHelpInformationStr: 
            this.additionalHelpInformationStr,
        };
        this.emitSubmitHelpForm.emit(formData);
    }

    reloadIdea(evt) {
        location.reload(true);
    }
}

Which has a template which looks like so:它有一个看起来像这样的模板:

<p-dialog (onHide)="closeHelpDialog($event)"
      [(visible)]="showHelp"
      [closeOnEscape]="false"
      [modal]="false"
      [resizable]="false"
      [responsive]="false"
      appendTo="body"
      styleClass="help-dialog"
      header="Support">
<div *ngIf="versionCheck">
    A new version ofis available. Click to <span 
(click)="reload($event)"
class="new-version-notice">reload</span>.
    <br/>
</div>
<span>
    Please add a comment or concern below.<br/>
    <textarea [(ngModel)]="additionalHelpInformationStr"
              #additionalHelpInformation
              cols="75"
              class="help-textarea"
              pInputTextarea
              rows="10"></textarea>
</span>
<div class="style-notice">
    Version: {{ version }}
</div>
<p-footer>
    <p class="align-right">
        <button (click)="closeHelpDialog($event)"
                class="ui-button-secondary"
                icon="fa fa-fw fa-close"
                label="Cancel"
                type="button"
                pButton></button>
        <button (click)="submitHelpForm($event)"
                class="ui-button"
                icon="fa fa-paper-plane"
                label="Send"
                type="button"
                pButton></button>
    </p>
</p-footer>

The problem is when I try to test.问题是当我尝试测试时。 The HTML does not full render. HTML 未完全呈现。

<div id="root0" _nghost-a-c0="" ng-version="8.2.4">
    <p-dialog _ngcontent-a-c0="" 
              appendto="body" 
              header="Support" 
              styleclass="help-dialog" 
              ng-reflect-visible="false" 
              ng-reflect-header="Support" 
              ng-reflect-resizable="false" 
              ng-reflect-modal="false" 
              ng-reflect-close-on-escape="false" 
              ng-reflect-responsive="false" 
              ng-reflect-append-to="body" 
              ng-reflect-style-class="help-dialog">
    <!--bindings={"ng-reflect-ng-if": "false"}-->
</p-dialog>
</div>

This is what my test component looks like:这是我的测试组件的样子:

import {ComponentFixture, TestBed} from '@angular/core/testing';
import {HelpComponent} from './help.component';
import {DialogModule} from 'primeng/dialog';
import {InputTextareaModule} from 'primeng/inputtextarea';
import {FormsModule} from '@angular/forms';

describe('help component', () => {

    let app: HelpComponent;
    let fixture: ComponentFixture<HelpComponent>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [
                HelpComponent,
            ],
            imports: [
                DialogModule,
                FormsModule,
                InputTextareaModule,
            ],
            }
        );

        fixture = TestBed.createComponent(HelpComponent);
        app = fixture.debugElement.componentInstance;
    });

    it('should create properly', () => {
        expect(app).toBeTruthy();
        fixture.detectChanges();
        fixture.whenStable().then(() => {
        const element = fixture.nativeElement;
        // this console log outputs the HTMLfragment above which is empty
        console.log('element is ', element);
    });
});

    it('should emit close dialog event', () => {
        // this test of course works.
        const component = fixture.componentInstance;
        spyOn(component.emitCloseHelpDialog, 'emit');
        component.closeHelpDialog(new Event('click'));
        fixture.detectChanges();  
     expect(component.emitCloseHelpDialog.emit).toHaveBeenCalledWith();    
    });   
});

What am I missing.我错过了什么。

According to documentation, the default visibility is false ( https://www.primefaces.org/primeng/showcase/#/dialog ) so, the dialog has to be visible first.根据文档,默认可见性为 false ( https://www.primefaces.org/primeng/showcase/#/dialog ) 因此,对话框必须首先可见。

it('should create properly', () => {
    expect(app).toBeTruthy();
    app.showHelp = true;
    fixture.detectChanges();
    fixture.whenStable().then(() => {
    const element = fixture.nativeElement;
    console.log('element is ', element);
});

Once again, after asking the question, I figure out the answer.再一次,在问完这个问题之后,我想出了答案。 :) :)

install ng-mocks from npm or similar and then change your TestBed to the following:从 npm 或类似安装 ng-mocks,然后将您的 TestBed 更改为以下内容:

TestBed.configureTestingModule({
        declarations: [
            HelpComponent,
            MockComponent(Dialog),
        ],
        schemas: [
            CUSTOM_ELEMENTS_SCHEMA
        ],
        imports: [
            FormsModule,
            InputTextareaModule,
        ],
        }
    );

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

相关问题 用 Jasmine(角打字稿)测试铯 - Testing Cesium with Jasmine (angular typescript) 用于TypeScript应用程序测试的Angular 2 - 规格在茉莉花中多次出现 - Angular 2 for TypeScript app testing - specs appear multiple times in jasmine 使用Jasmine测试Angular2 / TypeScript管道 - Testing Angular2 / TypeScript pipe with Jasmine 使用jQWidgets和Karma / Jasmine进行单元测试Angular应用 - Unit Testing Angular app with jQWidgets and Karma/Jasmine Angular 2 测试 PrimeNG ConfirmationDialog - Angular 2 Testing PrimeNG ConfirmationDialog Angular2,Typescript,业力,Jasmine命令行测试 - Angular2, Typescript, Karma, Jasmine command line testing 使用 jasmine 模拟 if/else 语句 - 使用 Angular/Typescript 进行单元测试 - Mock an if/else statement with jasmine - unit testing with Angular/Typescript 如何使用 Jasmine 为私有方法编写 Angular / TypeScript 单元测试 - How to write unit testing for Angular / TypeScript for private methods with Jasmine Angular - 使用 PrimeNg Table 进行 Jasmine 测试 - 无法验证单元测试中的行数 - Angular - Jasmine testing with PrimeNg Table - unable to verify row count inside of unittest Tab 键在模态对话框内的输入中不起作用(Angular 应用程序/PrimeNG) - Tab key not working in inputs inside modal dialog (Angular app/PrimeNG)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM