mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 14:12:10 +08:00
Add new cloneJSON
method for cloning an object
This is useful in tests where `deepMerge` would retain references to old objects.
This commit is contained in:
@ -3,6 +3,8 @@ function isObject(obj) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// a fairly simple deep merge based on: https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
|
// a fairly simple deep merge based on: https://gist.github.com/ahtcx/0cd94e62691f539160b32ecda18af3d6
|
||||||
|
// note: this approach might reference the original object. If you mutate an object once you've deep
|
||||||
|
// cloned it, say in a test, it might remain modified. Consider `cloneJSON` instead.
|
||||||
export function deepMerge(...objects) {
|
export function deepMerge(...objects) {
|
||||||
function deepMergeInner(target, source) {
|
function deepMergeInner(target, source) {
|
||||||
Object.keys(source).forEach((key) => {
|
Object.keys(source).forEach((key) => {
|
||||||
@ -53,3 +55,7 @@ export function deepEqual(obj1, obj2) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function cloneJSON(obj) {
|
||||||
|
return JSON.parse(JSON.stringify(obj));
|
||||||
|
}
|
||||||
|
@ -3,6 +3,7 @@ import { test } from "qunit";
|
|||||||
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
||||||
import selectKit from "discourse/tests/helpers/select-kit-helper";
|
import selectKit from "discourse/tests/helpers/select-kit-helper";
|
||||||
import userFixtures from "discourse/tests/fixtures/user-fixtures";
|
import userFixtures from "discourse/tests/fixtures/user-fixtures";
|
||||||
|
import { cloneJSON } from "discourse-common/lib/object";
|
||||||
|
|
||||||
acceptance("User's bookmarks", function (needs) {
|
acceptance("User's bookmarks", function (needs) {
|
||||||
needs.user();
|
needs.user();
|
||||||
@ -23,15 +24,11 @@ acceptance("User's bookmarks - reminder", function (needs) {
|
|||||||
needs.user();
|
needs.user();
|
||||||
|
|
||||||
needs.pretender((server, helper) => {
|
needs.pretender((server, helper) => {
|
||||||
let listResponse = JSON.parse(
|
server.get("/u/eviltrout/bookmarks.json", () => {
|
||||||
JSON.stringify(userFixtures["/u/eviltrout/bookmarks.json"])
|
let json = cloneJSON(userFixtures["/u/eviltrout/bookmarks.json"]);
|
||||||
);
|
json.user_bookmark_list.bookmarks[0].reminder_at = "2028-01-01T08:00";
|
||||||
listResponse.user_bookmark_list.bookmarks[0].reminder_at =
|
return helper.response(json);
|
||||||
"2028-01-01T08:00";
|
});
|
||||||
|
|
||||||
server.get("/u/eviltrout/bookmarks.json", () =>
|
|
||||||
helper.response(listResponse)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test("removing a bookmark with a reminder shows a confirmation", async (assert) => {
|
test("removing a bookmark with a reminder shows a confirmation", async (assert) => {
|
||||||
|
Reference in New Issue
Block a user