Skip to content
This repository was archived by the owner on Sep 21, 2021. It is now read-only.

Commit 0987331

Browse files
committed
Fix getValue function for legitimate falsy values.
The getValue was failing if the item has a falsy value (false, null, 0, …). This patch fixes this issue and adds some tests to ensure getValue works as expected.
1 parent d63b510 commit 0987331

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

packages/devtools-reps/src/object-inspector/tests/object-inspector.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,3 +816,89 @@ describe("getChildren", () => {
816816
expect(nodePaths).toEqual(childrenPaths);
817817
});
818818
});
819+
820+
describe("getValue", () => {
821+
it("get the value from contents.value", () => {
822+
let item = {
823+
contents: {
824+
value: "my value"
825+
}
826+
};
827+
expect(getValue(item)).toBe("my value");
828+
829+
item = {
830+
contents: {
831+
value: 0
832+
}
833+
};
834+
expect(getValue(item)).toBe(0);
835+
836+
item = {
837+
contents: {
838+
value: false
839+
}
840+
};
841+
expect(getValue(item)).toBe(false);
842+
843+
item = {
844+
contents: {
845+
value: null
846+
}
847+
};
848+
expect(getValue(item)).toBe(null);
849+
});
850+
851+
it("get the value from contents.getterValue", () => {
852+
let item = {
853+
contents: {
854+
getterValue: "my getter value"
855+
}
856+
};
857+
expect(getValue(item)).toBe("my getter value");
858+
859+
item = {
860+
contents: {
861+
getterValue: 0
862+
}
863+
};
864+
expect(getValue(item)).toBe(0);
865+
866+
item = {
867+
contents: {
868+
getterValue: false
869+
}
870+
};
871+
expect(getValue(item)).toBe(false);
872+
873+
item = {
874+
contents: {
875+
getterValue: null
876+
}
877+
};
878+
expect(getValue(item)).toBe(null);
879+
});
880+
881+
it("get the value from getter and setter", () => {
882+
let item = {
883+
contents: {
884+
get: "get"
885+
}
886+
};
887+
expect(getValue(item)).toEqual({get: "get"});
888+
889+
item = {
890+
contents: {
891+
set: "set"
892+
}
893+
};
894+
expect(getValue(item)).toEqual({set: "set"});
895+
896+
item = {
897+
contents: {
898+
get: "get",
899+
set: "set"
900+
}
901+
};
902+
expect(getValue(item)).toEqual({get: "get", set: "set"});
903+
});
904+
});

packages/devtools-reps/src/object-inspector/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
const get = require("lodash/get");
6+
const has = require("lodash/has");
67
const { maybeEscapePropertyName } = require("../reps/rep-utils");
78

89
let WINDOW_PROPERTIES = {};
@@ -12,10 +13,13 @@ if (typeof window === "object") {
1213
}
1314

1415
function getValue(item) {
15-
let value = get(item, "contents.value", undefined)
16-
|| get(item, "contents.getterValue", undefined);
16+
let value;
1717

18-
if (!value && nodeHasAccessors(item)) {
18+
if (has(item, "contents.value")) {
19+
value = get(item, "contents.value");
20+
} else if (has(item, "contents.getterValue")) {
21+
value = get(item, "contents.getterValue", undefined);
22+
} else if (nodeHasAccessors(item)) {
1923
value = item.contents;
2024
}
2125

0 commit comments

Comments
 (0)