O'Reilly logo

Test-Driven JavaScript Development by Harmeet Singh, Hetal Prajapati, Ravi Kumar Gupta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Implementing the observer pattern using TDD

We will see how we can use TDD to implement the observer pattern. We will use the red, green, and refactor life cycle of TDD to understand it in more detail.

Red step (code with error)

Let's try to add code, which has some dependency. Don't include dependency in the first step and see what error it's giving. Normally in the red step, we have some code with error, and then in next step we add proper code to make it green.

<script type="text/javascript"> Object.defineProperty(employee.status, 'Active', { get: function () { return inactive; }, set: function (status) { Object.getNotifier(this).notify({ type: 'update', name: 'Inactive', oldValue: inactive }); // Let's also print the value in logger anytime it ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required