From 87a64143f625c6a1604f60566607671a25d5cfb6 Mon Sep 17 00:00:00 2001 From: Max Salles Date: Sun, 30 May 2021 20:45:09 -0300 Subject: [PATCH] feat: modify the return of diff function --- src/diff.js | 11 ++- src/{perform.js => getDetails.js} | 4 +- test/diff.spec.js | 84 +++++++++++++++----- test/{perform.spec.js => getDetails.spec.js} | 6 +- 4 files changed, 76 insertions(+), 29 deletions(-) rename src/{perform.js => getDetails.js} (85%) rename test/{perform.spec.js => getDetails.spec.js} (98%) diff --git a/src/diff.js b/src/diff.js index 4c4a8cc..a6d8288 100644 --- a/src/diff.js +++ b/src/diff.js @@ -1,6 +1,13 @@ -import perform from './perform' +import getDetails from './getDetails' import wrap from './wrap' export default function diff (original, derived) { - return perform(wrap(original), wrap(derived)) + const details = getDetails(wrap(original), wrap(derived)) + + return { + hasChanged: !!details.length || ( + typeof original !== 'object' && typeof derived !== 'object' && original !== derived + ), + details + } } diff --git a/src/perform.js b/src/getDetails.js similarity index 85% rename from src/perform.js rename to src/getDetails.js index 7e6a194..b401a52 100644 --- a/src/perform.js +++ b/src/getDetails.js @@ -4,7 +4,7 @@ import getDeletion from './getDeletion' import getProperties from './getProperties' import heap from './heap' -export default function perform (originalWrapped, derivedWrapped) { +export default function getDetails (originalWrapped, derivedWrapped) { const properties = getProperties(originalWrapped.value, derivedWrapped.value) return properties.reduce((result, property) => { @@ -12,7 +12,7 @@ export default function perform (originalWrapped, derivedWrapped) { return [...result, ...( heaped.every(item => item && item.type === 'object') - ? perform(...heaped) + ? getDetails(...heaped) : [getChange(...heaped) || getDeletion(...heaped) || getDefinition(...heaped)].filter(Boolean) )] }, []) diff --git a/test/diff.spec.js b/test/diff.spec.js index 3667759..1c75db3 100644 --- a/test/diff.spec.js +++ b/test/diff.spec.js @@ -1,31 +1,71 @@ -jest.mock('../src/perform') -jest.mock('../src/wrap') - import diff from '../src/diff' -import perform from '../src/perform' -import wrap from '../src/wrap' describe('diff', () => { - afterEach(() => { - wrap.mockClear() - perform.mockClear() - }) + describe('when both parameters are non-objects', () => { + describe('and are different', () => { + const original = 'value' + const derived = 'new value' + + it('returns "hasChanged" as "true" and "details" as empty array', () => { + const result = diff(original, derived) + + expect(result).toEqual({ + hasChanged: true, + details: expect.any(Array) + }) + + expect(result.details.length).toBe(0) + }) + }) + + describe('and are equal', () => { + const original = 'value' + const derived = original + + it('returns "hasChanged" as "false" and "details" as empty array', () => { + const result = diff(original, derived) + + expect(result).toEqual({ + hasChanged: false, + details: expect.any(Array) + }) + + expect(result.details.length).toBe(0) + }) + }) + + describe('when one of the parameters is an object', () => { + describe('and both are different', () => { + const original = { attribute: 'value' } + const derived = { attribute: 'new value' } + + it('returns "hasChanged" as "true" and "details" as non-empty array', () => { + const result = diff(original, derived) + + expect(result).toEqual({ + hasChanged: true, + details: expect.any(Array) + }) + + expect(result.details.length).not.toBe(0) + }) + }) - it('returns the result of calling "perform"', () => { - const original = {} - const derived = {} - const originalWrapped = {} - const derivedWrapped = {} - const result = [] + describe('and both are equal', () => { + const original = { attribute: 'value' } + const derived = original - wrap - .mockReturnValueOnce(originalWrapped) - .mockReturnValueOnce(derivedWrapped) + it('returns "hasChanged" as "false" and "details" as empty array', () => { + const result = diff(original, derived) - perform.mockReturnValue(result) + expect(result).toEqual({ + hasChanged: false, + details: expect.any(Array) + }) - expect(diff(original, derived)).toBe(result) - expect(wrap.mock.calls).toEqual([[original], [derived]]) - expect(perform).toBeCalledWith(originalWrapped, derivedWrapped) + expect(result.details.length).toBe(0) + }) + }) + }) }) }) diff --git a/test/perform.spec.js b/test/getDetails.spec.js similarity index 98% rename from test/perform.spec.js rename to test/getDetails.spec.js index dc2612c..5614c27 100644 --- a/test/perform.spec.js +++ b/test/getDetails.spec.js @@ -1,7 +1,7 @@ import wrap from '../src/wrap' -import perform from '../src/perform' +import getDetails from '../src/getDetails' -describe('perform', () => { +describe('getDetails', () => { const examples = [ /* 0 */ [{}, {}], /* 1 */ [{ property: 'value' }, { property: 'value' }], @@ -420,7 +420,7 @@ describe('perform', () => { it('returns a list of differences between two objects', () => { examples.forEach((example, i) => { - expect(perform(...example)).toEqual(expectations[i]) + expect(getDetails(...example)).toEqual(expectations[i]) }) }) })