|
| 1 | +const expect = require('chai').expect; |
| 2 | +const { describe } = require('node-tdd'); |
| 3 | +const objectScan = require('object-scan'); |
| 4 | +const sampleSize = require('lodash.samplesize'); |
| 5 | +const cloneDeep = require('lodash.clonedeep'); |
| 6 | +const clone = require('../../src/core/clone'); |
| 7 | +const genData = require('./gen-data'); |
| 8 | + |
| 9 | +describe('Testing clone', { timeout: 100000 }, () => { |
| 10 | + it('Batch test (deep)', ({ fixture }) => { |
| 11 | + const refDiff = fixture('ref-diff'); |
| 12 | + const asRefDiff = fixture('as-ref-diff'); |
| 13 | + for (let x = 0; x < 5000; x += 1) { |
| 14 | + const data = genData(); |
| 15 | + const dataX = cloneDeep(data); |
| 16 | + const cloned = clone(data); |
| 17 | + expect(dataX).to.deep.equal(data); |
| 18 | + expect(data).to.deep.equal(cloned); |
| 19 | + expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data)); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + it('Batch test (shallow)', ({ fixture }) => { |
| 24 | + const refDiff = fixture('ref-diff'); |
| 25 | + const asRefDiff = fixture('as-ref-diff'); |
| 26 | + for (let x = 0; x < 5000; x += 1) { |
| 27 | + const data = genData(); |
| 28 | + const dataX = cloneDeep(data); |
| 29 | + const cloned = clone(data, ['**']); |
| 30 | + expect(dataX).to.deep.equal(data); |
| 31 | + expect(data).to.deep.equal(cloned); |
| 32 | + expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data, ['**'])); |
| 33 | + } |
| 34 | + }); |
| 35 | + |
| 36 | + it('Batch test (random shallow)', ({ fixture }) => { |
| 37 | + const refDiff = fixture('ref-diff'); |
| 38 | + const asRefDiff = fixture('as-ref-diff'); |
| 39 | + for (let x = 0; x < 5000; x += 1) { |
| 40 | + const data = genData(); |
| 41 | + const dataX = cloneDeep(data); |
| 42 | + const allKeys = objectScan(['**'], { joined: true })(data); |
| 43 | + const selectedKeys = sampleSize(allKeys, Math.floor(Math.random() * allKeys.length) + 1); |
| 44 | + const cloned = clone(data, selectedKeys); |
| 45 | + expect(dataX).to.deep.equal(data); |
| 46 | + expect(data).to.deep.equal(cloned); |
| 47 | + expect(refDiff(data, cloned)).to.deep.equal(asRefDiff(data, selectedKeys)); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + it('Batch test (random exclude)', ({ fixture }) => { |
| 52 | + const cloneWithout = fixture('clone-without'); |
| 53 | + for (let x = 0; x < 5000; x += 1) { |
| 54 | + const data = genData(); |
| 55 | + const dataX = cloneDeep(data); |
| 56 | + const allKeys = objectScan(['**'], { joined: true })(data); |
| 57 | + const selectedKeys = sampleSize(allKeys, Math.floor(Math.random() * allKeys.length) + 1); |
| 58 | + const excludeKeys = selectedKeys.map((k) => `!${k}`); |
| 59 | + const cloned = clone(data, excludeKeys); |
| 60 | + expect(dataX).to.deep.equal(data); |
| 61 | + expect(cloned).to.deep.equal(cloneWithout(data, selectedKeys)); |
| 62 | + } |
| 63 | + }); |
| 64 | + |
| 65 | + it('Test simple', () => { |
| 66 | + const data = { |
| 67 | + a: 1, |
| 68 | + b: { x: 2, y: { /* complex object */ } }, |
| 69 | + c: [{ /* complex object */ }, { z: 3 }] |
| 70 | + }; |
| 71 | + const cloned = clone(data, ['b.y', 'c[0]']); |
| 72 | + expect(data).to.deep.equal(cloned); |
| 73 | + expect(data).to.not.equal(cloned); |
| 74 | + expect(data.b).to.not.equal(cloned.b); |
| 75 | + expect(data.y).to.equal(cloned.y); |
| 76 | + expect(data.c[0]).to.equal(cloned.c[0]); |
| 77 | + expect(data.c[1]).to.not.equal(cloned.c[1]); |
| 78 | + }); |
| 79 | + |
| 80 | + it('Test shallow clone', () => { |
| 81 | + const data = { a: {} }; |
| 82 | + const cloned = clone(data, ['**']); |
| 83 | + expect(data).to.deep.equal(cloned); |
| 84 | + expect(data).to.not.equal(cloned); |
| 85 | + expect(data.a).to.equal(cloned.a); |
| 86 | + }); |
| 87 | + |
| 88 | + it('Test exclude', () => { |
| 89 | + const data = { a: {} }; |
| 90 | + const cloned = clone(data, ['!a']); |
| 91 | + expect(cloned).to.deep.equal({}); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Test complex exclude one', ({ fixture }) => { |
| 95 | + const cloneWithout = fixture('clone-without'); |
| 96 | + const data = { C: { A: undefined, C: [] }, B: [] }; |
| 97 | + const cloned = clone(data, ['!C', '!C.A', '!B']); |
| 98 | + const excluded = cloneWithout(data, ['C', 'C.A', 'B']); |
| 99 | + expect(cloned).to.deep.equal(excluded); |
| 100 | + }); |
| 101 | + |
| 102 | + it('Test complex exclude two', ({ fixture }) => { |
| 103 | + const cloneWithout = fixture('clone-without'); |
| 104 | + const data = { B: {}, C: {} }; |
| 105 | + const cloned = clone(data, ['!C']); |
| 106 | + const excluded = cloneWithout(data, ['C']); |
| 107 | + expect(cloned).to.deep.equal(excluded); |
| 108 | + }); |
| 109 | + |
| 110 | + it('Test complex exclude three', ({ fixture }) => { |
| 111 | + const cloneWithout = fixture('clone-without'); |
| 112 | + const data = { B: [2, []] }; |
| 113 | + const cloned = clone(data, ['!B[0]']); |
| 114 | + const excluded = cloneWithout(data, ['B[0]']); |
| 115 | + expect(cloned).to.deep.equal(excluded); |
| 116 | + }); |
| 117 | + |
| 118 | + it('Test exclude, shallow and deep clone', () => { |
| 119 | + const data = { a: {}, b: {}, c: [{}, {}] }; |
| 120 | + const cloned = clone(data, ['b', '!c[0]', 'c[1]']); |
| 121 | + expect(cloned).to.deep.equal({ a: {}, b: {}, c: [{}] }); |
| 122 | + expect(data.a).to.not.equal(cloned.a); |
| 123 | + expect(data.b).to.equal(cloned.b); |
| 124 | + expect(data.c).to.not.equal(cloned.c); |
| 125 | + expect(data.c[1]).to.equal(cloned.c[0]); |
| 126 | + }); |
| 127 | +}); |
0 commit comments