diff options
Diffstat (limited to 'devtools/client/shared/components/test/node/components/reps/array.test.js')
-rw-r--r-- | devtools/client/shared/components/test/node/components/reps/array.test.js | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/devtools/client/shared/components/test/node/components/reps/array.test.js b/devtools/client/shared/components/test/node/components/reps/array.test.js new file mode 100644 index 0000000000..cd8cfb9d97 --- /dev/null +++ b/devtools/client/shared/components/test/node/components/reps/array.test.js @@ -0,0 +1,117 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ + +"use strict"; + +const { shallow } = require("enzyme"); + +const { + REPS, + getRep, +} = require("resource://devtools/client/shared/components/reps/reps/rep.js"); + +const { + MODE, +} = require("resource://devtools/client/shared/components/reps/reps/constants.js"); +const { ArrayRep, Rep } = REPS; +const { maxLengthMap } = ArrayRep; + +describe("Array", () => { + it("selects Array Rep as expected", () => { + const stub = []; + expect(getRep(stub, undefined, true)).toBe(ArrayRep.rep); + }); + + it("renders empty array as expected", () => { + const object = []; + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultOutput = "[]"; + expect(renderRep({ mode: undefined }).text()).toBe(defaultOutput); + expect(renderRep({ mode: undefined }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.TINY }).text()).toBe(defaultOutput); + expect(renderRep({ mode: MODE.TINY }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe(defaultOutput); + }); + + it("renders basic array as expected", () => { + const object = [1, "foo", {}]; + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultOutput = '[ 1, "foo", {} ]'; + expect(renderRep({ mode: undefined }).text()).toBe(defaultOutput); + expect(renderRep({ mode: undefined }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.TINY }).text()).toBe("[…]"); + expect(renderRep({ mode: MODE.TINY }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe(defaultOutput); + }); + + it("renders array with more than SHORT mode max props as expected", () => { + const object = Array(maxLengthMap.get(MODE.SHORT) + 1).fill("foo"); + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultShortOutput = `[ ${Array(maxLengthMap.get(MODE.SHORT)) + .fill('"foo"') + .join(", ")}, … ]`; + expect(renderRep({ mode: undefined }).text()).toBe(defaultShortOutput); + expect(renderRep({ mode: MODE.TINY }).text()).toBe("[…]"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultShortOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe( + `[ ${Array(maxLengthMap.get(MODE.SHORT) + 1) + .fill('"foo"') + .join(", ")} ]` + ); + }); + + it("renders array with more than LONG mode maximum props as expected", () => { + const object = Array(maxLengthMap.get(MODE.LONG) + 1).fill("foo"); + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultShortOutput = `[ ${Array(maxLengthMap.get(MODE.SHORT)) + .fill('"foo"') + .join(", ")}, … ]`; + expect(renderRep({ mode: undefined }).text()).toBe(defaultShortOutput); + expect(renderRep({ mode: MODE.TINY }).text()).toBe("[…]"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultShortOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe( + `[ ${Array(maxLengthMap.get(MODE.LONG)).fill('"foo"').join(", ")}, … ]` + ); + }); + + it("renders recursive array as expected", () => { + const object = [1]; + object.push(object); + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultOutput = "[ 1, […] ]"; + expect(renderRep({ mode: undefined }).text()).toBe(defaultOutput); + expect(renderRep({ mode: undefined }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.TINY }).text()).toBe("[…]"); + expect(renderRep({ mode: MODE.TINY }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe(defaultOutput); + }); + + it("renders array containing an object as expected", () => { + const object = [ + { + p1: "s1", + p2: ["a1", "a2", "a3"], + p3: "s3", + p4: "s4", + }, + ]; + const renderRep = props => shallow(Rep({ object, noGrip: true, ...props })); + + const defaultOutput = "[ {…} ]"; + expect(renderRep({ mode: undefined }).text()).toBe(defaultOutput); + expect(renderRep({ mode: undefined }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.TINY }).text()).toBe("[…]"); + expect(renderRep({ mode: MODE.TINY }).prop("title")).toBe("Array"); + expect(renderRep({ mode: MODE.SHORT }).text()).toBe(defaultOutput); + expect(renderRep({ mode: MODE.LONG }).text()).toBe(defaultOutput); + }); +}); |