TagSpaces is an offline, open source, document manager with tagging support
Adding unit tests for search (#1397)
mladensap3bdf19e10188
diff --git a/tests/unit/search.test.js b/tests/unit/search.test.jsindex 68c6ff909..cda282786 100644--- a/tests/unit/search.test.js+++ b/tests/unit/search.test.js@@ -1,4 +1,5 @@import Search from '../../app/services/search';+import { entry1, entry2, entry3 } from './testEntries';/*const mockedSearchIndex = { data: {} };const SearchMock = jest.fn();@@ -20,89 +21,117 @@ test('calls Search.searchLocationIndex with the params', () => {expect(SearchMock).toHaveBeenCalledWith(locationContent, searchQuery);});*/-/*const searchQuery: SearchQuery = {- textQuery: this.state.textQuery,- tagsAND: this.state.tagsAND,- tagsOR: this.state.tagsOR,- tagsNOT: this.state.tagsNOT,- // @ts-ignore- searchBoxing: this.state.searchBoxing,- fileTypes: this.state.fileTypes,- lastModified: this.state.lastModified,- fileSize: this.state.fileSize,- tagTimePeriodFrom: this.state.tagTimePeriodFrom- ? this.state.tagTimePeriodFrom.getTime()- : null,- tagTimePeriodTo: this.state.tagTimePeriodTo- ? this.state.tagTimePeriodTo.getTime()- : null,- tagPlaceLat: this.state.tagPlaceLat,- tagPlaceLong: this.state.tagPlaceLong,- tagPlaceRadius: this.state.tagPlaceRadius,- maxSearchResults: this.props.maxSearchResults,- currentDirectory: this.props.currentDirectory,- forceIndexing: this.state.forceIndexing-};*/-const tag1 = { title: 'tagTitle1' };const tag2 = { title: 'tagTitle2' };const tag3 = { title: 'tagTitle3' };-const entry1 = {- name: 'entryTitle1',- description: 'test description',- isFile: true,- extension: 'jpg',- tags: [tag1],- size: 111,- path: '/gg/'-};--const entry2 = {- name: 'entryTitle2',- isFile: true,- extension: 'jpg',- tags: [tag2],- size: 111,- path: '/gg/'-};+// test('calls Search.searchLocationIndex for tags', () => {+// const locationContent = [entry1, entry2]; //enhanceEntry(entry)];++// const searchQuery = {+// tagsAND: [tag1],+// maxSearchResults: 2+// };++// const searchQueryNotExist = {+// tagsAND: [tag3],+// maxSearchResults: 2+// };++// expect(+// Search.searchLocationIndex(locationContent, searchQuery)+// ).resolves.toStrictEqual([entry1]);++// expect(+// Search.searchLocationIndex(locationContent, searchQuery)+// ).resolves.not.toStrictEqual([entry2]);++// expect(+// Search.searchLocationIndex(locationContent, searchQueryNotExist)+// ).resolves.toStrictEqual([]);+// });test('calls Search.searchLocationIndex for tags', () => {- const locationContent = [entry1, entry2]; //enhanceEntry(entry)];+ const locationContent = [entry1, entry2];const searchQuery = {tagsAND: [tag1],maxSearchResults: 2};+ expect(+ Search.searchLocationIndex(locationContent, searchQuery)+ ).resolves.toStrictEqual([entry1]);++ // expect(+ // Search.searchLocationIndex(locationContent, searchQuery)+ // ).resolves.not.toStrictEqual([entry2]);+});++test('calls Search.searchLocationIndex for tags with not to equal', () => {+ const locationContent = [entry1, entry2];++ const searchQuery = {+ tagsAND: [tag1],+ maxSearchResults: 2+ };++ expect(+ Search.searchLocationIndex(locationContent, searchQuery)+ ).resolves.not.toStrictEqual([entry2]);+});++test('calls Search.searchLocationIndex for not exist tags', () => {+ const locationContent = [entry1, entry2];+const searchQueryNotExist = {tagsAND: [tag3],maxSearchResults: 2};+ expect(+ Search.searchLocationIndex(locationContent, searchQueryNotExist)+ ).resolves.toStrictEqual([]);+});++test('calls Search.searchLocationIndex for OR tags', () => {+ const locationContent = [entry1, entry2, entry3]; //enhanceEntry(entry)];++ const searchQuery = {+ tagsNOT: [tag1],+ maxSearchResults: 2+ };++ const searchQueryNotExist = {+ tagsNOT: [tag3],+ maxSearchResults: 2+ };+expect(Search.searchLocationIndex(locationContent, searchQuery)- ).resolves.toStrictEqual([entry1]);+ ).resolves.toStrictEqual([entry1, entry2]);expect(Search.searchLocationIndex(locationContent, searchQuery)- ).resolves.not.toStrictEqual([entry2]);+ ).resolves.not.toStrictEqual([entry3]);expect(Search.searchLocationIndex(locationContent, searchQueryNotExist)).resolves.toStrictEqual([]);});-test('calls Search.searchLocationIndex for textQuery', () => {+test('calls Search.searchLocationIndex for textQuery', async () => {const locationContent = [entry1, entry2];const searchQuery = {textQuery: 'description'};- expect(- Search.searchLocationIndex(locationContent, searchQuery)- ).resolves.toStrictEqual([entry1]);+ const searchResults = await Search.searchLocationIndex(+ locationContent,+ searchQuery+ );+ expect(searchResults[0].name).toStrictEqual('entryTitle1');});//jest.mock('../../app/pro', () => require('../../extensions/pro'));@@ -123,3 +152,46 @@ test('calls Search.searchLocationIndex for Pro', () => {).resolves.toStrictEqual([entry1]);});*/++test.skip('calls Search.searchLocationIndex for extension', async () => {+ const locationContent = [entry2, entry3];++ const searchQuery = {+ fileTypes: 'jpg'+ };++ const searchResults = await Search.searchLocationIndex(+ locationContent,+ searchQuery+ );+ expect(searchResults).resolves.toStrictEqual('jpg');+});++test.skip('calls Search.searchLocationIndex for fileSize', async () => {+ const locationContent = [entry1, entry2];++ const searchQuery = {+ fileSize: 'sizeTiny'+ };++ const searchResults = await Search.searchLocationIndex(+ locationContent,+ searchQuery+ );+ expect(searchResults[9].fileSize).resolves.toStrictEqual('sizeTiny');+});++test('calls Search.searchLocationIndex for folder', async () => {+ const locationContent = [entry1, entry2, entry3];++ const searchQuery = {+ textQuery: 'testfolder1'+ };++ const searchResults = await Search.searchLocationIndex(+ locationContent,+ searchQuery+ );++ expect(searchResults[0].name).toStrictEqual('testFolder1');+});diff --git a/tests/unit/testEntries.js b/tests/unit/testEntries.jsnew file mode 100644index 000000000..2f79ea9a0--- /dev/null+++ b/tests/unit/testEntries.js@@ -0,0 +1,28 @@+export const entry1 = {+ name: 'entryTitle1',+ description: 'test description',+ isFile: true,+ extension: 'jpg',+ tags: [{ title: 'tagTitle1' }],+ textConten: 'adsadas dasda',+ fromTime: '',+ toTime: '',+ fileSize: 9+};++export const entry2 = {+ name: 'entryTitle2',+ isFile: true,+ extension: 'jpg',+ tags: [{ title: 'tagTitle2' }],+ size: 222,+ path: '/gg/'+};++export const entry3 = {+ name: 'testFolder1',+ description: 'test folder desciption',+ isDirectory: true,+ path: '/gg/',+ tags: []+};