-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from SMAKSS/develop
feat(core): add test for core functions
- Loading branch information
Showing
13 changed files
with
2,247 additions
and
739 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 |
---|---|---|
|
@@ -9,3 +9,6 @@ lint-staged.config.cjs | |
|
||
# commitlint config | ||
commitlint.config.cjs | ||
|
||
# jest config | ||
jest.config.cjs |
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,12 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
roots: ['<rootDir>/src'], | ||
testMatch: [ | ||
'**/__tests__/**/*.+(ts|tsx|js)', | ||
'**/?(*.)+(spec|test).+(ts|tsx|js)' | ||
], | ||
transform: { | ||
'^.+\\.(ts|tsx)$': 'ts-jest' | ||
} | ||
}; |
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,44 @@ | ||
import { searchWithinObject, recursiveSearch } from './search-functions'; // Adjust import path | ||
import { SearchItem } from './types'; | ||
|
||
describe('searchWithinObject', () => { | ||
it('should add object to results if a match is found', () => { | ||
const person: SearchItem = { name: 'John', lastName: 'Doe' }; | ||
const results: SearchItem[] = []; | ||
searchWithinObject(person, ['name'], true, /John/i, results); | ||
expect(results).toContain(person); | ||
}); | ||
|
||
it('should not add object to results if no match is found', () => { | ||
const person: SearchItem = { name: 'John', lastName: 'Doe' }; | ||
const results: SearchItem[] = []; | ||
searchWithinObject(person, ['name'], true, /Jane/i, results); | ||
expect(results).toEqual([]); | ||
}); | ||
}); | ||
|
||
describe('recursiveSearch', () => { | ||
it('should add all matching items from an array', () => { | ||
const people: SearchItem[] = [ | ||
{ name: 'John', lastName: 'Doe' }, | ||
{ name: 'Jane', lastName: 'Doe' } | ||
]; | ||
const results: SearchItem[] = []; | ||
recursiveSearch(people, ['lastName'], true, /Doe/i, results); | ||
expect(results).toEqual(expect.arrayContaining(people)); | ||
}); | ||
|
||
it('should work recursively through nested arrays/objects', () => { | ||
const nestedPeople: SearchItem[] = [ | ||
{ | ||
name: 'John', | ||
lastName: 'Doe', | ||
contacts: [{ name: 'Jake', lastName: 'Doe' }] | ||
}, | ||
{ name: 'Jane', lastName: 'Doe' } | ||
]; | ||
const results: SearchItem[] = []; | ||
recursiveSearch(nestedPeople, ['lastName'], true, /Doe/i, results); | ||
expect(results.length).toBe(2); | ||
}); | ||
}); |
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,96 @@ | ||
import search from './search'; | ||
|
||
describe('search', () => { | ||
const people = [ | ||
{ name: 'John', lastName: 'Doe', age: 30 }, | ||
{ name: 'Jane', lastName: 'Smith', age: 25 }, | ||
{ name: 'Jake', lastName: 'Doe', age: 22 } | ||
]; | ||
|
||
it('should find items by lastName', () => { | ||
const options = { | ||
searchText: 'doe', | ||
searchItems: people, | ||
keys: ['lastName'], | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(2); | ||
expect(results).toEqual( | ||
expect.arrayContaining([expect.objectContaining({ lastName: 'Doe' })]) | ||
); | ||
}); | ||
|
||
it('should handle exact matches', () => { | ||
const options = { | ||
searchText: 'Doe', | ||
searchItems: people, | ||
keys: ['lastName'], | ||
exact: true | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(2); | ||
expect(results).toEqual( | ||
expect.arrayContaining([expect.objectContaining({ lastName: 'Doe' })]) | ||
); | ||
}); | ||
|
||
it('should be case insensitive', () => { | ||
const options = { | ||
searchText: 'smith', | ||
searchItems: people, | ||
keys: ['lastName'], | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(1); | ||
expect(results[0].lastName).toBe('Smith'); | ||
}); | ||
|
||
it('should return an empty array when no matches are found', () => { | ||
const options = { | ||
searchText: 'nonexistent', | ||
searchItems: people, | ||
keys: ['lastName'], | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(0); | ||
}); | ||
|
||
it('should search all keys if none are specified', () => { | ||
const options = { | ||
searchText: '25', | ||
searchItems: people, | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(1); | ||
expect(results[0].age).toBe(25); | ||
}); | ||
|
||
it('should include keys in the results if include is true', () => { | ||
const options = { | ||
searchText: 'John', | ||
searchItems: people, | ||
keys: ['name'], | ||
include: true, | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.length).toBe(1); | ||
expect(results[0]).toHaveProperty('name', 'John'); | ||
}); | ||
|
||
it('should not include keys in the results if include is false', () => { | ||
const options = { | ||
searchText: 'Jane', | ||
searchItems: people, | ||
keys: ['name'], | ||
include: false, | ||
exact: false | ||
}; | ||
const results = search(options); | ||
expect(results.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { SearchItem } from './types'; | ||
import { isKeyIncluded, addUniqueMatch } from './utils'; | ||
|
||
describe('isKeyIncluded', () => { | ||
const keys = ['name', 'age']; | ||
|
||
it('should return true when key is in the list and include is true', () => { | ||
expect(isKeyIncluded('name', keys, true)).toBeTruthy(); | ||
}); | ||
|
||
it('should return false when key is not in the list and include is true', () => { | ||
expect(isKeyIncluded('address', keys, true)).toBeFalsy(); | ||
}); | ||
|
||
it('should return true when key is not in the list and include is false', () => { | ||
expect(isKeyIncluded('address', keys, false)).toBeTruthy(); | ||
}); | ||
|
||
it('should return false when key is in the list and include is false', () => { | ||
expect(isKeyIncluded('name', keys, false)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('addUniqueMatch', () => { | ||
it('should add item to results if not already included', () => { | ||
const results: SearchItem[] = []; | ||
const item = { name: 'John', age: 30 }; | ||
addUniqueMatch(results, item); | ||
expect(results).toContain(item); | ||
}); | ||
|
||
it('should not add item to results if already included', () => { | ||
const item = { name: 'Jane', age: 25 }; | ||
const results = [item]; | ||
addUniqueMatch(results, item); | ||
expect(results).toEqual([item]); | ||
}); | ||
}); |
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,6 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"types": ["jest"] | ||
} | ||
} |
Oops, something went wrong.