Repository: char0n/ramda-adjunct
Pull request #958 (2019-06-15 10:38:22)

test(flattenDepth): make tests more declarative
Ref #844




Patch:
diff --git a/src/flattenDepth.js b/src/flattenDepth.js
index 350a0e2f73..5892ae3677 100644
--- a/src/flattenDepth.js
+++ b/src/flattenDepth.js
@@ -11,7 +11,7 @@ const flatten1 = _makeFlat(false);
  * @since {@link https://char0n.github.io/ramda-adjunct/2.19.0|v2.19.0}
  * @category List
  * @sig Number -> [a] -> [b]
- * @param {!number} depth The maximum recursion depth.
+ * @param {!number} depth The maximum recursion depth
  * @param {!Array} list The array to flatten
  * @return {!Array} Returns the new flattened array
  * @see {@link http://ramdajs.com/docs/#flatten|R.flatten}, {@link http://ramdajs.com/docs/#unnest|R.unnest}
@@ -20,11 +20,11 @@ const flatten1 = _makeFlat(false);
  * RA.flattenDepth(
  *   2,
  *   [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10]
- * ); // => [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10];
+ * ); //=> [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10];
  */
 const flattenDepth = curry((depth, list) => {
   let currentDept = depth;
-  let flatList = list;
+  let flatList = [...list];
 
   while (currentDept > 0) {
     flatList = flatten1(flatList);


Test patch:
diff --git a/test/flattenDepth.js b/test/flattenDepth.js
index 4c2382bf98..685351859e 100644
--- a/test/flattenDepth.js
+++ b/test/flattenDepth.js
@@ -4,69 +4,126 @@ import * as RA from '../src';
 
 describe('flattenDepth', function() {
   context('given a list that has nested elements', function() {
-    specify(
-      'should return a flattened list to the specified depth',
-      function() {
-        const nest = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+    context('and the depth is positive integer number', function() {
+      specify('should flatten the list according the the depth', function() {
+        const depth = 2;
+        const actual = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+        const expected = [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10];
 
-        assert.deepEqual(
-          [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10],
-          RA.flattenDepth(-1, nest)
-        );
-        assert.deepEqual(
-          [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10],
-          RA.flattenDepth(0, nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, [4, 5], 6, [[[7], 8]], 9, 10],
-          RA.flattenDepth(1, nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10],
-          RA.flattenDepth(2, nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, [7], 8, 9, 10],
-          RA.flattenDepth(3, nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
-          RA.flattenDepth(4, nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
-          RA.flattenDepth(5, nest)
-        );
+        assert.deepEqual(RA.flattenDepth(depth, actual), expected);
+      });
+    });
 
-        assert.deepEqual(
-          [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10],
-          RA.flattenDepth(-1)(nest)
-        );
-        assert.deepEqual(
-          [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10],
-          RA.flattenDepth(0)(nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, [4, 5], 6, [[[7], 8]], 9, 10],
-          RA.flattenDepth(1)(nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, [[7], 8], 9, 10],
-          RA.flattenDepth(2)(nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, [7], 8, 9, 10],
-          RA.flattenDepth(3)(nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
-          RA.flattenDepth(4)(nest)
-        );
-        assert.deepEqual(
-          [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
-          RA.flattenDepth(5)(nest)
-        );
-      }
-    );
+    context('and the depth is negative integer number', function() {
+      specify('should return shallow copy of the list', function() {
+        const depth = -2;
+        const actual = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+        const expected = RA.flattenDepth(depth, actual);
+
+        assert.deepEqual(actual, expected);
+        assert.notStrictEqual(actual, expected);
+      });
+    });
+
+    context('and the depth is 0', function() {
+      specify('should return shallow copy of the list', function() {
+        const depth = 0;
+        const actual = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+        const expected = RA.flattenDepth(depth, actual);
+
+        assert.deepEqual(actual, expected);
+        assert.notStrictEqual(actual, expected);
+      });
+    });
+
+    context('and the depth is arbitrary positive integer number', function() {
+      specify('should flatten list', function() {
+        const list = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+
+        assert.deepEqual(RA.flattenDepth(1, list), [
+          1,
+          2,
+          3,
+          [4, 5],
+          6,
+          [[[7], 8]],
+          9,
+          10,
+        ]);
+        assert.deepEqual(RA.flattenDepth(2, list), [
+          1,
+          2,
+          3,
+          4,
+          5,
+          6,
+          [[7], 8],
+          9,
+          10,
+        ]);
+        assert.deepEqual(RA.flattenDepth(3, list), [
+          1,
+          2,
+          3,
+          4,
+          5,
+          6,
+          [7],
+          8,
+          9,
+          10,
+        ]);
+        assert.deepEqual(RA.flattenDepth(4, list), [
+          1,
+          2,
+          3,
+          4,
+          5,
+          6,
+          7,
+          8,
+          9,
+          10,
+        ]);
+        assert.deepEqual(RA.flattenDepth(5, list), [
+          1,
+          2,
+          3,
+          4,
+          5,
+          6,
+          7,
+          8,
+          9,
+          10,
+        ]);
+      });
+    });
+
+    context('and the depth is positive float number', function() {
+      specify('should ceil depth to positive integer number', function() {
+        const depth = 2.2;
+        const actual = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+        const expected = [1, 2, 3, 4, 5, 6, [7], 8, 9, 10];
+
+        assert.deepEqual(RA.flattenDepth(depth, actual), expected);
+      });
+    });
+
+    context('and the depth is negative float number', function() {
+      specify('should return shallow copy of the list', function() {
+        const depth = -2.2;
+        const actual = [1, [2], [3, [4, 5], 6, [[[7], 8]]], 9, 10];
+        const expected = RA.flattenDepth(depth, actual);
+
+        assert.deepEqual(actual, expected);
+        assert.notStrictEqual(actual, expected);
+      });
+    });
+  });
+
+  it('should curry', function() {
+    assert.deepEqual(RA.flattenDepth(0, []), []);
+    assert.deepEqual(RA.flattenDepth(0)([]), []);
   });
 });