Browse Source

chore: improve tests

e22m4u 2 years ago
parent
commit
9098c40375
2 changed files with 84 additions and 43 deletions
  1. 34 0
      src/filter/fields-clause-tool.spec.js
  2. 50 43
      src/filter/order-clause-tool.spec.js

+ 34 - 0
src/filter/fields-clause-tool.spec.js

@@ -13,6 +13,12 @@ describe('FieldsClauseTool', function () {
   describe('filter', function () {
   describe('filter', function () {
     describe('object', function () {
     describe('object', function () {
       describe('single field', function () {
       describe('single field', function () {
+        it('does not throw an error if the given field is not exist', function () {
+          const object = {foo: 'a1', bar: 'a2', baz: 'a3'};
+          const res = T.filter(object, MODEL_NAME, 'qux');
+          expect(res).to.be.eql({});
+        });
+
         it('requires the first argument to be an object', function () {
         it('requires the first argument to be an object', function () {
           const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
           const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
           const error = v =>
           const error = v =>
@@ -86,6 +92,12 @@ describe('FieldsClauseTool', function () {
       });
       });
 
 
       describe('multiple fields', function () {
       describe('multiple fields', function () {
+        it('does not throw an error if multiple fields is not exist', function () {
+          const object = {foo: 'a1', bar: 'a2', baz: 'a3'};
+          const res = T.filter(object, MODEL_NAME, ['bar', 'qux']);
+          expect(res).to.be.eql({bar: 'a2'});
+        });
+
         it('requires the first argument to be an object', function () {
         it('requires the first argument to be an object', function () {
           const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
           const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
           const error = v =>
           const error = v =>
@@ -162,6 +174,16 @@ describe('FieldsClauseTool', function () {
 
 
     describe('array', function () {
     describe('array', function () {
       describe('single field', function () {
       describe('single field', function () {
+        it('does not throw an error if the given field is not exist', function () {
+          const objects = [
+            {foo: 'a1', bar: 'a2', baz: 'a3'},
+            {foo: 'b1', bar: 'b2', baz: 'b3'},
+            {foo: 'c1', bar: 'c2', baz: 'c3'},
+          ];
+          const res = T.filter(objects, MODEL_NAME, 'qux');
+          expect(res).to.be.eql([{}, {}, {}]);
+        });
+
         it('requires the first argument to be an array of objects', function () {
         it('requires the first argument to be an array of objects', function () {
           const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
           const throwable = v => () => T.filter(v, MODEL_NAME, 'bar');
           const error = v =>
           const error = v =>
@@ -267,6 +289,18 @@ describe('FieldsClauseTool', function () {
       });
       });
 
 
       describe('multiple fields', function () {
       describe('multiple fields', function () {
+        it('does not throw an error if multiple fields is not exist', function () {
+          const object = [
+            {foo: 'a1', bar: 'a2', baz: 'a3'},
+            {foo: 'b1', bar: 'b2', baz: 'b3'},
+            {foo: 'c1', bar: 'c2', baz: 'c3'},
+          ];
+          const res = T.filter(object, MODEL_NAME, ['bar', 'qux']);
+          expect(res[0]).to.be.eql({bar: 'a2'});
+          expect(res[1]).to.be.eql({bar: 'b2'});
+          expect(res[2]).to.be.eql({bar: 'c2'});
+        });
+
         it('requires the first argument to be an array of objects', function () {
         it('requires the first argument to be an array of objects', function () {
           const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
           const throwable = v => () => T.filter(v, MODEL_NAME, ['bar', 'baz']);
           const error = v =>
           const error = v =>

+ 50 - 43
src/filter/order-clause-tool.spec.js

@@ -6,50 +6,17 @@ const S = new OrderClauseTool();
 
 
 describe('OrderClauseTool', function () {
 describe('OrderClauseTool', function () {
   describe('sort', function () {
   describe('sort', function () {
-    it('does not throw an error if a field does not exist', function () {
-      const objects = [{foo: 1}, {foo: 2}, {foo: 3}, {foo: 4}];
-      S.sort(objects, 'bar');
-      expect(objects).to.have.length(4);
-      expect(objects[0].foo).to.be.eq(1);
-      expect(objects[1].foo).to.be.eq(2);
-      expect(objects[2].foo).to.be.eq(3);
-      expect(objects[3].foo).to.be.eq(4);
-    });
-
-    it('does not throw an error if multiple fields are not exist', function () {
-      const objects = [{foo: 1}, {foo: 2}, {foo: 3}, {foo: 4}];
-      S.sort(objects, ['bar', 'baz']);
-      expect(objects).to.have.length(4);
-      expect(objects[0].foo).to.be.eq(1);
-      expect(objects[1].foo).to.be.eq(2);
-      expect(objects[2].foo).to.be.eq(3);
-      expect(objects[3].foo).to.be.eq(4);
-    });
-
-    it('does not throw an error if a nested field does not exist', function () {
-      const objects = [
-        {foo: 1},
-        {foo: 2, bar: undefined},
-        {foo: 3, bar: {baz: undefined}},
-        {foo: 4, bar: {baz: 1}},
-      ];
-      S.sort(objects, 'bar.baz');
-      expect(objects).to.have.length(4);
-      expect(objects[0].foo).to.be.eq(1);
-      expect(objects[1].foo).to.be.eq(2);
-      expect(objects[2].foo).to.be.eq(3);
-      expect(objects[3].foo).to.be.eq(4);
-    });
-
-    it('throws an error if a given property is not a string', function () {
-      const throwable = () => S.sort([], 10);
-      expect(throwable).to.throw(
-        'The provided option "order" should be a non-empty String ' +
-          'or an Array of non-empty String, but 10 given.',
-      );
-    });
-
     describe('single field', function () {
     describe('single field', function () {
+      it('does not throw an error if the given field is not exist', function () {
+        const objects = [{foo: 1}, {foo: 2}, {foo: 3}, {foo: 4}];
+        S.sort(objects, 'bar');
+        expect(objects).to.have.length(4);
+        expect(objects[0].foo).to.be.eq(1);
+        expect(objects[1].foo).to.be.eq(2);
+        expect(objects[2].foo).to.be.eq(3);
+        expect(objects[3].foo).to.be.eq(4);
+      });
+
       describe('with number values', function () {
       describe('with number values', function () {
         it('orders in ascending by default', function () {
         it('orders in ascending by default', function () {
           const objects = [{foo: 2}, {foo: 3}, {foo: 1}, {foo: 4}];
           const objects = [{foo: 2}, {foo: 3}, {foo: 1}, {foo: 4}];
@@ -116,6 +83,16 @@ describe('OrderClauseTool', function () {
     });
     });
 
 
     describe('multiple fields', function () {
     describe('multiple fields', function () {
+      it('does not throw an error if multiple fields are not exist', function () {
+        const objects = [{foo: 1}, {foo: 2}, {foo: 3}, {foo: 4}];
+        S.sort(objects, ['bar', 'baz']);
+        expect(objects).to.have.length(4);
+        expect(objects[0].foo).to.be.eq(1);
+        expect(objects[1].foo).to.be.eq(2);
+        expect(objects[2].foo).to.be.eq(3);
+        expect(objects[3].foo).to.be.eq(4);
+      });
+
       describe('with number values', function () {
       describe('with number values', function () {
         it('orders in ascending by default', function () {
         it('orders in ascending by default', function () {
           const objects = [
           const objects = [
@@ -304,6 +281,21 @@ describe('OrderClauseTool', function () {
     });
     });
 
 
     describe('nested single field', function () {
     describe('nested single field', function () {
+      it('does not throw an error if the nested field is not exist', function () {
+        const objects = [
+          {foo: 1},
+          {foo: 2, bar: undefined},
+          {foo: 3, bar: {baz: undefined}},
+          {foo: 4, bar: {baz: 1}},
+        ];
+        S.sort(objects, 'bar.baz');
+        expect(objects).to.have.length(4);
+        expect(objects[0].foo).to.be.eq(1);
+        expect(objects[1].foo).to.be.eq(2);
+        expect(objects[2].foo).to.be.eq(3);
+        expect(objects[3].foo).to.be.eq(4);
+      });
+
       describe('with number values', function () {
       describe('with number values', function () {
         it('orders in ascending by default', function () {
         it('orders in ascending by default', function () {
           const objects = [
           const objects = [
@@ -400,6 +392,21 @@ describe('OrderClauseTool', function () {
     });
     });
 
 
     describe('nested multiple fields', function () {
     describe('nested multiple fields', function () {
+      it('does not throw an error if nested multiple fields are not exist', function () {
+        const objects = [
+          {foo: 1},
+          {foo: 2, bar: undefined},
+          {foo: 3, bar: {baz: undefined}},
+          {foo: 4, bar: {baz: 1}},
+        ];
+        S.sort(objects, ['bar.baz', 'qux']);
+        expect(objects).to.have.length(4);
+        expect(objects[0].foo).to.be.eq(1);
+        expect(objects[1].foo).to.be.eq(2);
+        expect(objects[2].foo).to.be.eq(3);
+        expect(objects[3].foo).to.be.eq(4);
+      });
+
       describe('with number values', function () {
       describe('with number values', function () {
         it('orders in ascending by default', function () {
         it('orders in ascending by default', function () {
           const objects = [
           const objects = [