Skip to content

Commit

Permalink
Merge pull request #4 from xjdesigns/SearchTests
Browse files Browse the repository at this point in the history
chore(searchTests): Add 100% coverage to search util
  • Loading branch information
xjdesigns committed Apr 2, 2024
2 parents 59af224 + 625ca6c commit 950b133
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 10 deletions.
75 changes: 74 additions & 1 deletion src/__tests__/searchUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ describe('searchDataByKeys()', () => {
expect(res.length).toEqual(1)
})

it('should return no results when search does not match', () => {
const data = [{
name: 'Jason'
}, {
name: 'Trevor'
}]

const res = searchDataByKeys(data, 'Jason', ['firstName'])
expect(res.length).toEqual(0)
})

it('should return no results when search does not match, 2 levels', () => {
const data = [{
name: 'Jason',
job: {
title: 'Engineer'
}
}, {
name: 'Trevor',
job: {
title: 'Engineer'
}
}]

const res = searchDataByKeys(data, 'Engineer', ['job.description'])
expect(res.length).toEqual(0)
})

it('should return data from search term and nested keys, number to string conversion', () => {
const data = [{
name: 'Jason',
Expand Down Expand Up @@ -65,7 +93,7 @@ describe('searchDataByKeys()', () => {
expect(res.length).toEqual(2)
})

it('should return matches when allowed nesting is set to true', () => {
it('should return matches when allowed nesting is set to true, 3 levels', () => {
const data = [{
name: 'Jason',
person: {
Expand All @@ -88,6 +116,29 @@ describe('searchDataByKeys()', () => {
expect(res.length).toEqual(1)
})

it('should return matches when allowed nesting is set to true, 2 levels', () => {
const data = [{
name: 'Jason',
person: {
age: '40',
date: {
month: '05'
}
}
}, {
name: 'Trevor',
person: {
age: '25',
date: {
month: '02'
}
}
}]

const res = searchDataByKeys(data, '4', ['person.age'], { allowNested: true })
expect(res.length).toEqual(1)
})

it('should return all when no search term passed', () => {
const data = [{
name: 'Jason',
Expand Down Expand Up @@ -121,4 +172,26 @@ describe('searchDataByKeys()', () => {
const res = searchDataByKeys(data, '1', ['person.age'])
expect(res.length).toEqual(0)
})

it('should return no results if allow nesting is not true and lookup is more then 2', () => {
const data = [{
name: 'Jason',
person: {
age: '40'
}
}, {
name: 'Trevor',
person: {
age: '25'
}
}]

const res = searchDataByKeys(data, '1', ['person.date.month'])
expect(res.length).toEqual(0)
})

it('should handle default values when no args are passed', () => {
const res = searchDataByKeys()
expect(res.length).toEqual(0)
})
})
19 changes: 10 additions & 9 deletions src/searchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ export function searchDataByKeys (

// if allowed search nested objects for values
if (allowNested) {
if (s.length > 2) {
let t: string = '';
for (let ki = 0; ki < s.length; ki++) {
if (t) {
t = t[s[ki]]
} else {
t = d[s[ki]]
}
let t: string = '';
for (let ki = 0; ki < s.length; ki++) {
if (t) {
t = t[s[ki]]
} else {
t = d[s[ki]]
}
term = t;
}
term = t;
} else {
if (s.length <= 2) {
if (d[s[0]] && d[s[0]][s[1]]) {
term = term.concat(d[s[0]][s[1]]);
} else {
// eslint-disable-next-line no-console
console.warn('No match on search term');
}
} else {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 950b133

Please sign in to comment.