generated from maxsalles/js-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modify the return of diff function
- Loading branch information
Showing
4 changed files
with
76 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters