我想写一个函数,增加工资最低的员工的工资。它应该:接收所有员工的数据,查找工资最低的员工,向该员工发送加薪20%的请求,如果请求成功,则向该员工发送有关加薪的通知,并附上以下文本
你好,<name>
!恭喜你,你的新薪水是<new salary>
!
如果请求失败,则向管理员发送错误数据。应始终返回具有布尔值的已解决承诺:如果增加成功,则返回true,否则返回false。所有获取/更改数据的函数都是异步的,并返回承诺。
我无法通过测试“如果加薪出错,它应该向管理员发送通知,而不是向用户发送通知”,我该怎么做?
function increaseSalary() {
return api.getEmployees()
.then(employeeData => {
const [minSalaryEmployee] = employeeData.reduce(([minEmployee, minSalary], employee) => {
const {salary} = employee;
return (salary < minSalary
? [employee, salary]
: [minEmployee, minSalary]
);
}, [null, Infinity]);
const {id, salary: oldSalary} = minSalaryEmployee;
const newSalary = oldSalary * 1.2;
return {id, salary: newSalary};
})
.then(({id, salary}) => api.setEmployeeSalary(id, salary))
.then(({name, id, salary}) => api.notifyEmployee(id, `Hello, ${name}! Congratulations, your new salary is ${salary}!`))
.catch(e => api.notifyAdmin(e));
}
const api = {
_employees: [
{ id: 1, name: 'Alex', salary: 120000 },
{ id: 2, name: 'Fred', salary: 110000 },
{ id: 3, name: 'Bob', salary: 80000 },
],
getEmployees() {
return new Promise((resolve) => {
resolve(this._employees.slice());
});
},
setEmployeeSalary(employeeId, newSalary) {
return new Promise((resolve) => {
this._employees = this._employees.map((employee) =>
employee.id !== employeeId
? employee
: {
...employee,
salary: newSalary,
}
);
resolve(this._employees.find(({ id }) => id === employeeId));
});
},
notifyEmployee(employeeId, text) {
return new Promise((resolve) => {
resolve(true);
});
},
notifyAdmin(error) {
return new Promise((resolve) => {
resolve(true);
});
},
setEmployees(newEmployees) {
return new Promise((resolve) => {
this._employees = newEmployees;
resolve();
});
},
};
2条答案
按热度按时间rn0zuynd1#
我猜测试套件并不是模拟
api.notifyEmployee
来抛出它,而是将其解析为false。提示符的另一种可能的解释是,可能失败的“请求”是
api.setEmployeeSalary
,如果请求未被批准,它可能返回falsey。如果这两种解释都不起作用,那么我不确定还有什么其他的解释,我会尝试找到一种资源来澄清它们的返回值,并在成功和失败的情况下抛出合约。
o7jaxewo2#