import SearchService from "../resources/search/service"; import SearchMapper from "../resources/search/mapper"; describe("SearchService", () => { it("should fetch search with the provided query", (done) => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: [{ attributes: { title: "Poland", }, id: 1, type: "search-result", }], meta: { total: 1, query: "Poland", } })))); const service = new SearchService("access123"); service.mapper = new SearchMapper(); service.http = { fetch: fetchMock, }; return service.query({ query: "Poland" }) .then((payload) => { expect(payload.data.length).toBe(1); expect(payload.total).toBe(1); expect(payload.meta.query).toBe("Poland"); expect(payload.meta.total).toBe(1); done(); }) .then(() => { expect(fetchMock).toHaveBeenCalledWith( `/search?filter[search-result][query][equals]=Poland`, { "headers": { "Authorization": "Bearer access123" }, "query": "Poland" } ); }) .catch((e) => console.error(e)); }); it("should fetch search with the provided query, id, and type", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: [{ attributes: { title: "Poland", }, id: 1, type: "search-result", }], meta: { total: 1, query: "Poland" } })))); const service = new SearchService("access123"); service.mapper = new SearchMapper(); service.http = { fetch: fetchMock, }; return service.query({ query: "Poland", "op-id": "360265", "op-type": "place", }) .then(() => { expect(fetchMock).toHaveBeenCalledWith( `/search?filter[search-result][query][equals]=Poland&filter[search-result][op-id][equals]=360265&filter[search-result][op-type][equals]=place`, { "headers": { "Authorization": "Bearer access123" }, "query": "Poland", "op-id": "360265", "op-type": "place" } ); }) .catch((e) => console.error(e)); }); it("should post clickthrough with given term & id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { message: "Clickthrough registered." } })))); const service = new SearchService("access123"); service.http = { fetch: fetchMock, }; return service.createClickthrough("Gatsby", "c41e0fde-5e64-11e8-b625-2fe248649608").then(() => { expect(fetchMock).toHaveBeenCalledWith("/search/clickthroughs", { "body": { "data": { "type": "search-clickthrough", "attributes": { "term": "Gatsby", "id": "c41e0fde-5e64-11e8-b625-2fe248649608" } } }, "headers": { "Authorization": "Bearer access123" }, "method": "POST" } ); }); }); it("should post autocomplete clickthrough with given prefix & swiftype id", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: { message: "Clickthrough registered." } })))); const service = new SearchService("access123"); service.http = { fetch: fetchMock, }; return service.createAutocompleteClickthrough("Gats", "5b3a84760cc37f95de30e254").then(() => { expect(fetchMock).toHaveBeenCalledWith("/search/autocomplete/clickthroughs", { "body": { "data": { "type": "autocomplete-clickthrough", "attributes": { "prefix": "Gats", "id": "5b3a84760cc37f95de30e254" } } }, "headers": { "Authorization": "Bearer access123" }, "method": "POST" } ); }); }); it("should fetch autocomplete with the provided query", (done) => { const fetchMock = jest.fn().mockImplementation(() => Promise.resolve({ data: [{ attributes: { title: "Poland", }, id: 1, type: "search-result", }], meta: { total: 1, query: "Poland", } })); const service = new SearchService("access123"); service.mapper = new SearchMapper(); service.http = { fetch: fetchMock, }; return service.queryAutocomplete({ query: "Poland", limit: 20 }) .then((payload) => { expect(payload.data.length).toBe(1); expect(payload.meta.query).toBe("Poland"); expect(payload.meta.total).toBe(1); done(); }) .then(() => { expect(fetchMock).toHaveBeenCalledWith( `/search/autocomplete?filter[search-result][query][equals]=Poland&page[limit]=20`, { "headers": { "Authorization": "Bearer access123" }, "query": "Poland", "limit": 20 } ); }) .catch((e) => console.error(e)); }); });