【发布时间】:2019-11-13 07:42:47
【问题描述】:
这是我测试某些类对象是否相等的代码。如果你想知道我为什么不只是在做,请看我的other question
expect(receivedDeals).toEqual(expectedDeals) 和其他更简单的断言。
type DealCollection = { [key: number]: Deal }; // imported from another file
it("does the whole saga thing", async () => {
sagaStore.dispatch(startAction);
await sagaStore.waitFor(successAction.type);
const calledActionTypes: string[] = sagaStore
.getCalledActions()
.map(a => a.type);
expect(calledActionTypes).toEqual([startAction.type, successAction.type]);
const receivedDeals: DealCollection = sagaStore.getLatestCalledAction()
.deals;
Object.keys(receivedDeals).forEach((k: string) => {
const id = Number(k);
const deal = receivedDeals[id];
const expected: Deal = expectedDeals[id];
for (let key in expected) {
if (typeof expected[key] === "function") continue;
expect(expected[key]).toEqual(deal[key]);
}
});
});
测试顺利通过,但我在 expected[key] 上收到 Flow 错误:
Cannot get 'expected[key]' because an index signature declaring the expected key / value type is missing in 'Deal'
我可以通过请求从Deal 粘贴代码,但我想你只需要知道我还没有声明索引签名(因为我不知道如何声明!)。
我搜索了一下,但找不到这个确切的案例。
更新:我可以通过更改 deal 和 expected 来消除错误:
const deal: Object = { ...receivedDeals[id] };
const expected: Object = { ...expectedDeals[id] };
因为我在循环中比较属性,所以这不是问题。但我想我应该可以用Deals 来做到这一点,我想知道我如何声明错误中提到的索引签名。
附言。额外的问题:在某个疯狂的科学家将 JS 与 Swift 杂交的世界里,我想你可以做类似的事情
const deal: Object = { ...receivedDeals[id] where (typeof receivedDeals[id] !== "function" };
const expected = // same thing
expect(deal).toEqual(expected);
// And then after some recombining of objects:
expect(receivedDeals).toEqual(expectedDeals);
这是一回事吗?
编辑:
添加一点Deal类的定义:
Deal.js(摘要)
export default class Deal {
obj: { [key: mixed]: mixed };
id: number;
name: string;
slug: string;
permalink: string;
headline: string;
// ...other property definitions
constructor(obj?: Object) {
if (!obj) return;
this.id = obj.id;
this.name = obj.name;
this.headline = obj.headline;
// ...etc
}
static fromApi(obj: Object): Deal {
const deal = new Deal();
deal.id = obj.id;
deal.name = obj.name;
deal.slug = obj.slug;
deal.permalink = obj.permalink;
// ...etc
return deal;
}
descriptionWithTextSize(size: number): string {
return this.descriptionWithStyle(`font-size:${size}`);
}
descriptionWithStyle(style: string): string {
return `<div style="${style}">${this.description}</div>`;
}
distanceFromLocation = (
location: Location,
unit: unitOfDistance = "mi"
): number => {
return distanceBetween(this.location, location);
};
distanceFrom = (otherDeal: Deal, unit: unitOfDistance = "mi"): number => {
return distanceBetween(this.location, otherDeal.location);
};
static toApi(deal: Deal): Object {
return { ...deal };
}
static collectionFromArray(array: Object[]) {
const deals: DealCollection = {};
array.forEach(p => (deals[p.id] = Deal.fromApi(p)));
return deals;
}
}
【问题讨论】:
标签: javascript flowtype