Skip to content

Commit

Permalink
feat: modify the return of diff function
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsalles committed May 30, 2021
1 parent fac5468 commit 87a6414
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 29 deletions.
11 changes: 9 additions & 2 deletions src/diff.js
Original file line number Diff line number Diff line change
@@ -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
}
}
4 changes: 2 additions & 2 deletions src/perform.js → src/getDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ 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) => {
const heaped = [originalWrapped, derivedWrapped].map(wrapped => heap(wrapped, property))

return [...result, ...(
heaped.every(item => item && item.type === 'object')
? perform(...heaped)
? getDetails(...heaped)
: [getChange(...heaped) || getDeletion(...heaped) || getDefinition(...heaped)].filter(Boolean)
)]
}, [])
Expand Down
84 changes: 62 additions & 22 deletions test/diff.spec.js
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
})
})
6 changes: 3 additions & 3 deletions test/perform.spec.js → test/getDetails.spec.js
Original file line number Diff line number Diff line change
@@ -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' }],
Expand Down Expand Up @@ -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])
})
})
})

0 comments on commit 87a6414

Please sign in to comment.