diff options
author | Breno Silva <brenophp@gmail.com> | 2021-02-16 17:32:51 -0300 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-02-18 00:22:45 +0100 |
commit | cfb0f3309d86d01bd4aa4cd026cf5f4f21db495a (patch) | |
tree | 84e9c62ee85c45c73c3750e894011aff851795dc | |
parent | 3940635ed32c932abfaad22ae754b5d3f370608e (diff) | |
download | serenity-cfb0f3309d86d01bd4aa4cd026cf5f4f21db495a.zip |
LibJS: Implement tests for Array.prototype.flat
-rw-r--r-- | Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.flat.js | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.flat.js b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.flat.js new file mode 100644 index 0000000000..faa4e2d0b4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Array/Array.prototype.flat.js @@ -0,0 +1,43 @@ +test("length is 0", () => { + expect(Array.prototype.flat).toHaveLength(0); +}); + +describe("normal behavior", () => { + test("basic functionality", () => { + var array1 = [1, 2, [3, 4]]; + var array2 = [1, 2, [3, 4, [5, 6]]]; + var array3 = [1, 2, [3, 4, [5, 6]]]; + expect(array1.flat()).toEqual([1, 2, 3, 4]); + expect(array2.flat()).toEqual([1, 2, 3, 4, [5, 6]]); + expect(array3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]); + }); + + test("calls depth as infinity", () => { + var array1 = [1, 2, [3, 4, [5, 6, [7, 8]]]]; + expect(array1.flat(Infinity)).toEqual([1, 2, 3, 4, 5, 6, 7, 8]); + expect(array1.flat(-Infinity)).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + }); + + test("calls depth as undefined", () => { + var array1 = [1, 2, [3, 4, [5, 6, [7, 8]]]]; + expect(array1.flat(undefined)).toEqual([1, 2, 3, 4, [5, 6, [7, 8]]]); + }); + + test("calls depth as null", () => { + var array1 = [1, 2, [3, 4, [5, 6, [7, 8]]]]; + expect(array1.flat(null)).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + expect(array1.flat(NaN)).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + }); + + test("calls depth as non integer", () => { + var array1 = [1, 2, [3, 4, [5, 6, [7, 8]]]]; + expect(array1.flat("depth")).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + expect(array1.flat("2")).toEqual([1, 2, 3, 4, 5, 6, [7, 8]]); + expect(array1.flat(2.1)).toEqual([1, 2, 3, 4, 5, 6, [7, 8]]); + expect(array1.flat(0.7)).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + expect(array1.flat([2])).toEqual([1, 2, 3, 4, 5, 6, [7, 8]]); + expect(array1.flat([2, 1])).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + expect(array1.flat({})).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + expect(array1.flat({ depth: 2 })).toEqual([1, 2, [3, 4, [5, 6, [7, 8]]]]); + }); +}); |