UNPKG

4.77 kBJavaScriptView Raw
1require("./common.js");
2var LastFmInfo = require("lastfm/lastfm-info");
3var LastFmRequest = require("lastfm/lastfm-request");
4var fakes = require("./fakes");
5
6(function() {
7describe("a new info instance")
8 var lastfm, gently;
9 before(function() {
10 lastfm = new LastFmNode();
11 gently = new Gently();
12 });
13
14 it("accepts listeners in options", function() {
15 var handlers = { error: function() {}, success: function() {} };
16 gently.expect(handlers, "error");
17 gently.expect(handlers, "success");
18 var info = new LastFmInfo(lastfm, "", handlers);
19 info.emit("success");
20 });
21
22 it("emits error if type not specified", function() {
23 var handler = { error: function() {}};
24 gently.expect(handler, "error", function(error) {
25 assert.equal("Item type not specified", error.message);
26 });
27 var info = new LastFmInfo(lastfm, "", { error: handler.error });
28 });
29
30 it("allows requests for user info", function() {
31 gently.expect(lastfm, "read", function() {
32 return new fakes.LastFmRequest();
33 });
34 var info = new LastFmInfo(lastfm, "user");
35 });
36
37 it("allows requests for track info", function() {
38 gently.expect(lastfm, "read", function() {
39 return new fakes.LastFmRequest();
40 });
41 var info = new LastFmInfo(lastfm, "track");
42 });
43
44 it("allows all [itemtype].getinfo calls", function() {
45 gently.expect(lastfm, "read", function(params) {
46 assert.equal("event.getinfo", params.method);
47 return new fakes.LastFmRequest();
48 });
49 new LastFmInfo(lastfm, "event");
50 });
51
52 it("calls unsigned methods", function() {
53 gently.expect(lastfm, "read", function(params, signed) {
54 assert.equal(false, signed);
55 return new fakes.LastFmRequest();
56 });
57 var info = new LastFmInfo(lastfm, "user");
58 });
59
60 it("passes through parameters", function() {
61 gently.expect(lastfm, "read", function(params) {
62 assert.equal("username", params.user);
63 assert.equal("anything", params.arbitrary);
64 return new fakes.LastFmRequest();
65 });
66 new LastFmInfo(lastfm, "user", { user: "username", arbitrary: "anything" });
67 });
68
69 it("doesnt pass through callback parameters", function() {
70 gently.expect(lastfm, "read", function(params) {
71 assert.ok(!params.error);
72 assert.ok(!params.success);
73 return new fakes.LastFmRequest();
74 });
75 new LastFmInfo(lastfm, "user", { error: function() {}, success: function() {} });
76 });
77})();
78
79(function() {
80describe("when receiving data")
81 var gently, lastfm, request;
82 before(function() {
83 gently = new Gently();
84 lastfm = new LastFmNode();
85 request = new fakes.LastFmRequest();
86 });
87
88 it("emits error if response contains error", function() {
89 gently.expect(lastfm, "read", function(params, signed) {
90 return request;
91 });
92 new LastFmInfo(lastfm, "track", {
93 error: gently.expect(function errorHandler(error) {
94 assert.equal("You must supply either a track & artist name or a track mbid.", error.message);
95 })
96 });
97 request.emit("success", FakeData.NotEnoughTrackInfo);
98 });
99
100 it("emits error when receiving unexpected data", function() {
101 gently.expect(lastfm, "read", function(params, signed) {
102 return request;
103 });
104 new LastFmInfo(lastfm, "track", {
105 error: gently.expect(function errorHandler(error) {
106 assert.equal("Unexpected error", error.message);
107 })
108 });
109 request.emit("success", FakeData.SuccessfulAuthorisation);
110 });
111
112 it("emits error if receiving junk", function() {
113 gently.expect(lastfm, "read", function(params, signed) {
114 return request;
115 });
116 new LastFmInfo(lastfm, "track", {
117 error: gently.expect(function errorHandler(error) {
118 assert.ok(error.message.indexOf(FakeData.Garbage) > -1);
119 assert.ok("Syntax error");
120 })
121 });
122 request.emit("success", FakeData.Garbage);
123 });
124
125 it("emits success with received data when matches expected type", function() {
126 gently.expect(lastfm, "read", function(params, signed) {
127 return request;
128 });
129 new LastFmInfo(lastfm, "track", {
130 success: gently.expect(function success(track) {
131 assert.equal("Run To Your Grave", track.name);
132 assert.equal("232000", track.duration);
133 })
134 });
135 request.emit("success", FakeData.RunToYourGraveTrackInfo);
136 });
137
138 it("bubbles up errors", function() {
139 gently.expect(lastfm, "read", function(params, signed) {
140 return request;
141 });
142 var info = new LastFmInfo(lastfm, "track");
143 gently.expect(info, "emit", function(event, error) {
144 assert.equal("error", event);
145 assert.equal("Bubbled error", error.message);
146 });
147 request.emit("error", new Error("Bubbled error"));
148 });
149})();