Я пытаюсь протестировать действие сбоя в своем эффекте и смоделировал его после блога Брайана Лава .Однако при неудачном тестировании я получаю сообщение об ошибке, в котором говорится, что ожидалось успешное действие.
Вот мой код:
search.service.ts:
save(acronym: Acronym) {
if (acronym.id) {
return this.update(acronym);
} else {
return this.add(acronym);
}
}
add(acronym: Acronym) {
return this.db.collection(config.acronyms).add(acronym).then(() => this.search(acronym.code, acronym.project));
}
update(acronym: Acronym) {
return this.db.collection(config.acronyms).doc(acronym.id).update(acronym).then(() => this.search(acronym.code, acronym.project));
}
effect.ts
@Effect()
saveAcronym$ = this.actions$.pipe(
ofType(acronymActions.SAVE_ACRONYM),
map((action: acronymActions.SaveAcronym) => action.payload),
mergeMap((payload) => {
this._searchService.save(payload);
return of(payload);
}),
map(data => new acronymActions.SaveAcronymSuccess(data)),
catchError(error => of(new acronymActions.SaveAcronymFail(error)))
);
effect.spec.ts
it("should return a SaveAcronymFail action, with an error, on fail", () => {
const payload = {code: "SME", project: "SWAN"};
const action = new fromActions.SaveAcronym(payload);
const error = new Error();
const result = new fromActions.SaveAcronymFail(error);
actions = hot("-a", {a: action});
const response = cold("-#|", {}, error);
const expected = cold("--(b|)", {b: result});
searchService.save = jest.fn(() => response);
expect(effects.saveAcronym$).toBeObservable(expected);
});
action.ts
export class SaveAcronymFail implements Action {
readonly type = SAVE_ACRONYM_FAIL;
constructor(public payload: Error) {}
}
редуктор.ts:
case AcronymActions.SAVE_ACRONYM_FAIL:
return {...state, loading: false, loaded: true};
И ошибка:
expect(received).toEqual(expected)
Expected value to equal:
[{"frame": 20, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value": {"payload": [Error], "type": "[ACRONYM] Save Fail"}}}, {"frame": 20, "notification": {"error": undefined, "hasValue": false, "kind": "C", "value": undefined}}]
Received:
[{"frame": 10, "notification": {"error": undefined, "hasValue": true, "kind": "N", "value": {"payload": {"code": "SME", "project": "SWAN"}, "type": "[ACRONYM] Save Success"}}}]
Я попытался изменить кадры, но безуспешно.