2018-10-12 03:50:48 +08:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
import QueryMatcher from "../../src/autocomplete/QueryMatcher";
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
const OBJECTS = [
|
|
|
|
{ name: "Mel B", nick: "Scary" },
|
|
|
|
{ name: "Mel C", nick: "Sporty" },
|
|
|
|
{ name: "Emma", nick: "Baby" },
|
|
|
|
{ name: "Geri", nick: "Ginger" },
|
|
|
|
{ name: "Victoria", nick: "Posh" },
|
|
|
|
];
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const NONWORDOBJECTS = [{ name: "B.O.B" }, { name: "bob" }];
|
2018-10-12 04:04:50 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("QueryMatcher", function () {
|
|
|
|
it("Returns results by key", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("Geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(1);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns results by prefix", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("Ge");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(1);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Matches case-insensitive", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(1);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Matches ignoring accents", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher([{ name: "Gëri", foo: 46 }], { keys: ["name"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("geri");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(1);
|
|
|
|
expect(results[0].foo).toBe(46);
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns multiple results in order of search string appearance", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, { keys: ["name", "nick"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("or");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Mel C");
|
|
|
|
expect(results[1].name).toBe("Victoria");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
qm.setObjects(OBJECTS.slice().reverse());
|
2022-12-12 19:24:14 +08:00
|
|
|
const reverseResults = qm.match("or");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
// should still be in the same order: search string position
|
|
|
|
// takes precedence over input order
|
|
|
|
expect(reverseResults.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(reverseResults[0].name).toBe("Mel C");
|
|
|
|
expect(reverseResults[1].name).toBe("Victoria");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns results with search string in same place according to key index", function () {
|
2020-06-18 11:31:02 +08:00
|
|
|
const objects = [
|
|
|
|
{ name: "a", first: "hit", second: "miss", third: "miss" },
|
|
|
|
{ name: "b", first: "miss", second: "hit", third: "miss" },
|
|
|
|
{ name: "c", first: "miss", second: "miss", third: "hit" },
|
|
|
|
];
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(objects, { keys: ["second", "first", "third"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("hit");
|
2020-06-18 11:31:02 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(3);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("b");
|
|
|
|
expect(results[1].name).toBe("a");
|
|
|
|
expect(results[2].name).toBe("c");
|
2020-06-18 11:31:02 +08:00
|
|
|
|
|
|
|
qm.setObjects(objects.slice().reverse());
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const reverseResults = qm.match("hit");
|
2020-06-18 11:31:02 +08:00
|
|
|
|
|
|
|
// should still be in the same order: key index
|
|
|
|
// takes precedence over input order
|
|
|
|
expect(reverseResults.length).toBe(3);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(reverseResults[0].name).toBe("b");
|
|
|
|
expect(reverseResults[1].name).toBe("a");
|
|
|
|
expect(reverseResults[2].name).toBe("c");
|
2020-06-18 11:31:02 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns results with search string in same place and key in same place in insertion order", function () {
|
2021-06-29 20:11:58 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, { keys: ["name"] });
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("Mel");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Mel B");
|
|
|
|
expect(results[1].name).toBe("Mel C");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
qm.setObjects(OBJECTS.slice().reverse());
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const reverseResults = qm.match("Mel");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(reverseResults.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(reverseResults[0].name).toBe("Mel C");
|
|
|
|
expect(reverseResults[1].name).toBe("Mel B");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns numeric results in correct order (input pos)", function () {
|
2018-10-12 03:50:48 +08:00
|
|
|
// regression test for depending on object iteration order
|
2022-12-12 19:24:14 +08:00
|
|
|
const qm = new QueryMatcher([{ name: "123456badger" }, { name: "123456" }], { keys: ["name"] });
|
|
|
|
const results = qm.match("123456");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("123456badger");
|
|
|
|
expect(results[1].name).toBe("123456");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns numeric results in correct order (query pos)", function () {
|
|
|
|
const qm = new QueryMatcher([{ name: "999999123456" }, { name: "123456badger" }], { keys: ["name"] });
|
|
|
|
const results = qm.match("123456");
|
2018-10-12 03:50:48 +08:00
|
|
|
|
|
|
|
expect(results.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("123456badger");
|
|
|
|
expect(results[1].name).toBe("999999123456");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Returns results by function", function () {
|
2018-10-12 03:50:48 +08:00
|
|
|
const qm = new QueryMatcher(OBJECTS, {
|
|
|
|
keys: ["name"],
|
2022-12-12 19:24:14 +08:00
|
|
|
funcs: [(x) => x.name.replace("Mel", "Emma")],
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("Emma");
|
2018-10-12 03:50:48 +08:00
|
|
|
expect(results.length).toBe(3);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("Emma");
|
|
|
|
expect(results[1].name).toBe("Mel B");
|
|
|
|
expect(results[2].name).toBe("Mel C");
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|
2018-10-12 04:04:50 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Matches words only by default", function () {
|
2018-10-12 04:04:50 +08:00
|
|
|
const qm = new QueryMatcher(NONWORDOBJECTS, { keys: ["name"] });
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("bob");
|
2018-10-12 04:04:50 +08:00
|
|
|
expect(results.length).toBe(2);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("B.O.B");
|
|
|
|
expect(results[1].name).toBe("bob");
|
2018-10-12 04:04:50 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("Matches all chars with words-only off", function () {
|
2018-10-12 04:04:50 +08:00
|
|
|
const qm = new QueryMatcher(NONWORDOBJECTS, {
|
|
|
|
keys: ["name"],
|
|
|
|
shouldMatchWordsOnly: false,
|
2021-04-27 23:23:27 +08:00
|
|
|
});
|
2018-10-12 04:04:50 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const results = qm.match("bob");
|
2018-10-12 04:04:50 +08:00
|
|
|
expect(results.length).toBe(1);
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(results[0].name).toBe("bob");
|
2018-10-12 04:04:50 +08:00
|
|
|
});
|
2018-10-12 03:50:48 +08:00
|
|
|
});
|