Skip to content

Commit 987e81f

Browse files
Merge pull request #459 from preactjs/sort-obj-properties
2 parents 594e28b + eafe140 commit 987e81f

File tree

7 files changed

+48
-10
lines changed

7 files changed

+48
-10
lines changed

src/adapter/shared/serialize.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export function isReadOnlySignal(signal: Signal): boolean {
4141
);
4242
}
4343

44+
function sortStrings(a: string, b: string) {
45+
return a.localeCompare(b);
46+
}
47+
4448
export function jsonify(
4549
data: any,
4650
getVNode: (x: any) => SerializedVNode | null,
@@ -123,10 +127,12 @@ export function jsonify(
123127
}),
124128
};
125129
}
126-
const out = { ...data };
127-
Object.keys(out).forEach(key => {
128-
out[key] = jsonify(out[key], getVNode, seen);
129-
});
130+
const out = {};
131+
Object.keys(data)
132+
.sort(sortStrings)
133+
.forEach(key => {
134+
(out as any)[key] = jsonify((data as any)[key], getVNode, seen);
135+
});
130136
return out;
131137
}
132138
default:
@@ -168,7 +174,10 @@ export function setInCopy<T extends Record<string, unknown> = any>(
168174
if (idx >= path.length) return value;
169175

170176
// Signals bypass everything
171-
if (path[path.length - 1] === "value" && maybeSetSignal(obj, path, value)) {
177+
if (
178+
path[path.length - 1] === "value" &&
179+
maybeSetSignal(obj as any, path, value)
180+
) {
172181
return obj;
173182
}
174183

test-e2e/fixtures/apps/props.jsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { h, Fragment, render } from "preact";
2+
3+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
4+
function NestedObjProps(props) {
5+
return <div style="padding: 2rem;">Nested props</div>;
6+
}
7+
8+
render(
9+
<Fragment>
10+
<NestedObjProps {...{ c: 3, a: 1, b: { c: 3, a: 1, b: 2 } }} />
11+
</Fragment>,
12+
document.getElementById("app"),
13+
);

test-e2e/tests/hooks/hooks-depth-limit.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ test("Show a deeply nested hook tree and limit value parsing depth", async ({
3737
await clickHookItem(devtools, "key6");
3838
await clickHookItem(devtools, "key7");
3939

40-
// await page.pause;
4140
const text = await devtools
4241
.locator(
4342
'form [data-testid="props-row"]:last-child [data-testid="prop-value"]',
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { test, expect } from "@playwright/test";
2+
import { gotoTest, locateTreeItem, getProps } from "../pw-utils";
3+
4+
test("Inspect should sort object keys", async ({ page }) => {
5+
const { devtools } = await gotoTest(page, "props");
6+
7+
await devtools.click(locateTreeItem("NestedObjProps"));
8+
await devtools.waitForSelector('[data-testid="Props"]');
9+
10+
await devtools.waitForSelector('[data-testid="props-row"]');
11+
12+
const props = await getProps(devtools);
13+
expect(props).toEqual({
14+
a: "1",
15+
b: "{a: 1, b: 2, c: 3}",
16+
c: "3",
17+
});
18+
});

test-e2e/tests/inspect-scroll.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ test("Highlighting should move with scroll", async ({ page }) => {
2525

2626
const _x = size.x + size.width / 2;
2727
const _y = size.y + size.height / 2;
28-
await page.pause();
2928
await page.mouse.move(_x - 100, _y - 100);
3029
await expect(page.locator(highlight)).toHaveCount(1);
3130

test-e2e/tests/inspect-truncate.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ test("Format inspected data", async ({ page }) => {
1313

1414
texts = texts.map(x => x.replace(/\n/g, ""));
1515
expect(texts).toEqual([
16+
"arr[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]",
1617
"blobBlob {}",
17-
'obj{type: "foo", props: null}',
18+
'obj{props: null, type: "foo"}',
1819
"obj2{foobarA: 1, foobarB: 1, foobarC: 1, foobarD: 1, foobarE: 1, foobarF: 1, foobarG: 1, foobarH: 1}",
19-
"arr[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39]",
2020
"vnode<div />",
2121
"vnode2<Child />",
2222
]);

test-e2e/tests/prop-input.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ test("Input various data types into DataInput", async ({ page }) => {
9292

9393
data = await enterText(page, devtools, input, '{"foo":123, "bar": [1,2]}');
9494
expect(data).toEqual({
95-
value: "{foo: 123, bar: [1, 2]}",
95+
value: "{bar: [1, 2], foo: 123}",
9696
rendered: { foo: 123, bar: [1, 2] },
9797
type: "non-editable",
9898
});

0 commit comments

Comments
 (0)