如何从Promise、reduce、format内部返回数据



我一直在努力让这个测试通过一段时间。我希望它返回mockExpectedResult 的3个对象的数组

步骤1:减少计划操作数组(省略没有路径的项(。这应该返回InventoryItemPath 的字符串数组

步骤2:减少InventoryItemPath数组(freeRewardsInventory(,对服务进行异步调用(getItem是这个异步GET请求的模拟(,该调用返回Promise。

步骤3:减少免费奖励原始承诺,格式化为mockExpectedResult

步骤4:返回输出(mockExpectedResults数组(

我认为我的主要问题是,我没有等待所有这些承诺(可能错过了一个等待?(

谢谢你的帮助。

const mockScheduledOperation = {
Ranks: [{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
}
]
};
const getAllRewards = async () => {
const freeRewardsInventory = mockScheduledOperation.Ranks.reduce(
(agg, rank) => {
if (rank.FreeRewards.InventoryRewards.length > 0) {
const rewardList = rank.FreeRewards.InventoryRewards.reduce(
(agg, reward) => {
if (reward.InventoryItemPath) {
agg = reward.InventoryItemPath;
}
return agg;
},
''
);
agg.push(rewardList);
}
return agg;
},
[]
);
const getItem = async (rewardPath: string) => mockReturnedItem;
const freeRewardsRaw = freeRewardsInventory.reduce < [] > (
async (agg, rewardPath) => {
const promise = await getItem(rewardPath);
agg.push(promise);
return agg;
},
[]
);
const formattedRewards = await Promise.all(freeRewardsRaw).then(
(response) => {
response.reduce < ProgressionRewards[] > ((agg, res) => {
const formattedReward: ProgressionRewards = {
// free = unlocked, paid = locked
locked: false,
level: null,
difficulty: res.CommonData.Quality || null,
date: res.CommonData.DateReleased.ISO8601Date || null,
rewardAttachments: [{
image: res.CommonData.DisplayPath.Media.MediaUrl.Path || null,
title: res.CommonData.Title.value || null,
description: res.CommonData.Description.value || null,
type: res.CommonData.Type || null,
released: null,
manufacturer: null,
howUnlock: null,
}, ],
};
agg.push(formattedReward);
return agg;
}, []);
}
);
return formattedRewards;
};
const mockExpectedResult: ProgressionRewards = {
locked: false,
level: null,
difficulty: ChallengeLevel.Easy,
date: '',
rewardAttachments: [{
image: 'media-image-path',
title: 'MIA',
description: 'reach-mia',
type: 'ArmorVisor',
released: null,
manufacturer: null,
howUnlock: null,
}, ],
};
fit('free rewards to return an array of rewards', async () => {
const awards: ProgressionRewards = await getAllRewards();
expect(awards).toBe([
mockExpectedResult,
mockExpectedResult,
mockExpectedResult,
]);
});

我试图简化您的代码和reduce中的问题。我已经用filtermap替换了reduce。请检查一下,并告诉我这是否有帮助。

const mockScheduledOperation = {
Ranks: [{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
},
{
FreeRewards: {
InventoryRewards: [{
InventoryItemPath: 'Inventory/Armor/Visors/012-001-reach-c09fa0b7.json',
}, ],
},
}
]
};

const getAllRewards = async () => {

const freeRewardsInventory = 
([] as string[])
// converting multi-dimensional array into uni-dimensional
.concat(
...mockScheduledOperation.Ranks
.filter(rank => rank.FreeRewards.InventoryRewards.length)
.map(rank => (
rank.FreeRewards.InventoryRewards
// removing all falsy values
.filter(Boolean)
.map(item => item.InventoryItemPath)
)
)
);
const getItem = (rewardPath: string) => mockReturnedItem;

const freeRewardsRaw = await Promise.all(freeRewardsInventory.map(rewardPath => getItem(rewardPath)))
const formattedRewards = freeRewardsRaw
.map < ProgressionRewards[] > ((res) => {
const formattedReward: ProgressionRewards = {
// free = unlocked, paid = locked
locked: false,
level: null,
difficulty: res.CommonData.Quality || null,
date: res.CommonData.DateReleased.ISO8601Date || null,
rewardAttachments: [{
image: res.CommonData.DisplayPath.Media.MediaUrl.Path || null,
title: res.CommonData.Title.value || null,
description: res.CommonData.Description.value || null,
type: res.CommonData.Type || null,
released: null,
manufacturer: null,
howUnlock: null,
}, ],
};
return formattedReward;
}, []);
}
);
return formattedRewards;
};

最新更新