Book Image

Jasmine JavaScript Testing

By : Paulo Ragonha
Book Image

Jasmine JavaScript Testing

By: Paulo Ragonha

Overview of this book

<p>From a little renegade scripting language to the de facto standard platform of today, JavaScript has become a universal language available in the widest range of devices; it is truly the 'write once, run everywhere’ language. However, as JavaScript applications become more complicated, testing and applying sustainable software engineering practices also become mandatory.</p> <p>Jasmine JavaScript Testing is a practical guide to a more sustainable JavaScript development process. You will learn by example how to drive the development of a web application using tests and best practices.</p> <p>This book is about becoming a better JavaScript developer. So, throughout the chapters, you will not only learn about writing tests, but also about the best practices for writing software in the JavaScript language. This book is about acknowledging JavaScript as a real platform for application development and leveraging all of its potential. You will also learn about tooling and automation and how to make your life easier and more productive.</p> <p>You will learn how to create a sustainable codebase with the help of Jasmine. We will take a look at integrated testing (with a backend NodeJS server) and how you can speed this process up by faking AJAX requests. As you progress through the book, you will learn about the challenges of testing an application built on top of a framework and how you can prevent your application from suffering from dependency management hell. Also, since your applications need to get into production, you will learn about optimizing the code to reduce the number of requests the browser needs to make while loading your application.</p> <p>With this book, you will learn everything you need to know to become a real professional in the ever-demanding JavaScript universe.</p>
Table of Contents (16 chapters)

Testing Backbone collections


Backbone collections are basically Arrays with superpowers:

  • They come with a bunch of enumerable functions built in, such as map, sort, and select

  • They have support for all sorts of events such as addition, removal, and even changes made to individual models it contains

  • They support reading data from a remote server

Expect to use it a lot in conjunction with models in your Backbone application.

Let's see a small code snippet to get a feeling on how it works. Here we instantiate a new collection passing an array with an initial data item for it to start:

var collection = new Backbone.Collection([
  { id: 1, name 'first' }
]);

It will by default, create a new Backbone.Model for each object in that array, but it is possible to specify your own custom Model objects.

Next, we show how to add a new Model to the collection:

collection.add(new Backbone.Model({ id: 2, name: 'second' }));

And finally, we can retrieve the added model by its id attribute:

var model = collection.get...