0

我一直坚持试图通过这个测试一段时间。我希望它返回一个由 3 个 mockExpectedResult 对象组成的数组

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

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

第 3 步:reducer over freeRewardsRaw Promises,格式化为 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,
    ]);
});
4

1 回答 1

0

我试图简化您的代码和reduce. 我已经reducefilters 和s 替换了maps。请检查一下,让我知道这是否有帮助。

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;
};
于 2020-11-19T19:21:00.193 回答