0

NGXS 库具有单元测试指令,用于注入存储并通过重置功能创建一些准备好测试的初始状态。

文档中的示例:

import { TestBed, async } from '@angular/core/testing';

export const SOME_DESIRED_STATE = {
  animals: ['Panda'],
};

describe('Zoo', () => {
  let store: Store;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [NgxsModule.forRoot([ZooState])],
    }).compileComponents();

    store = TestBed.get(Store);
    store.reset(SOME_DESIRED_STATE);
  }));

  it('it toggles feed', async(() => {
    store.dispatch(new FeedAnimals());
    store.selectOnce(state => state.zoo.feed).subscribe(feed => {
      expect(feed).toBe(true);
    });
  }));
});

在测试带有状态的组件时,我希望能够使用测试库做类似的事情。

import {render, screen, fireEvent} from '@testing-library/angular'
import {TestComponent} from './test.component.ts'

export const SOME_DESIRED_STATE = {
  animals: ['Panda'],
};

describe('TestComponent', () => {
  let store: Store;

  it('it has desired init state', async(() => {
    render(<TestComponent/>, {
      imports: [NgxsModule.forRoot([ZooState])]
    })

    // how to init state to SOME_DESIRED_STATE ready for a ui test?
  }));
});

有什么方法可以在测试前操纵状态吗?

我最初的想法是创建一个提供者工厂,在返回之前对商店进行引用,但这似乎不起作用。

4

0 回答 0