UNPKG

7.41 kBJavaScriptView Raw
1require('./common.js');
2var LastFmSession = require('lastfm/lastfm-session');
3var LastFmUpdate = require('lastfm/lastfm-update');
4var fakes = require("./fakes");
5
6(function() {
7 describe("new LastFmUpdate")
8 it("can have success and error handlers specified at creation (deprecated)", function() {
9 var gently = new Gently();
10 var lastfm = new LastFmNode();
11 var update = new LastFmUpdate(lastfm, "method", new LastFmSession(lastfm, "user", "key"), {
12 error: gently.expect(function error() {}),
13 success: gently.expect(function success() {})
14 });
15 update.emit("error");
16 update.emit("success");
17 });
18
19 it("can have success and error handlers specified in option at creation", function() {
20 var gently = new Gently();
21 var lastfm = new LastFmNode();
22 var update = new LastFmUpdate(lastfm, "method", new LastFmSession(lastfm, "user", "key"), { handlers: {
23 error: gently.expect(function error() {}),
24 success: gently.expect(function success() {})
25 }});
26 update.emit("error");
27 update.emit("success");
28 });
29})();
30
31(function() {
32 var request, returndata, options, session, method, gently, lastfm, authorisedSession, requestError;
33
34 function setupFixture() {
35 request = new fakes.LastFmRequest();
36 returndata;
37 options = {};
38 session = null;
39 method = "";
40 gently = new Gently();
41 lastfm = new LastFmNode();
42 authorisedSession = new LastFmSession(lastfm, "user", "key");
43 requestError = null;
44 }
45
46 function whenWriteRequestReturns(data) {
47 returndata = data;
48 gently.expect(lastfm, "request", function(method, params) {
49 return request;
50 });
51 }
52
53 function whenWriteRequestThrowsError(errorMessage) {
54 requestError = errorMessage;
55 gently.expect(lastfm, "request", function(method, params) {
56 return request;
57 });
58 }
59
60 function andOptionsAre(setOptions) {
61 options = setOptions;
62 }
63
64 function andMethodIs(setMethod) {
65 method = setMethod;
66 }
67
68 function andSessionIs(setSession) {
69 session = setSession;
70 }
71
72 function expectSuccess(assertions) {
73 options.handlers = options.handlers || {};
74 options.handlers.success = function(track) {
75 if (assertions) {
76 assertions(track);
77 }
78 };
79 new LastFmUpdate(lastfm, method, session, options);
80 request.emit("success", returndata);
81 }
82
83 function expectError(expectedError) {
84 options.handlers = options.handlers || {};
85 options.handlers.error = gently.expect(function(error) {
86 assert.equal(expectedError, error.message);
87 });
88 new LastFmUpdate(lastfm, method, session, options);
89 if (requestError) {
90 request.emit("error", new Error(requestError));
91 }
92 else {
93 request.emit("success", returndata);
94 }
95 }
96
97 describe("update requests")
98 before(function() {
99 setupFixture();
100 });
101
102 it("fail when the session is not authorised", function() {
103 var session = new LastFmSession();
104 assert.throws(function() {
105 new LastFmUpdate(lastfm, "method", session);
106 });
107 });
108
109 it("emits error when problem updating", function() {
110 whenWriteRequestReturns(FakeData.UpdateError);
111 andMethodIs("nowplaying");
112 andSessionIs(authorisedSession);
113 andOptionsAre({
114 track: FakeTracks.RunToYourGrave
115 });
116 expectError("Invalid method signature supplied");
117 });
118
119 describe("nowPlaying updates")
120 before(function() {
121 setupFixture();
122 });
123
124 it("uses updateNowPlaying method", function() {
125 gently.expect(lastfm, "request", function(method, params) {
126 assert.equal("track.updateNowPlaying", method);
127 return request;
128 });
129 new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
130 track: FakeTracks.RunToYourGrave
131 });
132 });
133
134 it("sends required parameters", function() {
135 gently.expect(lastfm, "request", function(method, params) {
136 assert.equal("The Mae Shi", params.artist);
137 assert.equal("Run To Your Grave", params.track);
138 assert.equal("key", params.sk);
139 return request;
140 });
141 new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
142 track: FakeTracks.RunToYourGrave
143 });
144 });
145
146 it("emits success when updated", function() {
147 whenWriteRequestReturns(FakeData.UpdateNowPlayingSuccess);
148 andMethodIs("nowplaying");
149 andSessionIs(authorisedSession);
150 andOptionsAre({
151 track: FakeTracks.RunToYourGrave
152 });
153 expectSuccess(function(track) {
154 assert.equal("Run To Your Grave", track.name);
155 });
156 });
157
158 it("sends duration when supplied", function() {
159 gently.expect(lastfm, "request", function(method, params) {
160 assert.equal(232000, params.duration);
161 return request;
162 });
163 new LastFmUpdate(lastfm, "nowplaying", authorisedSession, {
164 track: FakeTracks.RunToYourGrave,
165 duration: 232000
166 });
167 });
168
169 it("bubbles up errors", function() {
170 var errorMessage = "Bubbled error";
171 whenWriteRequestThrowsError(errorMessage);
172 andMethodIs("nowplaying");
173 andSessionIs(authorisedSession);
174 andOptionsAre({
175 track: FakeTracks.RunToYourGrave,
176 timestamp: 12345678
177 });
178 expectError(errorMessage);
179 });
180
181 describe("a scrobble request")
182 before(function() {
183 setupFixture();
184 });
185
186 it("emits error when no timestamp supplied", function() {
187 new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
188 track: FakeTracks.RunToYourGrave,
189 handlers: {
190 error: gently.expect(function error(error) {
191 assert.equal("Timestamp is required for scrobbling", error.message);
192 })
193 }
194 });
195 });
196
197 it("uses scrobble method", function() {
198 gently.expect(lastfm, "request", function(method, params) {
199 assert.equal("track.scrobble", method);
200 return request;
201 });
202 new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
203 track: FakeTracks.RunToYourGrave,
204 timestamp: 12345678
205 });
206 });
207
208 it("sends required parameters", function() {
209 gently.expect(lastfm, "request", function(method, params) {
210 assert.equal("The Mae Shi", params.artist);
211 assert.equal("Run To Your Grave", params.track);
212 assert.equal("key", params.sk);
213 assert.equal(12345678, params.timestamp);
214 return request;
215 });
216
217 new LastFmUpdate(lastfm, "scrobble", authorisedSession, {
218 track: FakeTracks.RunToYourGrave,
219 timestamp: 12345678
220 });
221 });
222
223 it("emits success when updated", function() {
224 whenWriteRequestReturns(FakeData.ScrobbleSuccess);
225 andMethodIs("scrobble");
226 andSessionIs(authorisedSession);
227 andOptionsAre({
228 track: FakeTracks.RunToYourGrave,
229 timestamp: 12345678
230 });
231 expectSuccess(function(track) {
232 assert.equal("Run To Your Grave", track.name);
233 });
234 });
235
236 it("bubbles up errors", function() {
237 var errorMessage = "Bubbled error";
238 whenWriteRequestThrowsError(errorMessage);
239 andMethodIs("scrobble");
240 andSessionIs(authorisedSession);
241 andOptionsAre({
242 track: FakeTracks.RunToYourGrave,
243 timestamp: 12345678
244 });
245 expectError(errorMessage);
246 });
247})();