-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rankItem in match-sorter-utils use threshold when using accessors (
#4417) - rankItem was not using the accessor specific threshold and neither the general threshold - rankItem did not set `passed` to true when using accessors
- Loading branch information
Showing
2 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
packages/match-sorter-utils/__tests__/match-sorter-utils.test.ts
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,103 @@ | ||
import {rankings, rankItem} from "../src"; | ||
|
||
interface Person { | ||
firstName: string; | ||
lastName: string; | ||
email: string; | ||
} | ||
|
||
|
||
const testPerson: Person = { | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "j.doe@email.com" | ||
} | ||
|
||
describe("match-sorter-utils", () => { | ||
describe("rankItem", () => { | ||
describe("with accessorFn", () => { | ||
it("CASE_SENSITIVE_EQUAL", () => { | ||
const ranking = rankItem(testPerson, "John", {accessors: [item => item.firstName, item => item.lastName, item => item.email]}) | ||
expect(ranking.rank).toBe(rankings.CASE_SENSITIVE_EQUAL); | ||
expect(ranking.passed).toBe(true); | ||
expect(ranking.rankedValue).toBe(testPerson.firstName); | ||
expect(ranking.accessorIndex).toBe(0); | ||
expect(ranking.accessorThreshold).toBe(1); | ||
}) | ||
|
||
it("NO_MATCH", () => { | ||
const ranking = rankItem(testPerson, "Tom", {accessors: [item => item.firstName, item => item.lastName, item => item.email]}) | ||
expect(ranking.rank).toBe(rankings.NO_MATCH); | ||
expect(ranking.passed).toBe(false); | ||
expect(ranking.rankedValue).toBe(testPerson); | ||
expect(ranking.accessorIndex).toBe(-1); | ||
expect(ranking.accessorThreshold).toBe(1); | ||
}) | ||
}) | ||
|
||
describe("with accessorOptions and custom Threshold", () => { | ||
it("CASE_SENSITIVE_EQUAL", () => { | ||
const ranking = rankItem(testPerson, "John", { | ||
accessors: [{ | ||
accessor: item => item.firstName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.lastName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.email, | ||
threshold: rankings.MATCHES | ||
}] | ||
}) | ||
expect(ranking.rank).toBe(rankings.CASE_SENSITIVE_EQUAL); | ||
expect(ranking.passed).toBe(true); | ||
expect(ranking.rankedValue).toBe(testPerson.firstName); | ||
expect(ranking.accessorIndex).toBe(0); | ||
expect(ranking.accessorThreshold).toBe(rankings.CONTAINS); | ||
}) | ||
|
||
it("ACRONYM but threshold is CONTAINS", () => { | ||
const ranking = rankItem(testPerson, "jd", { | ||
threshold: rankings.ACRONYM, | ||
accessors: [{ | ||
accessor: item => item.firstName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.lastName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => `${item.firstName} ${item.lastName}`, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.email, | ||
threshold: rankings.CONTAINS | ||
}] | ||
}) | ||
expect(ranking.rank).toBe(rankings.NO_MATCH); | ||
expect(ranking.passed).toBe(false); | ||
expect(ranking.rankedValue).toBe(testPerson); | ||
expect(ranking.accessorIndex).toBe(-1); | ||
expect(ranking.accessorThreshold).toBe(rankings.ACRONYM); | ||
}) | ||
|
||
it("NO_MATCH", () => { | ||
const ranking = rankItem(testPerson, "Tom", { | ||
accessors: [{ | ||
accessor: item => item.firstName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.lastName, | ||
threshold: rankings.CONTAINS | ||
}, { | ||
accessor: item => item.email, | ||
threshold: rankings.MATCHES | ||
}]}) | ||
expect(ranking.rank).toBe(rankings.NO_MATCH); | ||
expect(ranking.passed).toBe(false); | ||
expect(ranking.rankedValue).toBe(testPerson); | ||
expect(ranking.accessorIndex).toBe(-1); | ||
expect(ranking.accessorThreshold).toBe(1); | ||
}) | ||
}) | ||
}) | ||
}) |
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