-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: π₯ Add generic comparison function
Add comparison function and start change over to using it β Closes: #220
- Loading branch information
Ryan Smee
committed
Apr 1, 2022
1 parent
af09284
commit 6833592
Showing
8 changed files
with
173 additions
and
22 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
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
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
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
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { objectIsUnique } from '../../lib/core/core'; | ||
import { randUser, User } from '../../lib/user'; | ||
import { randUuid } from '../../lib/uuid'; | ||
import { randFirstName } from '../../lib/first-name'; | ||
|
||
describe('valueExistsInObjectArray', () => { | ||
describe("keys contains a key that doesn't exist", () => { | ||
let array: User[]; | ||
let newItem: User; | ||
let keys: string[]; | ||
|
||
beforeEach(() => { | ||
array = randUser({ length: 3 }); | ||
newItem = randUser(); | ||
keys = ['id', 'noExistentKey', 'firstName']; | ||
}); | ||
|
||
it('should throw error', () => { | ||
expect(() => objectIsUnique(newItem, array, keys)).toThrow( | ||
'noExistentKey does not exist in this array value type' | ||
); | ||
}); | ||
}); | ||
|
||
describe('1 key is passed', () => { | ||
let array: User[]; | ||
let newItem: User; | ||
let keys: string[]; | ||
|
||
beforeEach(() => { | ||
array = randUser({ length: 3 }); | ||
newItem = randUser(); | ||
keys = ['id']; | ||
}); | ||
|
||
describe('passed item matches and array item have 1 matching key value', () => { | ||
let sharedId: string; | ||
|
||
beforeEach(() => { | ||
sharedId = randUuid(); | ||
newItem.id = sharedId; | ||
array[1].id = sharedId; | ||
}); | ||
|
||
it('should return true', () => { | ||
const result = objectIsUnique(newItem, array, keys); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('passed item matches and array item have 0 matching key values', () => { | ||
beforeEach(() => { | ||
array[0].id = randUuid() + '0'; | ||
array[1].id = randUuid() + '1'; | ||
array[2].id = randUuid() + '2'; | ||
newItem.id = randUuid() + '3'; | ||
}); | ||
|
||
it('should return true', () => { | ||
const result = objectIsUnique(newItem, array, keys); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('multiple keys are passed', () => { | ||
let array: User[]; | ||
let newItem: User; | ||
let keys: string[]; | ||
|
||
beforeEach(() => { | ||
array = randUser({ length: 3 }); | ||
newItem = randUser(); | ||
keys = ['id', 'firstName']; | ||
}); | ||
|
||
describe('passed item matches and array item have 1 matching key value', () => { | ||
let sharedFirstName: string; | ||
|
||
beforeEach(() => { | ||
sharedFirstName = randFirstName(); | ||
newItem.firstName = sharedFirstName; | ||
array[1].firstName = sharedFirstName; | ||
}); | ||
|
||
it('should return true', () => { | ||
const result = objectIsUnique(newItem, array, keys); | ||
|
||
expect(result).toEqual(true); | ||
}); | ||
}); | ||
|
||
describe('passed item matches and array item have 0 matching key values', () => { | ||
beforeEach(() => { | ||
array[0].id = randUuid() + '0'; | ||
array[0].firstName = randFirstName() + '0'; | ||
array[1].id = randUuid() + '1'; | ||
array[1].id = randFirstName() + '1'; | ||
array[2].id = randUuid() + '2'; | ||
array[2].id = randFirstName() + '2'; | ||
newItem.id = randUuid() + '3'; | ||
newItem.id = randFirstName() + '3'; | ||
}); | ||
|
||
it('should return true', () => { | ||
const result = objectIsUnique(newItem, array, keys); | ||
|
||
expect(result).toEqual(false); | ||
}); | ||
}); | ||
}); | ||
}); |