TAILIEUCHUNG - Test Driven JavaScript Development- P20

Test Driven JavaScript Development- P20:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. | Event Emitters 373 Listing Expecting chatRoom to be event emitter testCase exports chatRoom should be event emitter function test We can pass this test by popping in as chatRoom s prototype as seen in Listing . Listing chatRoom inheriting from . var EventEmitter require events .EventEmitter . var chatRoom function user message . function id . Note that because V8 fully supports ECMAScript 5 s we could have used property descriptors to add the methods as well as seen in Listing . Listing chatRoom defined with property descriptors var chatRoom addMessage value function user message . getMessagesSince value function id . Please purchase PDF Split-Merge on to remove this watermark. From the Library of 374 Server-Side JavaScript with At this point the property descriptors don t provide anything we have a documented need for . the ability to override default property attribute values so we ll avoid the added indentation and stick with the simple assignments in Listing . Next up we make sure that addMessage emits an event. Listing shows the test. Listing Expecting addMessage to emit a message event testCase exports . should emit message event function test var message message function m message m cjno msg .then function m m message To pass this test we need to place a call to emit right before we resolve the promise as seen in Listing . Listing Emitting a message event function user message callback var promise new Promise function . if err . message data data else .

TÀI LIỆU HOT