Просмотр исходного кода

chore: improve tests of "transformValuesDeep"

e22m4u 2 лет назад
Родитель
Сommit
5a19b653bf
2 измененных файлов с 27 добавлено и 4 удалено
  1. 1 1
      src/utils/transform-values-deep.js
  2. 26 3
      src/utils/transform-values-deep.spec.js

+ 1 - 1
src/utils/transform-values-deep.js

@@ -11,7 +11,7 @@ export function transformValuesDeep(value, transformer) {
   if (!transformer || typeof transformer !== 'function')
     throw new InvalidArgumentError(
       'The second argument of "transformValuesDeep" ' +
-        'must be a Function, but %s given.',
+        'must be a Function, but %v given.',
       transformer,
     );
   if (Array.isArray(value)) {

+ 26 - 3
src/utils/transform-values-deep.spec.js

@@ -1,8 +1,9 @@
 import {expect} from 'chai';
+import {format} from '@e22m4u/js-format';
 import {transformValuesDeep} from './transform-values-deep.js';
 
 describe('transformValuesDeep', function () {
-  it('transforms property values of an object', function () {
+  it('transforms property values of the given object', function () {
     const object = {
       foo: 1,
       bar: {
@@ -24,13 +25,13 @@ describe('transformValuesDeep', function () {
     });
   });
 
-  it('transforms elements of an array', function () {
+  it('transforms elements of the given array', function () {
     const object = [1, 2, 3, [4, 5, 6, [7, 8, 9]]];
     const result = transformValuesDeep(object, String);
     expect(result).to.be.eql(['1', '2', '3', ['4', '5', '6', ['7', '8', '9']]]);
   });
 
-  it('transforms non-pure objects', function () {
+  it('transforms the Date instance', function () {
     const date = new Date();
     const str = String(date);
     const result1 = transformValuesDeep(date, String);
@@ -44,4 +45,26 @@ describe('transformValuesDeep', function () {
     expect(result4).to.be.eql({date: str});
     expect(result5).to.be.eql({foo: {date: str}});
   });
+
+  it('requires the second argument to be a function', function () {
+    const throwable = v => () => transformValuesDeep('val', v);
+    const error = v =>
+      format(
+        'The second argument of "transformValuesDeep" ' +
+          'must be a Function, but %s given.',
+        v,
+      );
+    expect(throwable('str')).to.throw(error('"str"'));
+    expect(throwable('')).to.throw(error('""'));
+    expect(throwable(10)).to.throw(error('10'));
+    expect(throwable(0)).to.throw(error('0'));
+    expect(throwable(true)).to.throw(error('true'));
+    expect(throwable(false)).to.throw(error('false'));
+    expect(throwable([])).to.throw(error('Array'));
+    expect(throwable({})).to.throw(error('Object'));
+    expect(throwable(undefined)).to.throw(error('undefined'));
+    expect(throwable(null)).to.throw(error('null'));
+    throwable(() => undefined)();
+    throwable(function () {})();
+  });
 });