import "isomorphic-fetch"; import { addBookmarkToList, updateBookmark } from "../actions/bookmark"; import * as fetchMock from "fetch-mock"; describe("bookmark actions", () => { beforeEach(() => { const lp = { hosts: { profile: "https://foo.com", }, }; (global).window.lp = lp; }); afterEach(() => { fetchMock.reset(); }); it("should add an item to a list", () => { fetchMock.postOnce("https://foo.com/profile/api/1234/lists/5678/entries.json", { id: "5678", checked: true, target: { type: "poi", id: "abc1234", }, note: "", }); const thunk = addBookmarkToList({ userId: "1234", listId: "5678", data: { checked: true, note: "oh hai", target: { type: "poi", id: "abc1234", }, } }); const dispatch = jest.fn(); thunk(dispatch, () => ({}), {}) .then(() => { expect(dispatch.mock.calls[1][0].type).toBe("ADD_BOOKMARK_DONE"); }); expect(dispatch.mock.calls[0][0].type).toBe("ADD_BOOKMARK_START"); }); it("should update a bookmark", () => { fetchMock.patchOnce("https://foo.com/profile/api/1234/lists/5678/entries/999.json", { id: "5678", checked: false, target: { type: "poi", id: "abc1234", }, note: "", }); const thunk = updateBookmark({ userId: "1234", listId: "5678", entryId: "999", data: { checked: false, note: "oh hai!", target: { type: "poi", id: "abc1234", }, } }); const dispatch = jest.fn(); thunk(dispatch, () => ({}), {}) .then(() => { expect(dispatch.mock.calls[1][0].type).toBe("UPDATE_BOOKMARK_DONE"); }); expect(dispatch.mock.calls[0][0].type).toBe("UPDATE_BOOKMARK_START"); }); it("should handle errors updating a bookmark", () => { fetchMock.patchOnce("https://foo.com/profile/api/1234/lists/5678/entries/9999.json", { status: 500, body: { message: "an error" } }); const thunk = updateBookmark({ userId: "1234", listId: "5678", entryId: "9999", data: { checked: false, note: "oh hai!", target: { type: "poi", id: "abc1234", }, } }); const dispatch = jest.fn(); thunk(dispatch, () => ({}), {}) .then(() => { const showToast = dispatch.mock.calls[1][0]; showToast(dispatch); expect(dispatch.mock.calls[2][0].type).toBe("TOAST_SHOW"); expect(dispatch.mock.calls[2][0].payload.message).toBe("an error"); }); expect(dispatch.mock.calls[0][0].type).toBe("UPDATE_BOOKMARK_START"); }); it("should dispatch error messages", () => { fetchMock.post("https://foo.com/profile/api/12345/lists/5678/entries.json", { status: 500, body: { message: "an error" } }); const thunk = addBookmarkToList({ userId: "12345", listId: "5678", data: { checked: true, note: "oh hai", target: { type: "poi", id: "abc1234", }, } }); const dispatch = jest.fn(); thunk(dispatch, () => ({}), {}) .then(() => { const showToast = dispatch.mock.calls[1][0]; showToast(dispatch); expect(dispatch.mock.calls[2][0].type).toBe("TOAST_SHOW"); expect(dispatch.mock.calls[2][0].payload.message).toBe("an error"); }); expect(dispatch.mock.calls[0][0].type).toBe("ADD_BOOKMARK_START"); }); });