Skip to content

Commit 2d538c2

Browse files
Sort inspected object properties
1 parent e4dd359 commit 2d538c2

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
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,
@@ -118,10 +122,12 @@ export function jsonify(
118122
}),
119123
};
120124
}
121-
const out = { ...data };
122-
Object.keys(out).forEach(key => {
123-
out[key] = jsonify(out[key], getVNode, seen);
124-
});
125+
const out = {};
126+
Object.keys(data)
127+
.sort(sortStrings)
128+
.forEach(key => {
129+
(out as any)[key] = jsonify((data as any)[key], getVNode, seen);
130+
});
125131
return out;
126132
}
127133
default:
@@ -162,7 +168,10 @@ export function setInCopy<T = any>(
162168
if (idx >= path.length) return value;
163169

164170
// Signals bypass everything
165-
if (path[path.length - 1] === "value" && maybeSetSignal(obj, path, value)) {
171+
if (
172+
path[path.length - 1] === "value" &&
173+
maybeSetSignal(obj as any, path, value)
174+
) {
166175
return obj;
167176
}
168177

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+
);
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-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)