小编J-m*_*man的帖子

当在Angular中使用Jasmine使用*ngIf指令时,如何单元测试元素是否可见

我有一个Angular 6应用程序,并编写一些单元测试,试图确定一个元素是否可见,仅仅基于*ngIf指令的布尔结果.

标记:

<div class="header" *ngIf="show">
    <div>...</div>
</div>
Run Code Online (Sandbox Code Playgroud)

规范文件:

it('should hide contents if show is false', () => {
    const button = debugElement.query(By.css('button')).nativeElement;
    button.click();   // this will change show to false
    fixture.detectChanges();
    expect(debugElement.query(By.css('.header')).nativeElement.style.hidden).toBe(true);
});
Run Code Online (Sandbox Code Playgroud)

我似乎无法hidden从div中获取属性.angular是否使用另一种方法使用*ngIf指令从DOM中隐藏元素?我需要从另一个房产nativeElement

谢谢!

javascript jasmine karma-jasmine angular angular-test

15
推荐指数
3
解决办法
2万
查看次数

如何按照“操作卫生”指南使用多个 NGRX 操作来执行相同的操作?

如果我有两个或多个 NGRX 操作执行相同的操作,但在应用程序的不同部分触发,我是否可以使用一个操作来执行此操作,同时区分 NGRX 日志中的操作字符串?我之所以这样问,是因为在编写操作并尝试遵循“操作卫生”原则时,操作应该特定于某个事件,并且可以在一年后可读,并告诉开发人员到底在应用程序中触发操作的位置,我想限制为尽可能多的 NGRX 样板。

例如:我有一个“AddTodo”操作。此操作可以在我的应用程序中的两个位置执行 - 一个在“待办事项列表”页面上,另一个在“待办事项详细信息”页面上。每个操作对于触发它们的页面来说都是唯一的,但它们执行完全相同的操作;他们向我的状态对象添加了一个新的待办事项。

const TODO_LIST = '[Todo List]';
const TODO_DETAILS = '[Todo Details]';

export const TODO_LIST_ADD = `${TODO_LIST} Add`;
export const TODO_DETAILS_ADD = `${TODO_DETAILS} Add`;

export class TodoListAdd implements Action {
    readonly type = TODO_LIST_ADD;
    constructor(public payload: Todo) { }
}

export class TodoDetailsAdd implements Action {
    readonly type = TODO_DETAILS_ADD;
    constructor(public payload: Todo) { }
}

