-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlastfm-info-test.js
118 lines (106 loc) · 3.75 KB
/
lastfm-info-test.js
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
require("./common.js");
var LastFmInfo = require("../lib/lastfm/lastfm-info");
var LastFmRequest = require("../lib/lastfm/lastfm-request");
var fakes = require("./fakes");
(function() {
describe("a new info instance")
var lastfm, gently;
before(function() {
lastfm = new LastFmNode();
gently = new Gently();
});
it("accepts listeners in handler options", function() {
var handlers = { error: function() {}, success: function() {} };
var options = { handlers: handlers };
gently.expect(handlers, "error");
gently.expect(handlers, "success");
var info = new LastFmInfo(lastfm, "", options);
info.emit("success");
});
it("emits error if type not specified", function() {
var handlers = { error: function() {}};
gently.expect(handlers, "error", function(error) {
assert.equal("Item type not specified", error.message);
});
var info = new LastFmInfo(lastfm, "", { handlers: handlers });
});
it("allows requests for user info", function() {
gently.expect(lastfm, "request", function() {
return new fakes.LastFmRequest();
});
var info = new LastFmInfo(lastfm, "user");
});
it("allows requests for track info", function() {
gently.expect(lastfm, "request", function() {
return new fakes.LastFmRequest();
});
var info = new LastFmInfo(lastfm, "track");
});
it("allows all [itemtype].getinfo calls", function() {
gently.expect(lastfm, "request", function(method, params) {
assert.equal("event.getinfo", method);
return new fakes.LastFmRequest();
});
new LastFmInfo(lastfm, "event");
});
it("passes through parameters", function() {
gently.expect(lastfm, "request", function(method, params) {
assert.equal("username", params.user);
assert.equal("anything", params.arbitrary);
return new fakes.LastFmRequest();
});
new LastFmInfo(lastfm, "user", { user: "username", arbitrary: "anything" });
});
it("doesn't pass through callback parameters", function() {
gently.expect(lastfm, "request", function(method, params) {
assert.ok(!params.error);
assert.ok(!params.success);
assert.ok(!params.handlers);
return new fakes.LastFmRequest();
});
new LastFmInfo(lastfm, "user", { handlers: { error: function() {}, success: function() {} } });
});
})();
(function() {
describe("when receiving data")
var gently, lastfm, request;
before(function() {
gently = new Gently();
lastfm = new LastFmNode();
request = new fakes.LastFmRequest();
});
it("emits error when receiving unexpected data", function() {
gently.expect(lastfm, "request", function() {
return request;
});
new LastFmInfo(lastfm, "track", { handlers: {
error: gently.expect(function errorHandler(error) {
assert.equal("Unexpected error", error.message);
})
}});
request.emit("success", JSON.parse(FakeData.SuccessfulAuthorisation));
});
it("emits success with received data when matches expected type", function() {
gently.expect(lastfm, "request", function() {
return request;
});
new LastFmInfo(lastfm, "track", { handlers: {
success: gently.expect(function success(track) {
assert.equal("Run To Your Grave", track.name);
assert.equal("232000", track.duration);
})
}});
request.emit("success", JSON.parse(FakeData.RunToYourGraveTrackInfo));
});
it("bubbles up errors", function() {
gently.expect(lastfm, "request", function() {
return request;
});
var info = new LastFmInfo(lastfm, "track");
gently.expect(info, "emit", function(event, error) {
assert.equal("error", event);
assert.equal("Bubbled error", error.message);
});
request.emit("error", new Error("Bubbled error"));
});
})();