|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var assert = require('proclaim'); |
| 4 | +var clone = require('../lib/utils/clone'); |
| 5 | + |
| 6 | +describe('clone', function() { |
| 7 | + it('should be a function', function() { |
| 8 | + assert.equal(typeof clone, 'function'); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should have an arity of 1', function() { |
| 12 | + assert.equal(clone.length, 1); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('object', function() { |
| 16 | + it('should return an object with the same contents as the input', function() { |
| 17 | + var input = { a: 1, b: 2, c: 3 }; |
| 18 | + var cloned = clone(input); |
| 19 | + |
| 20 | + assert.deepEqual(cloned, input); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should deeply clone nested objects', function() { |
| 24 | + var date = new Date(); |
| 25 | + var input = { |
| 26 | + a: { |
| 27 | + b: [1, 2, date, { hello: 'world' }] |
| 28 | + } |
| 29 | + }; |
| 30 | + var cloned = clone(input); |
| 31 | + |
| 32 | + assert.deepEqual(cloned, input); |
| 33 | + |
| 34 | + assert.deepEqual(cloned, input); |
| 35 | + assert.notStrictEqual(cloned.a, input.a); |
| 36 | + assert.notStrictEqual(cloned.a.b, input.a.b); |
| 37 | + assert.notStrictEqual(cloned.a.b[2], input.a.b[2]); |
| 38 | + assert.strictEqual(cloned.a.b[2].getTime(), input.a.b[2].getTime()); |
| 39 | + assert.deepEqual(cloned.a.b[3], input.a.b[3]); |
| 40 | + assert.notStrictEqual(cloned.a.b[3], input.a.b[3]); |
| 41 | + }); |
| 42 | + |
| 43 | + it('object with functions', function() { |
| 44 | + var func = function() { |
| 45 | + return 'original'; |
| 46 | + }; |
| 47 | + var input = { func: func }; |
| 48 | + var cloned = clone(input); |
| 49 | + |
| 50 | + assert.strictEqual(cloned.func, func); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('array', function() { |
| 55 | + it('should return an array with the same contents as the input', function() { |
| 56 | + var input = [1, 2, 3, '4']; |
| 57 | + var cloned = clone(input); |
| 58 | + |
| 59 | + assert.deepEqual(cloned, input); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should deeply clone nested complex data structures', function() { |
| 63 | + var input = [{}]; |
| 64 | + var cloned = clone(input); |
| 65 | + |
| 66 | + assert.notStrictEqual(cloned[0], input[0]); |
| 67 | + assert.deepEqual(cloned[0], input[0]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should return a new array object', function() { |
| 71 | + var input = [1, 2, 3, '4']; |
| 72 | + var cloned = clone(input); |
| 73 | + |
| 74 | + assert.notStrictEqual(cloned, input); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('regexp', function() { |
| 79 | + it('regexp', function() { |
| 80 | + var input = /hello/i; |
| 81 | + var cloned = clone(input); |
| 82 | + |
| 83 | + assert.strictEqual(cloned.toString(), input.toString()); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should return a new regexp object', function() { |
| 87 | + var input = /hello/i; |
| 88 | + var cloned = clone(input); |
| 89 | + |
| 90 | + assert.notStrictEqual(cloned, input); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('date', function() { |
| 95 | + it('should return a date with the same time as the input date', function() { |
| 96 | + var input = new Date(); |
| 97 | + var cloned = clone(input); |
| 98 | + |
| 99 | + assert.strictEqual(cloned.getTime(), input.getTime()); |
| 100 | + }); |
| 101 | + |
| 102 | + it('should return a new date object', function() { |
| 103 | + var input = new Date(); |
| 104 | + var cloned = clone(input); |
| 105 | + |
| 106 | + assert.notStrictEqual(cloned, input); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('other data types', function() { |
| 111 | + it('should pass other data types through untouched', function() { |
| 112 | + assert.strictEqual(clone('a'), 'a'); |
| 113 | + assert.strictEqual(clone({ a: 1 }).a, 1); |
| 114 | + }); |
| 115 | + }); |
| 116 | +}); |
0 commit comments