export function todoReducer(state = initialState, action: Action): TodoState {
    switch (action.type) {
        case actions.TODO_LIST_ADD:
        case actions.TODO_DETAILS_ADD: …
Run Code Online (Sandbox Code Playgroud)

redux ngrx angular

8
推荐指数
1
解决办法
1122
查看次数

延迟加载的要素可以通过Angular 6中的ngrx在要素之间传递状态吗?

如果我有一个使用延迟加载的应用程序,并且还使用ngrx来管理状态,则每个功能的状态实例都有其自己的reducer,action等。因此,例如:

product-feature
    product-edit
    product-add
    product-admin
    state
        product.reducer.ts
        product.actions.ts
        product.effects.ts
        product.index.ts
customer-feature
    customer-edit
    customer-add
    customer-admin
    state
        customer.reducer.ts
        customer.actions.ts
        customer.effects.ts
        customer.index.ts
Run Code Online (Sandbox Code Playgroud)

通过这种结构,我的主要问题是之间的状态是否可以product-feature通信并使用之间的状态customer-feature?如果我作为用户进入customer-feature,但是customer-feature需要从中获取一些状态信息product-feature,即使product-feature由于用户没有去创建(由于延迟加载)而从未创建,它仍会渲染并获取数据吗?

我在网上看到的大多数示例都将ngrx视为一体,AppState并且不进行延迟加载,而在惰性加载示例中,组件之间的通信是父级/子级。我读过的一些文章说,您需要扩展应用程序状态以包括功能状态,因为无法在应用程序状态中引用功能状态。我想知道的实例正在兄弟功能之间传递状态。ngrx是否可以通过延迟加载来实现?

谢谢。

javascript lazy-loading ngrx angular

7
推荐指数
1
解决办法
1143
查看次数

如何使用 Angular 6 在 Jasmine 中对 ngrx 功能选择器进行单元测试?

我的产品功能有一个功能选择器和相邻的选择器:

const getProductState = createFeatureSelector<fromProducts.State>('products');

export const getOldProducts = createSelector(
    getProductState,
    state => state.oldProducts
);

export getNewProducts = createSelector(
    getProductState,
    state => state.newProducts
);
Run Code Online (Sandbox Code Playgroud)

我想对这些选择器进行单元测试。我读过的大多数文章和帖子都只涉及非特征选择器,所以是时候模拟特征状态了,我的测试将返回undefined状态对象。我的规范文件如下所示:

beforeEach(async(() => {
    TestBed.configureTestingModule({
        imports: [
            StoreModule.forRoot({}),
            StoreModule.forFeature('products', fromProduct.reducer)
        ],
        declarations: [],
        providers: [{
            provide: Store, useClass: class { select = jasmine.createSpy('select'); dispatch = jasmine.createSpy('dispatch'); }
        }]
    })
    mockStore = TestBed.get(Store);
}));

it('getOldProducts should return the oldProducts', () => {
    const state = {
        name: 'Product1',
        oldProduct: {
            price: 45.00
        }, …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine ngrx angular angular6

6
推荐指数
1
解决办法
4186
查看次数

为什么在Jasmine Angular单元测试中不触发反应形式控件的更改事件?

我有一个更改函数,该函数将传递给反应形式控件的更改事件的更改事件,该控件评估状态并检查控件上是否有任何错误,如果是,则将布尔标志设置为true / false。然后,此布尔标志用于确定是否显示<div>具有错误消息的元素。这在浏览器中可以正常工作,但是在运行单元测试时,永远不会将“ dirty”设置为true。这是我的代码:

的HTML

<form [formGroup]="myForm" novalidate>
    <input id="age" formControlName="age" (change)="onChange()" />
    <div id="ageError" *ngIf="ageIsError()">
        <label>Age has errored</label>
    </div>
</form>
Run Code Online (Sandbox Code Playgroud)

零件

constructor(private fb: FormBuilder) {}

ngOnInit() {
    this.myForm = this.fb.group({
        age: [null, [Validators.min(18)]]
    });
}

onChange() {
    if (this.ageIsError())
        // do something
}

ageIsError() {
    return this.myForm.controls.age.hasError('min') &&
           this.myForm.controls.age.dirty;
}
Run Code Online (Sandbox Code Playgroud)

单元测试

it('should show error message when age is less than 18', fakeAsync(() => {
    let age = component.myForm.controls.age;
    age.setValue('10', { emitEvent: …
Run Code Online (Sandbox Code Playgroud)

unit-testing jasmine typescript angular angular-reactive-forms

6
推荐指数
1
解决办法
1262
查看次数

iTextSharp处理PDF压缩吗?

iTextSharp可以压缩PDF文件吗?我正在寻找可用于开发以压缩PDF文件的PDF库.基本上,我有一个包含许多PDF文件的文件夹列表,大小从1MB到10MB不等,这些文件夹的数量每天都在增长,所以为了节省磁盘空间,我希望能够在PDF文件中读取一次它已被处理,压缩,然后将其保存到指定的文件夹位置.

如果iTextSharp不支持压缩,是否有人可以获得其他.NET PDF库的建议?购买图书馆不是问题.我查看了许多免费的内容,例如PDFSharp,我认为它在制作PDF方面非常好,但无法渲染或压缩它们.

我在Chris Haas的stackoverflow上读到了一个很好的答案:

PdfStamper是一个帮助器类,它最终使用另一个名为PdfStamperImp的类来完成大部分工作.PdfStamperImp派生自PdfWriter,当你使用stamper.Writer时,实际上你正在回到这个实现类.PdfStamper上的许多属性也直接传递给实现类.所以这两个电话实际上做同样的事情.

stamper.SetFullCompression();
stamper.Writer.SetFullCompression();

另一个混淆点是SetFullCompression和CompressionLevel实际上并不相关."完全压缩"表示在PDF 1.5中添加的称为"对象流"的功能,该功能允许将PDF对象组合在一起以潜在地允许更大的压缩.实际上并没有要求我们所谓的"压缩"实际发生,但实际上我认为它总会发生.(可能一个超级简单的文档可能会变大,启用此功能,不确定并且不想测试.)

CompressionLevel实际上是您通常认为的压缩,0到9之间的数字或-1表示默认值(目前等于六我认为).这个属性实际上是PdfStream类的一部分,许多类最终都是从这个类派生出来的.但是,此设置不会"涓滴".由于您是通过GetPageContent()和SetPageContent()从其他位置导入流,因此特定流具有与Writer的压缩设置无关的压缩设置.实际上有第三个参数可以传递给SetPageContent()来设置你想要的特定压缩级别.

reader.SetPageContent(1, reader.GetPageContent(1), PdfStream.BEST_COMPRESSION);

/sf/answers/1541960591/

任何帮助或建议将不胜感激.

谢谢.

c# compression pdf pdfsharp itextsharp

5
推荐指数
1
解决办法
1万
查看次数

使用延迟加载时,如何在Angular中动态构建导航?

当我所有的路线都使用延迟加载存储在路线模块文件中时,是否可以为标题组件构建导航?我已经看到了将所有导航存储在JSON对象或文件中时动态创建导航的文章,它们一次加载了所有内容并从那里构建它,但是这些文章都没有使用延迟加载。我有一个Angular 6应用程序,该应用程序使用延迟加载并且具有相当广泛的导航结构,并且我不想硬编码HTML中的所有导航项,即使这并不难做到。我认为从结构中加载值并使用Angulars *ngFor指令动态构建导航标记是一个更可行的选择。我的问题是,延迟加载是否有可能?

更详细一点:我的应用程序有一个app-routing.module,其中包含主路由数据,该数据告诉应用程序加载模块,例如:

{ path: 'test', loadChildren: './test/test.module#TestModule' }
Run Code Online (Sandbox Code Playgroud)

因此,当用户导航到时/test,将TestModule正确加载。我在我的导航标记中对此路线进行了硬编码header.component,如下所示:

<a routerLink="test/main">Test</a>
Run Code Online (Sandbox Code Playgroud)

是否可以<a>通过延迟加载来动态构建导航(标签)?

谢谢!

javascript lazy-loading angular-routing angular

5
推荐指数
1
解决办法
515
查看次数

使用 Angular 6 延迟加载时,ngrx 的最佳文件夹结构是什么?

我有一个使用延迟加载的 Angular 6 应用程序。我的文件夹结构如下所示:

src
  app
    main
      products
      invoices
      customers
      suppliers
    core
      header
      footer
      services
      core.module.ts
    shared
    app-routing.module.ts
    app.component.html
    app.component.css
    app.component.spec.ts
    app.component.ts
Run Code Online (Sandbox Code Playgroud)

我按照推荐的文件夹结构进行延迟加载(这里,文件夹下的每个文件main夹都是它自己的模块部分)。

如果我想合并ngrx,我已经看到了关于在何处包含 store、reducer 等的不同意见。我读过的一篇文章说要添加一个store文件夹并将所有 ngrx 片段包含在其中。我读过的另一篇文章说要将 ngrx 片段添加到每个模块/部分。

这里还有另一篇 stackoverflow 文章:What is the best structure for app using ngrx? 谈到 ngrx 文件夹结构,但没有提到延迟加载。

使用延迟加载时,是否有推荐的方法来构建 ngrx?向每个模块添加一个带有减速器、动作等的存储,还是创建一个包含所有内容的巨大存储文件夹更有意义?这个应用程序不是太大;它更像是一个中等规模的应用程序。

谢谢!

javascript ngrx angular

5
推荐指数
1
解决办法
4472
查看次数

如何为单元测试 Angular 6 组件提供带有防护的路由?

我有一个带有输入字段的Angular 6组件。如果任何输入字段在用户试图离开时验证失败,则会触发该canDeactivate功能的守卫。守卫是通用的,因为这一逻辑需要发生在应用程序中的多个组件上。它运行起来很漂亮,但是当我尝试对这种行为进行单元测试时,canDeactivate从未达到过守卫中的功能。达到了守卫本身,但从未达到过功能。我是否以不正确的方式提供警卫?

防护组件接口

export interface GuardComponent {
    canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}
Run Code Online (Sandbox Code Playgroud)

CanDeactivateGuard

export class CanDeactivateGuard implements CanDeactivate<GuardComponent> {
    constructor() { }

    canDeactivate(component: GuardComponent): Observable<boolean> | Promise<boolean> | boolean {
        return component.canDeactivate();
    }
}
Run Code Online (Sandbox Code Playgroud)

成分

export class MyComponent implements OnInit, GuardComponent {

...

    canDeactivate() {
        if (!this.form.invalid) {
            return true;
        }
        this.isError = true;
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

规格

const routes = [
    {
        path: 'my-component',
        component: MyComponent,
        canDeactivate: [CanDeactivateGuard], …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing jasmine angular-router-guards angular6

5
推荐指数
0
解决办法
686
查看次数

如何在Angular中使用Jasmine和Karma检查段落的innerHTML内容

我有一个Angular组件,其中<p>带有一些文本标签。该段中的句子之一为粗体。我试图在Jasmine / Karma中编写一个单元测试来检查innerHTML内容。但是,我所有的尝试似乎都失败了。我会以错误的方式处理吗?这是我的标记和JavaScript:

角度组件HTML

<p>
    Four score and seven years ago our fathers brought forth on this 
    continent, <b>a new nation</b>, conceived in Liberty, and dedicated to the 
    proposition that all men are created equal.
</p>
Run Code Online (Sandbox Code Playgroud)

茉莉花单元测试

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

    beforeEach(() => {
        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
        el = fixture.debugElement;
        fixture.detectChanges();
    });

    it('should display "a new nation" as bold text', () => {
        const …
Run Code Online (Sandbox Code Playgroud)

javascript jasmine karma-jasmine angular

4
推荐指数
1
解决办法
1419
查看次数