Danh mục

Test Driven JavaScript Development- P20

Số trang: 20      Loại file: pdf      Dung lượng: 197.48 KB      Lượt xem: 13      Lượt tải: 0    
Hoai.2512

Hỗ trợ phí lưu trữ khi tải xuống: 9,000 VND Tải xuống file đầy đủ (20 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Test Driven JavaScript Development- P20:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P20 14.5 Event Emitters 373 Listing 14.54 Expecting chatRoom to be event emitter testCase(exports, chatRoom, { should be event emitter: function (test) { test.isFunction(chatRoom.addListener); test.isFunction(chatRoom.emit); test.done(); } }); We can pass this test by popping EventEmitter.prototype in as chat- Room’s prototype, as seen in Listing 14.55. Listing 14.55 chatRoom inheriting from EventEmitter.prototype /* ... */ var EventEmitter = require(events).EventEmitter; /* ... */ var chatRoom = Object.create(EventEmitter.prototype); chatRoom.addMessage = function (user, message) {/* ... */}; chatRoom.getMessagesSince = function (id) {/* ... */}; Note that because V8 fully supports ECMAScript 5’s Object.create, we could have used property descriptors to add the methods as well, as seen in Listing 14.56. Listing 14.56 chatRoom defined with property descriptors var chatRoom = Object.create(EventEmitter.prototype, { addMessage: { value: function (user, message) { /* ... */ } }, getMessagesSince: { value: function (id) { /* ... */ } } });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 374 Server-Side JavaScript with Node.js At this point the property descriptors don’t provide anything we have a doc- umented need for (i.e., the ability to override default property attribute values), so we’ll avoid the added indentation and stick with the simple assignments in Listing 14.55. Next up, we make sure that addMessage emits an event. Listing 14.57 shows the test. Listing 14.57 Expecting addMessage to emit a “message” event testCase(exports, chatRoom.addMessage, { /* ... */ should emit message event: function (test) { var message; this.room.addListener(message, function (m) { message = m; }); this.room.addMessage(cjno, msg).then(function (m) { test.same(m, message); test.done(); }); } }); To pass this test we need to place a call to emit right before we resolve the promise, as seen in Listing 14.58. Listing 14.58 Emitting a message event chatRoom.addMessage= function (user, message, callback) { var promise = new Promise() process.nextTick(function () { /* ... */ if (!err) { /* ... */ this.emit(message, data); promise.resolve(data); } else { promise.reject(err, true); }Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 14.5 Event Emitters 375 }.bind(this)); return promise; }; With the event in place, we can build the waitForMessagesSince method. 14.5.2 Waiting for Messages The waitForMessagesSince method will do one of two things; if messages are available since the provided id, the returned promise will resolve immediately. If no messages are currently available, the method will add a listener for the “message” event, and the returned promise will resolve once a new message is added. The test in Listing 14.59 expects that the promise is immediately resolved if messages are available. Listing 14.59 Expecting available messages to resolve immediately /* ... */ var Promise = require(node-promise/promise).Promise; var stub = require(stub); /* ... */ testCase(exports, chatRoom.waitForMessagesSince, { setUp: chatRoomSetup, should yield existing messages: function (test) { var promise = new Promise(); promise.resolve([{ id: 43 }]); this.room.getMessagesSince = stub(promise); this.room.waitForMessagesSince(42).then(function (m) { test.same([{ id: 43 }], m); test.done(); }); } }); This test stubs the getMessagesSince method to verify that its results are used if there are any. To pass this test we can simply return the promise returned from getMessagesSince, as seen in Listing 14.60.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 376 Server-Side JavaScript with Node.js Listing 14.60 Proxying getMessagesSince chatRoom.waitForMessagesSince = function (id) { return this.getMessagesSince(id); }; Now to the ...

Tài liệu được xem nhiều: