forked from metabrainz/listenbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LastFMImporter.test.tsx
302 lines (259 loc) · 8.71 KB
/
LastFMImporter.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import * as React from "react";
import { mount, shallow } from "enzyme";
import LastFmImporter from "./LastFMImporter";
// Mock data to test functions
import * as page from "./__mocks__/page.json";
import * as getInfo from "./__mocks__/getInfo.json";
import * as getInfoNoPlayCount from "./__mocks__/getInfoNoPlayCount.json";
// Output for the mock data
import * as encodeScrobbleOutput from "./__mocks__/encodeScrobbleOutput.json";
jest.useFakeTimers();
const props = {
user: {
id: "id",
name: "dummyUser",
auth_token: "foobar",
},
profileUrl: "http://profile",
apiUrl: "apiUrl",
lastfmApiUrl: "http://ws.audioscrobbler.com/2.0/",
lastfmApiKey: "foobar",
};
describe("encodeScrobbles", () => {
it("encodes the given scrobbles correctly", () => {
expect(LastFmImporter.encodeScrobbles(page)).toEqual(encodeScrobbleOutput);
});
});
let instance: LastFmImporter;
describe("getNumberOfPages", () => {
beforeEach(() => {
const wrapper = shallow<LastFmImporter>(<LastFmImporter {...props} />);
instance = wrapper.instance();
instance.setState({ lastfmUsername: "dummyUser" });
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(page),
});
});
});
it("should call with the correct url", () => {
instance.getNumberOfPages();
expect(window.fetch).toHaveBeenCalledWith(
`${props.lastfmApiUrl}?method=user.getrecenttracks&user=${instance.state.lastfmUsername}&api_key=${props.lastfmApiKey}&from=1&format=json`
);
});
it("should return number of pages", async () => {
const num = await instance.getNumberOfPages();
expect(num).toBe(1);
});
it("should return -1 if there is an error", async () => {
// Mock function for failed fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: false,
});
});
const num = await instance.getNumberOfPages();
expect(num).toBe(-1);
});
});
describe("getTotalNumberOfScrobbles", () => {
beforeEach(() => {
const wrapper = shallow<LastFmImporter>(<LastFmImporter {...props} />);
instance = wrapper.instance();
instance.setState({ lastfmUsername: "dummyUser" });
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(getInfo),
});
});
});
it("should call with the correct url", () => {
instance.getTotalNumberOfScrobbles();
expect(window.fetch).toHaveBeenCalledWith(
`${props.lastfmApiUrl}?method=user.getinfo&user=${instance.state.lastfmUsername}&api_key=${props.lastfmApiKey}&format=json`
);
});
it("should return number of pages", async () => {
const num = await instance.getTotalNumberOfScrobbles();
expect(num).toBe(1026);
});
it("should return -1 if playcount is not available", async () => {
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(getInfoNoPlayCount),
});
});
const num = await instance.getTotalNumberOfScrobbles();
expect(num).toBe(-1);
});
it("should throw an error when fetch fails", async () => {
// Mock function for failed fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: false,
});
});
await expect(instance.getTotalNumberOfScrobbles()).rejects.toThrowError();
});
});
describe("getPage", () => {
beforeEach(() => {
const wrapper = shallow<LastFmImporter>(<LastFmImporter {...props} />);
instance = wrapper.instance();
instance.setState({ lastfmUsername: "dummyUser" });
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.resolve(page),
});
});
});
it("should call with the correct url", () => {
instance.getPage(1);
expect(window.fetch).toHaveBeenCalledWith(
`${props.lastfmApiUrl}?method=user.getrecenttracks&user=${instance.state.lastfmUsername}&api_key=${props.lastfmApiKey}&from=1&page=1&format=json`
);
});
it("should call encodeScrobbles", async () => {
// Mock function for encodeScrobbles
LastFmImporter.encodeScrobbles = jest.fn(() => ["foo", "bar"]);
const data = await instance.getPage(1);
expect(LastFmImporter.encodeScrobbles).toHaveBeenCalledTimes(1);
expect(data).toEqual(["foo", "bar"]);
});
it("should retry if 50x error is recieved", async () => {
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: false,
status: 503,
});
});
await instance.getPage(1);
// There is no direct way to check if retry has been called
expect(setTimeout).toHaveBeenCalledTimes(1);
jest.runAllTimers();
});
it("should skip the page if 40x is recieved", async () => {
// Mock function for failed fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: false,
status: 404,
});
});
await instance.getPage(1);
expect(setTimeout).not.toHaveBeenCalled();
});
it("should retry if there is any other error", async () => {
// Mock function for fetch
window.fetch = jest.fn().mockImplementation(() => {
return Promise.resolve({
ok: true,
json: () => Promise.reject(),
});
});
await instance.getPage(1);
// There is no direct way to check if retry has been called
expect(setTimeout).toHaveBeenCalledTimes(1);
jest.runAllTimers();
});
});
describe("submitPage", () => {
beforeEach(() => {
const wrapper = shallow<LastFmImporter>(<LastFmImporter {...props} />);
instance = wrapper.instance();
instance.setState({ lastfmUsername: "dummyUser" });
instance.getRateLimitDelay = jest.fn().mockImplementation(() => 0);
instance.updateRateLimitParameters = jest.fn();
});
it("calls submitListens once", async () => {
// window.fetch = jest.fn().mockImplementation(() => {
// return Promise.resolve({
// ok: true,
// json: () => Promise.resolve({ status: 200 }),
// });
// });
instance.APIService.submitListens = jest.fn().mockImplementation(() => {
return Promise.resolve({ status: 200 });
});
instance.submitPage([
{
listened_at: 1000,
track_metadata: {
artist_name: "foobar",
track_name: "bazfoo",
},
},
]);
jest.runAllTimers();
// Flush all promises
// https://stackoverflow.com/questions/51126786/jest-fake-timers-with-promises
await new Promise((resolve) => setImmediate(resolve));
expect(instance.APIService.submitListens).toHaveBeenCalledTimes(1);
});
it("calls updateRateLimitParameters once", async () => {
instance.APIService.submitListens = jest.fn().mockImplementation(() => {
return Promise.resolve({ status: 200 });
});
instance.submitPage([
{
listened_at: 1000,
track_metadata: {
artist_name: "foobar",
track_name: "bazfoo",
},
},
]);
jest.runAllTimers();
// Flush all promises
// https://stackoverflow.com/questions/51126786/jest-fake-timers-with-promises
await new Promise((resolve) => setImmediate(resolve));
expect(instance.updateRateLimitParameters).toHaveBeenCalledTimes(1);
expect(instance.updateRateLimitParameters).toHaveBeenCalledWith({
status: 200,
});
});
it("calls getRateLimitDelay once", async () => {
instance.submitPage([
{
listened_at: 1000,
track_metadata: {
artist_name: "foobar",
track_name: "bazfoo",
},
},
]);
expect(instance.getRateLimitDelay).toHaveBeenCalledTimes(1);
});
});
describe("LastFmImporter Page", () => {
it("renders", () => {
const wrapper = mount(<LastFmImporter {...props} />);
expect(wrapper.html()).toMatchSnapshot();
});
it("modal renders when button clicked", () => {
const wrapper = shallow(<LastFmImporter {...props} />);
// Simulate submiting the form
wrapper.find("form").simulate("submit", {
preventDefault: () => null,
});
// Test if the show property has been set to true
expect(wrapper.exists("LastFMImporterModal")).toBe(true);
});
it("submit button is disabled when input is empty", () => {
const wrapper = shallow(<LastFmImporter {...props} />);
// Make sure that the input is empty
wrapper.setState({ lastfmUsername: "" });
// Test if button is disabled
expect(wrapper.find('input[type="submit"]').props().disabled).toBe(true);
});
});