cozy-dataproxy-lib
Version:
Library meant to be by Cozy Cloud's DataProxy apps for data manipulation
250 lines (240 loc) • 7.91 kB
JavaScript
;
var _cozyClient = require("cozy-client");
var _SearchEngine = require("./SearchEngine");
var consts = _interopRequireWildcard(require("./consts"));
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
jest.mock('flexsearch');
jest.mock('flexsearch/dist/module/lang/latin/simple');
jest.mock("./helpers/client", function () {
return {
getPouchLink: jest.fn()
};
});
jest.mock("./helpers/getSearchEncoder", function () {
return {
getSearchEncoder: jest.fn()
};
});
jest.mock("./consts", function () {
return {
LIMIT_DOCTYPE_SEARCH: 3,
SEARCH_SCHEMA: {
'io.cozy.files': ['name', 'path'],
'io.cozy.contacts': ['displayName', 'fullname'],
'io.cozy.apps': ['slug', 'name']
},
DOCTYPE_DEFAULT_ORDER: {
'io.cozy.apps': 0,
'io.cozy.contacts': 1,
'io.cozy.files': 2
},
FILES_DOCTYPE: 'io.cozy.files',
CONTACTS_DOCTYPE: 'io.cozy.contacts',
APPS_DOCTYPE: 'io.cozy.apps'
};
});
describe('sortSearchResults', function () {
var searchEngine;
beforeEach(function () {
var client = (0, _cozyClient.createMockClient)();
searchEngine = new _SearchEngine.SearchEngine(client);
});
afterEach(function () {
jest.clearAllMocks();
});
it('should sort results by doctype order', function () {
var searchResults = [{
doctype: consts.FILES_DOCTYPE,
doc: {
_type: consts.FILES_DOCTYPE
}
}, {
doctype: consts.APPS_DOCTYPE,
doc: {
_type: consts.APPS_DOCTYPE
}
}, {
doctype: consts.CONTACTS_DOCTYPE,
doc: {
_type: consts.CONTACTS_DOCTYPE
}
}];
var sortedResults = searchEngine.sortSearchResults(searchResults);
expect(sortedResults[0].doctype).toBe(consts.APPS_DOCTYPE);
expect(sortedResults[1].doctype).toBe(consts.CONTACTS_DOCTYPE);
expect(sortedResults[2].doctype).toBe(consts.FILES_DOCTYPE);
});
it('should sort apps by slug', function () {
var searchResults = [{
doctype: consts.APPS_DOCTYPE,
doc: {
slug: 'appB',
_type: consts.APPS_DOCTYPE
}
}, {
doctype: consts.APPS_DOCTYPE,
doc: {
slug: 'appA',
_type: consts.APPS_DOCTYPE
}
}];
var sortedResults = searchEngine.sortSearchResults(searchResults);
expect(sortedResults[0].doc.slug).toBe('appA');
expect(sortedResults[1].doc.slug).toBe('appB');
});
it('should sort contacts by displayName', function () {
var searchResults = [{
doctype: consts.CONTACTS_DOCTYPE,
doc: {
displayName: 'June',
_type: consts.CONTACTS_DOCTYPE
}
}, {
doctype: consts.CONTACTS_DOCTYPE,
doc: {
displayName: 'Alice',
_type: consts.CONTACTS_DOCTYPE
}
}];
var sortedResults = searchEngine.sortSearchResults(searchResults);
expect(sortedResults[0].doc.displayName).toBe('Alice');
expect(sortedResults[1].doc.displayName).toBe('June');
});
it('should sort files by type and name', function () {
var searchResults = [{
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'fileB',
type: 'file',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}, {
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'fileA',
type: 'file',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}, {
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'folderA',
type: 'directory',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}];
var sortedResults = searchEngine.sortSearchResults(searchResults);
expect(sortedResults[0].doc.type).toBe('directory'); // Folders should come first
expect(sortedResults[1].doc.name).toBe('fileA');
expect(sortedResults[2].doc.name).toBe('fileB');
});
it('should sort files first if they match on name, then path', function () {
var searchResults = [{
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'test11',
path: 'test/test11',
type: 'file',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}, {
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'test1',
path: 'test/test1',
type: 'file',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}, {
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'DirName1',
path: 'test1/path',
type: 'directory',
_type: consts.FILES_DOCTYPE
},
fields: ['path']
}, {
doctype: consts.FILES_DOCTYPE,
doc: {
name: 'DirName2',
path: 'test1/path',
type: 'directory',
_type: consts.FILES_DOCTYPE
},
fields: ['name']
}];
var sortedResults = searchEngine.sortSearchResults(searchResults);
expect(sortedResults[0].doc.name).toBe('DirName2'); // Dir match on name
expect(sortedResults[1].doc.name).toBe('test1'); // File match on name
expect(sortedResults[2].doc.name).toBe('test11'); // File match on name
expect(sortedResults[3].doc.name).toBe('DirName1'); // Directory
});
});
describe('limitSearchResults', function () {
var searchEngine;
beforeEach(function () {
var client = (0, _cozyClient.createMockClient)();
searchEngine = new _SearchEngine.SearchEngine(client);
});
afterEach(function () {
jest.clearAllMocks();
});
it('should return all results if doctype count is below or equal the limit', function () {
var searchResults = [{
doctype: consts.FILES_DOCTYPE,
id: 1
}, {
doctype: consts.FILES_DOCTYPE,
id: 2
}, {
doctype: consts.FILES_DOCTYPE,
id: 3
}];
var filteredResults = searchEngine.limitSearchResults(searchResults);
expect(filteredResults).toEqual(searchResults);
});
it('should filter results exceeding the limit for a specific doctype', function () {
var searchResults = [{
doctype: consts.FILES_DOCTYPE,
id: 1
}, {
doctype: consts.FILES_DOCTYPE,
id: 2
}, {
doctype: consts.FILES_DOCTYPE,
id: 3
}, {
doctype: consts.FILES_DOCTYPE,
id: 4
}, {
doctype: consts.CONTACTS_DOCTYPE,
id: 5
}];
var filteredResults = searchEngine.limitSearchResults(searchResults);
expect(filteredResults).toEqual([{
doctype: consts.FILES_DOCTYPE,
id: 1
}, {
doctype: consts.FILES_DOCTYPE,
id: 2
}, {
doctype: consts.FILES_DOCTYPE,
id: 3
}, {
doctype: consts.CONTACTS_DOCTYPE,
id: 5
}]);
});
it('should return an empty array if input is empty', function () {
var searchResults = [];
var filteredResults = searchEngine.limitSearchResults(searchResults);
expect(filteredResults).toEqual([]);
});
});