UNPKG

3.65 kBPlain TextView Raw
1import "isomorphic-fetch";
2import { addBookmarkToList, updateBookmark } from "../actions/bookmark";
3import * as fetchMock from "fetch-mock";
4
5describe("bookmark actions", () => {
6 beforeEach(() => {
7 const lp = {
8 hosts: {
9 profile: "https://foo.com",
10 },
11 };
12
13 (<any>global).window.lp = lp;
14 });
15
16 afterEach(() => {
17 fetchMock.reset();
18 });
19
20 it("should add an item to a list", () => {
21 fetchMock.postOnce("https://foo.com/profile/api/1234/lists/5678/entries.json", {
22 id: "5678",
23 checked: true,
24 target: {
25 type: "poi",
26 id: "abc1234",
27 },
28 note: "",
29 });
30
31 const thunk = addBookmarkToList({
32 userId: "1234",
33 listId: "5678",
34 data: {
35 checked: true,
36 note: "oh hai",
37 target: {
38 type: "poi",
39 id: "abc1234",
40 },
41 }
42 });
43
44 const dispatch = jest.fn();
45 thunk(dispatch, () => ({}), {})
46 .then(() => {
47 expect(dispatch.mock.calls[1][0].type).toBe("ADD_BOOKMARK_DONE");
48 });
49
50 expect(dispatch.mock.calls[0][0].type).toBe("ADD_BOOKMARK_START");
51 });
52
53 it("should update a bookmark", () => {
54 fetchMock.patchOnce("https://foo.com/profile/api/1234/lists/5678/entries/999.json", {
55 id: "5678",
56 checked: false,
57 target: {
58 type: "poi",
59 id: "abc1234",
60 },
61 note: "",
62 });
63
64 const thunk = updateBookmark({
65 userId: "1234",
66 listId: "5678",
67 entryId: "999",
68 data: {
69 checked: false,
70 note: "oh hai!",
71 target: {
72 type: "poi",
73 id: "abc1234",
74 },
75 }
76 });
77
78 const dispatch = jest.fn();
79 thunk(dispatch, () => ({}), {})
80 .then(() => {
81 expect(dispatch.mock.calls[1][0].type).toBe("UPDATE_BOOKMARK_DONE");
82 });
83
84 expect(dispatch.mock.calls[0][0].type).toBe("UPDATE_BOOKMARK_START");
85 });
86
87 it("should handle errors updating a bookmark", () => {
88 fetchMock.patchOnce("https://foo.com/profile/api/1234/lists/5678/entries/9999.json", {
89 status: 500,
90 body: {
91 message: "an error"
92 }
93 });
94
95 const thunk = updateBookmark({
96 userId: "1234",
97 listId: "5678",
98 entryId: "9999",
99 data: {
100 checked: false,
101 note: "oh hai!",
102 target: {
103 type: "poi",
104 id: "abc1234",
105 },
106 }
107 });
108
109 const dispatch = jest.fn();
110 thunk(dispatch, () => ({}), {})
111 .then(() => {
112 const showToast = dispatch.mock.calls[1][0];
113 showToast(dispatch);
114 expect(dispatch.mock.calls[2][0].type).toBe("TOAST_SHOW");
115 expect(dispatch.mock.calls[2][0].payload.message).toBe("an error");
116 });
117
118 expect(dispatch.mock.calls[0][0].type).toBe("UPDATE_BOOKMARK_START");
119 });
120
121 it("should dispatch error messages", () => {
122 fetchMock.post("https://foo.com/profile/api/12345/lists/5678/entries.json", {
123 status: 500,
124 body: {
125 message: "an error"
126 }
127 });
128
129 const thunk = addBookmarkToList({
130 userId: "12345",
131 listId: "5678",
132 data: {
133 checked: true,
134 note: "oh hai",
135 target: {
136 type: "poi",
137 id: "abc1234",
138 },
139 }
140 });
141
142 const dispatch = jest.fn();
143 thunk(dispatch, () => ({}), {})
144 .then(() => {
145 const showToast = dispatch.mock.calls[1][0];
146 showToast(dispatch);
147 expect(dispatch.mock.calls[2][0].type).toBe("TOAST_SHOW");
148 expect(dispatch.mock.calls[2][0].payload.message).toBe("an error");
149 });
150
151 expect(dispatch.mock.calls[0][0].type).toBe("ADD_BOOKMARK_START");
152 });
153});