NGXS-测试懒惰选择器失败



我们使用的是ngxs,并且在与状态定义分离的文件中定义了一些惰性选择器

export class SectionSelectors {
@Selector([CatalogState])
static ById(state: CatalogModel) {
return function getSectionById(id: number): Section {
const selectedSection: Section = state.sections[id];
return selectedSection;
};
}
}

我们有像这样的测试用例

import { TestBed } from '@angular/core/testing';
import { Section } from '@miq-catalog/catalog';
import { NgxsModule, Store } from '@ngxs/store';
import { CatalogModel, CatalogState } from './catalog.state';
import { SectionSelectors } from './section.selectors';
describe('SectionSelectors', () => {
it('should select the section by id', () => {
const one: Section = { sectionId: 1, title: '', columns: [] };
const two: Section = { sectionId: 2, title: '', columns: [] };
const state: CatalogModel = {
catalog: [],
sections: { 1: one, 2: two },
columns: {},
catalogLoaded: true,
};
const selectionFunction = SectionSelectors.ById(state);
const result = selectionFunction(1);
expect(result).toBeDefined();
expect(result).toBe(one);
expect(result.sectionId).toBe(1);
const result2 = selectionFunction(2);
expect(result2).toBeDefined();
expect(result2).toBe(two);
expect(result2.sectionId).toBe(2);
});
});

我们正在将状态传递给选择器,但是我们得到了下一个错误

An error was thrown in afterAll
Uncaught ReferenceError: Cannot access 'CatalogState' before initialization
ReferenceError: Cannot access 'CatalogState' before initialization

我注意到,如果我将这些选择器移到CatalogState(@State定义所在的位置(,问题就解决了。但这迫使我们把所有的选择器都放在那里,我们认为最好让它们在自己的相关文件上确定范围,这样我们就不会";"污染";使用混合选择器。

有没有一种方法可以修复测试用例?有人之前已经面临过这种懒惰选择器测试吗?

作为补充信息,这就是我们的州看起来像的样子

@State({
name: 'Catalog',
defaults: {
catalogLoaded: false,
columns: {},
sections: {},
catalog: [],
},
})
export class CatalogState {
constructor(private store: Store) {}
@Action(RetrieveCatalogInfo)
@Action(ChangeColumnConfig)
@Action(ClearCatalog)
public executeAction(ctx: StateContext<CatalogModel>, params: ExecutableAction<CatalogModel>) {
return params.execute({ ctx, store: this.store });
}
}

这应该不是最新版本的NGXS的问题(自v3.6.1以来(。

最新更新