| 1 | # Testing Cocktail Mixins |
| 2 | |
| 3 | This readme presents a testing pattern for testing mixins with [Jasmine](http://www.github.com/pivotal/jasmine). To run the specs for this example, open up `example/SpecRunner.html` |
| 4 | |
| 5 | ### The problem |
| 6 | |
| 7 | Mixins define new behavior that can be applied across multiple Backbone objects. This DRYs up code nicely. But how to test each object that uses the mixin without repeating the same test code across multiple specs? |
| 8 | |
| 9 | ### The heart of the problem |
| 10 | |
| 11 | One solution is to DRY up our specs by only testing one of the objects that uses the mixin in question. This is the wrong answer: we really do want to test that our mixin operates correctly in *all* objects that use it. |
| 12 | |
| 13 | Moreover, this becomes crucially important in an interpreted language like JavaScript where there are no explicit interfaces. Quite often objects that use mixins must abide by some sort of **implicit** contract. An easy to use, and robust, spec can serve the dual purpose of making that contract **explicit** *and* of enforcing it. |
| 14 | |
| 15 | ### An example mixin |
| 16 | |
| 17 | Consider a mixin that allows views to perform some basic pagination: |
| 18 | |
| 19 | // PaginateMixin.js |
| 20 | |
| 21 | window.MyMixins = {}; |
| 22 | MyMixins.PaginateMixin = { |
| 23 | initialize: function() { |
| 24 | this.currentPage = 0; |
| 25 | }, |
| 26 | |
| 27 | events: { |
| 28 | 'click .previous': 'goToPreviousPage', |
| 29 | 'click .next': 'goToNextPage' |
| 30 | }, |
| 31 | |
| 32 | render: function() { |
| 33 | this.$el.append('<div class="previous"></div>'); |
| 34 | this.$el.append('<div class="next"></div>'); |
| 35 | this.renderCurrentPage(this.currentPage); |
| 36 | }, |
| 37 | |
| 38 | goToPage: function(page) { |
| 39 | var numberOfPages = this.numberOfPages(); |
| 40 | |
| 41 | this.currentPage = Math.min(Math.max(page, 0), numberOfPages - 1); |
| 42 | this.renderCurrentPage(this.currentPage); |
| 43 | |
| 44 | this.$('.previous').toggleClass('hidden', this.currentPage == 0); |
| 45 | this.$('.next').toggleClass('hidden', this.currentPage == numberOfPages - 1); |
| 46 | |
| 47 | return this.currentPage; |
| 48 | }, |
| 49 | |
| 50 | goToNextPage: function() { |
| 51 | return this.goToPage(this.currentPage + 1); |
| 52 | }, |
| 53 | |
| 54 | goToPreviousPage: function() { |
| 55 | return this.goToPage(this.currentPage - 1); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | It is not immediately obvious, but any class that uses this mixin must abide by an implicit contract. It must define `numberOfPages()` and `renderCurentPage()`. |
| 60 | |
| 61 | ### An example mixin spec |
| 62 | |
| 63 | Jasmine does not have a formal way to define shared behaviors. This is not a problem (we're using JavaScript after all)! Here's one way to pull off shared behaviors: |
| 64 | |
| 65 | // PaginateMixinSpec.js |
| 66 | |
| 67 | window.MyMixinSpecs = {}; |
| 68 | MyMixinSpecs.PaginateMixinSpec = function(context) { |
| 69 | describe("[Pagination]", function() { |
| 70 | var view, numberOfPages, ensureOnPage; |
| 71 | |
| 72 | beforeEach(function() { |
| 73 | view = context.view; |
| 74 | numberOfPages = context.numberOfPages; |
| 75 | ensureOnPage = context.ensureOnPage; |
| 76 | }); |
| 77 | |
| 78 | describe("The Contract", function() { |
| 79 | it("should implement numberOfPages", function() { |
| 80 | expect(view.numberOfPages).toBeTruthy(); |
| 81 | expect(view.numberOfPages()).toEqual(numberOfPages); |
| 82 | }); |
| 83 | |
| 84 | it("should implement renderCurrentPage", function() { |
| 85 | expect(view.renderCurrentPage).toBeTruthy(); |
| 86 | view.renderCurrentPage(0); |
| 87 | ensureOnPage(0); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe("jumping to a particular page", function() { |
| 92 | describe("when the page is in bounds", function() { |
| 93 | it("should jump to the specified page", function() { |
| 94 | view.goToPage(0); |
| 95 | ensureOnPage(0); |
| 96 | view.goToPage(numberOfPages - 1); |
| 97 | ensureOnPage(numberOfPages - 1); |
| 98 | }); |
| 99 | }); |
| 100 | |
| 101 | describe("when the page is out of bounds", function() { |
| 102 | it("should jump to the nearest legal page", function() { |
| 103 | view.goToPage(-1); |
| 104 | ensureOnPage(0); |
| 105 | view.goToPage(numberOfPages); |
| 106 | ensureOnPage(numberOfPages - 1); |
| 107 | }); |
| 108 | }); |
| 109 | }); |
| 110 | |
| 111 | describe("clicking between pages", function() { |
| 112 | it("should allow the user to go to the next/previous page and hide/show the next/previous buttons appropriately", function() { |
| 113 | view.goToPage(0); |
| 114 | expect(view.$('.previous').hasClass('hidden')).toBeTruthy(); |
| 115 | |
| 116 | view.$('.next').click(); |
| 117 | ensureOnPage(1); |
| 118 | expect(view.$('.previous').hasClass('hidden')).toBeFalsy(); |
| 119 | |
| 120 | view.goToPage(numberOfPages - 1); |
| 121 | ensureOnPage(numberOfPages - 1); |
| 122 | expect(view.$('.next').hasClass('hidden')).toBeTruthy(); |
| 123 | expect(view.$('.previous').hasClass('hidden')).toBeFalsy(); |
| 124 | |
| 125 | view.$('.previous').click(); |
| 126 | ensureOnPage(numberOfPages - 2); |
| 127 | expect(view.$('.next').hasClass('hidden')).toBeFalsy(); |
| 128 | }); |
| 129 | }) |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | That's a lot of code! All the more reason to avoid repeating it! |
| 134 | |
| 135 | Note that the mixin spec is simply a function builds a mini-hierarchy of Jasmine tests. This function accepts a `context` argument. The `context` allows our spec to pass information down to the mixin spec; in particular, we pass three things in: the `view` to be tested, the number of pages to expect the view to have, and a method called `ensureOnPage` that runs all the tests necessary to ensure that the view is, indeed, displaying the passed in page. |
| 136 | |
| 137 | Note also that we explicitly test the contract as part of the mixin test. This makes it very easy for the author of the mixin to communicate his or her intent to developers who wish to use the mixin. |
| 138 | |
| 139 | ### An example usage and its accompanying spec |
| 140 | |
| 141 | Consider this view: |
| 142 | |
| 143 | // PaginatedView.js |
| 144 | |
| 145 | window.MyPaginatedView = Backbone.View.extend({ |
| 146 | render: function() { |
| 147 | this.$el.append('<div class="the-number"></div>') |
| 148 | }, |
| 149 | |
| 150 | renderCurrentPage: function(page) { |
| 151 | this.$('.the-number').text(page + 1); |
| 152 | }, |
| 153 | |
| 154 | numberOfPages: function() { |
| 155 | return 10; |
| 156 | } |
| 157 | }) |
| 158 | |
| 159 | Cocktail.mixin(MyPaginatedView, MyMixins.PaginateMixin); |
| 160 | |
| 161 | Here's how we'd test this view: |
| 162 | |
| 163 | // PaginatedViewSpec.js |
| 164 | |
| 165 | describe("MyPaginatedView", function() { |
| 166 | var context = {}; |
| 167 | |
| 168 | beforeEach(function() { |
| 169 | context.view = new MyPaginatedView(); |
| 170 | context.numberOfPages = 10; |
| 171 | context.ensureOnPage = function(page) { |
| 172 | expect($('.the-number').text()).toEqual(page + 1 + ''); |
| 173 | } |
| 174 | |
| 175 | $('#content').append(context.view.$el); |
| 176 | context.view.render(); |
| 177 | }); |
| 178 | |
| 179 | MyMixinSpecs.PaginateMixinSpec(context); |
| 180 | }); |
| 181 | |
| 182 | Note that we set up the context as a closure variable in the outermost describe and **modify** (not *assign* to) that object in our `beforeEach`. The modifications get passed down to the hierarchy built by `MyMixinSpecs.PaginateMixinSpec` thanks to the closure. |
| 183 | |
| 184 | Obviously this is a somewhat canned example. However this principle could apply to more complicated mixins and classes. |