import Bookmark, { IBookmark } from "../resources/bookmark"; import BookmarkService from "../resources/bookmark/bookmarkService"; describe("BookmarkService", () => { it("should upsert multiple entries to a bookmark list", () => { const fetchMock = jest.fn(() => new Promise((resolve => resolve({ data: [{ "id": "123", "type": "bookmark-entry", "attributes": { "order_number": 2, "note": "test", "created_at": "2018-02-19T14:56:26.398114", "checked": true }, "relationships": { "target": { "links": { "related": "/pois/1537908" }, "data": { "type": "poi", "id": "1537908" } } }, }, { "id": "234", "type": "bookmark-entry", "attributes": { "order_number": 3, "note": "test bew", "created_at": "2018-03-19T14:56:26.398114", "checked": true }, "relationships": { "target": { "links": { "related": "/pois/1537909" }, "data": { "type": "poi", "id": "1537909" } } }, }] })))); const bookmarks: IBookmark[] = [ new Bookmark({ id: "123", order: 2, checked: true, note: "test", target: { id: "1537908", type: "poi" } }), new Bookmark({ order: 3, checked: true, note: "test new", target: { id: "1537909", type: "poi" } }), ]; const list: any = { id: "12345" }; const service = new BookmarkService("access123"); service.http = { fetch: fetchMock, }; service.updateOrInsertBookmarksToList("1", bookmarks, list) .then((bookmarks) => { expect(bookmarks).toHaveLength(2); expect(bookmarks[0].id).toBe("123"); expect(bookmarks[1].id).toBe("234"); }) .catch((e) => console.error(e)); expect(fetchMock).toHaveBeenCalledWith("/users/1/bookmark-lists/12345/entries/upsert", { "method": "POST", "headers": {"Authorization": "Bearer access123"}, "body": { data: [{ id: "123", type: "bookmark-entry", attributes: { order_number: 2, checked: true, note: "test", }, relationships: { target: { data: { id: "1537908", type: "poi" } } } }, { type: "bookmark-entry", attributes: { order_number: 3, checked: true, note: "test new", }, relationships: { target: { data: { id: "1537909", type: "poi" } } } }] }, }); }); });