discourse/plugins/chat/test/javascripts/helpers/fabricator.js
Roman Rizzi 0a5f548635
DEV: Move discourse-chat to the core repo. (#18776)
As part of this move, we are also renaming `discourse-chat` to `chat`.
2022-11-02 10:41:30 -03:00

22 lines
603 B
JavaScript

import { cloneJSON } from "discourse-common/lib/object";
// heavily inspired by https://github.com/travelperk/fabricator
export function Fabricator(Model, attributes = {}) {
return (opts) => fabricate(Model, attributes, opts);
}
function fabricate(Model, attributes, opts = {}) {
if (typeof attributes === "function") {
return attributes();
}
const extendedModel = cloneJSON({ ...attributes, ...opts });
const props = {};
for (const [key, value] of Object.entries(extendedModel)) {
props[key] = typeof value === "function" ? value() : value;
}
return Model.create(props);
}