Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
it("rejects malformed API data", () => {
const payload = {
uuid: "",
statusBeforeError: SpecialStatus.DIRTY,
dispatch: jest.fn(),
index: buildResourceIndex([fakePeripheral()]).index
};
payload.uuid = Object.keys(payload.index.all)[0];
console.error = jest.fn();
updateViaAjax(payload).catch(e => {
expect("" + e).toEqual("Error: Just saved a malformed TR.");
expect(console.error).toHaveBeenCalledTimes(1);
expect(console.error).toHaveBeenCalledWith(
expect.stringContaining("Peripheral"));
});
});
});
it("doesn't fire maybeClearForm() if form is filled", () => {
const { el, instance } = testCase();
el.setState({
status: SpecialStatus.DIRTY,
form: { ...instance().state.form, password: "X" }
});
el.update();
expect(instance().state.form.password).toEqual("X");
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
instance().maybeClearForm();
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
expect(instance().state.form.password).toEqual("X");
});
it("doesn't fire maybeClearForm() if form is filled", () => {
const { el, instance } = testCase();
el.setState({
status: SpecialStatus.DIRTY,
form: { ...instance().state.form, password: "X" }
});
el.update();
expect(instance().state.form.password).toEqual("X");
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
instance().maybeClearForm();
expect(instance().state.status).toBe(SpecialStatus.DIRTY);
expect(instance().state.form.password).toEqual("X");
});
export function overwrite(tr: T,
changeset: T["body"],
specialStatus = SpecialStatus.DIRTY):
ReduxAction {
return {
type: Actions.OVERWRITE_RESOURCE,
payload: { uuid: tr.uuid, update: changeset, specialStatus }
};
}
.filter(x => x.specialStatus === SpecialStatus.DIRTY);
return
export function SaveBtn(props: SaveBtnProps) {
const STATUS_TRANSLATION: Partial> = {
[SpecialStatus.DIRTY]: "is-dirty",
[SpecialStatus.SAVING]: "is-saving"
};
const CAPTIONS: Partial> = {
[SpecialStatus.DIRTY]: (props.dirtyText || t("Save") + " *"),
[SpecialStatus.SAVING]: props.savingText || t("Saving")
};
const { savedText, onClick, hidden } = props;
const statusClass = STATUS_TRANSLATION[props.status || ""] || "is-saved";
const klass = `${props.color || "green"} ${statusClass} save-btn fb-button`;
const spinnerEl = (props.status === SpecialStatus.SAVING) ?
spinner : "";
return <button hidden="{!!hidden}">
{CAPTIONS[props.status] || (savedText || t("Saved") + " ✔")} {spinnerEl}
</button>;
}
.add(Actions.EDIT_RESOURCE, (s, { payload }) => {
const { update } = payload;
const target = findByUuid(s.index, payload.uuid);
const before = defensiveClone(target.body);
merge(target, { body: update });
const didChange = !equals(before, target.body);
didChange && mutateSpecialStatus(target.uuid, s.index, SpecialStatus.DIRTY);
return s;
})
.add(Actions.OVERWRITE_RESOURCE, (s, { payload }) => {
export const generalizedError = (payload: GeneralizedError) => {
const badStatus = payload.statusBeforeError == SpecialStatus.SAVING;
if (badStatus) {
/** If, somehow, a `SAVING` status sneaks in, default it to DIRTY. */
payload.statusBeforeError = SpecialStatus.DIRTY;
}
toastErrors(payload);
stopTracking(payload.uuid);
return { type: Actions._RESOURCE_NO, payload };
};
(e: React.SyntheticEvent) => {
const wow = {
status: SpecialStatus.DIRTY,
form: { ...this.state.form, [key]: e.currentTarget.value }
};
this.setState(wow, this.maybeClearForm);
};
render() {
return
<label>{t("Tool Name")}</label>
<input> this.setState({ toolName: e.currentTarget.value })} />
this.props.dispatch(initSave("Tool", { name: this.state.toolName }))}
status={SpecialStatus.DIRTY} />
;
}
}