Fixing regressions and refactoring our tests

This commit is contained in:
Will Marshall
2014-07-16 16:00:44 +10:00
parent cd033c300e
commit 2b1ab53d8e
14 changed files with 304 additions and 289 deletions

View File

@@ -10,29 +10,15 @@ describe "AccordionCtrl", ->
module ($provide)->
$provide.value "CurrentHub", CurrentHubMock
null
localStorage.clear()
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
scope.order =
id: 129
ctrl = $controller 'AccordionCtrl', {$scope: scope}
describe "loading incomplete form", ->
beforeEach ->
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
scope.order =
id: 129
ctrl = $controller 'AccordionCtrl', {$scope: scope}
it "defaults the details accordion to visible", ->
expect(scope.accordion.details).toEqual true
it "defaults the details accordion to visible", ->
expect(scope.accordion.details).toEqual true
it "changes accordion", ->
scope.show "shipping"
expect(scope.accordion["shipping"]).toEqual true
describe "loading complete form", ->
beforeEach ->
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
scope.checkout =
$valid: true
scope.order =
id: 129
ctrl = $controller 'AccordionCtrl', {$scope: scope}
it "changes accordion", ->
scope.show "shipping"
expect(scope.accordion["shipping"]).toEqual true

View File

@@ -1,47 +1,72 @@
describe "CheckoutCtrl", ->
ctrl = null
scope = null
Order = null
Checkout = null
CurrentUser = null
CurrentHubMock =
hub:
id: 1
storage = null
beforeEach ->
module("Darkswarm")
angular.module('Darkswarm').value('user', {})
Order =
angular.module('Darkswarm').value('currentHub', {id: 1})
module ($provide)->
$provide.value "CurrentHub", CurrentHubMock
null
Checkout =
submit: ->
navigate: ->
bindFieldsToLocalStorage: ->
order:
id: 1
email: "public"
user_id: 1
secrets:
card_number: "this is a secret"
describe "with user", ->
beforeEach ->
inject ($controller, $rootScope) ->
inject ($controller, $rootScope, _storage_) ->
storage = _storage_
spyOn(storage, "bind").andCallThrough()
scope = $rootScope.$new()
spyOn(Order, "bindFieldsToLocalStorage")
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Order: Order, CurrentUser: {}}
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Checkout: Checkout, CurrentUser: {}}
it "delegates to the service on submit", ->
event =
preventDefault: ->
spyOn(Order, "submit")
spyOn(Checkout, "submit")
scope.purchase(event)
expect(Order.submit).toHaveBeenCalled()
expect(Checkout.submit).toHaveBeenCalled()
it "is enabled", ->
expect(scope.enabled).toEqual true
it "triggers localStorage binding", ->
expect(Order.bindFieldsToLocalStorage).toHaveBeenCalled()
describe "Local storage", ->
it "binds to localStorage when given a scope", ->
prefix = "order_#{scope.order.id}#{scope.order.user_id}#{CurrentHubMock.hub.id}"
field = scope.fieldsToBind[0]
expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.order.#{field}", {storeName: "#{prefix}_#{field}"})
expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.ship_address_same_as_billing", {storeName: "#{prefix}_sameasbilling", defaultValue: true})
it "it can retrieve data from localstorage", ->
prefix = "order_#{scope.order.id}#{scope.order.user_id}#{CurrentHubMock.hub.id}"
expect(localStorage.getItem("#{prefix}_email")).toMatch "public"
it "does not store secrets in local storage", ->
Checkout.secrets =
card_number: "superfuckingsecret"
keys = (localStorage.key(i) for i in [0..localStorage.length])
for key in keys
expect(localStorage.getItem(key)).not.toMatch Checkout.secrets.card_number
describe "without user", ->
beforeEach ->
inject ($controller, $rootScope) ->
scope = $rootScope.$new()
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Order: Order, CurrentUser: undefined}
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Checkout: Checkout, CurrentUser: undefined}
it "is disabled", ->
expect(scope.enabled).toEqual false
@@ -49,4 +74,4 @@ describe "CheckoutCtrl", ->
it "does not store secrets in local storage", ->
keys = (localStorage.key(i) for i in [0..localStorage.length])
for key in keys
expect(localStorage.getItem(key)).not.toMatch Order.secrets.card_number
expect(localStorage.getItem(key)).not.toMatch Checkout.secrets.card_number

View File

@@ -14,5 +14,6 @@ describe 'OrderCycleCtrl', ->
scope = {}
ctrl = $controller 'OrderCycleCtrl', {$scope: scope, OrderCycle: OrderCycle}
#it "puts the order cycle in scope", ->
#expect(scope.order_cycle).toEqual "test"
it "puts the order cycle in scope", ->
expect(scope.order_cycle).toEqual "test"

View File

@@ -5,6 +5,7 @@ describe "ProductNodeCtrl", ->
id: 99
price: 10.00
variants: []
supplier: {}
beforeEach ->
module('Darkswarm')
@@ -13,12 +14,5 @@ describe "ProductNodeCtrl", ->
product: product
ctrl = $controller 'ProductNodeCtrl', {$scope: scope}
describe "determining the price to display for a product", ->
it "displays the product price when the product does not have variants", ->
expect(scope.price()).toEqual 10.00
it "displays the minimum variant price when the product has variants", ->
scope.product =
price: 11
variants: [{price: 22}, {price: 33}]
expect(scope.price()).toEqual 22
it "puts a reference to supplier in the scope", ->
expect(scope.enterprise).toBe product.supplier

View File

@@ -9,7 +9,7 @@ describe 'ProductsCtrl', ->
Product =
all: ->
update: ->
products: "testy mctest"
products: ["testy mctest"]
OrderCycle =
order_cycle: {}
@@ -18,4 +18,19 @@ describe 'ProductsCtrl', ->
ctrl = $controller 'ProductsCtrl', {$scope: scope, Product: Product, OrderCycle: OrderCycle}
it 'fetches products from Product', ->
expect(scope.Product.products).toEqual 'testy mctest'
expect(scope.Product.products).toEqual ['testy mctest']
it "increments the limit up to the number of products", ->
scope.limit = 0
scope.incrementLimit()
expect(scope.limit).toEqual 1
scope.incrementLimit()
expect(scope.limit).toEqual 1
it "blocks keypresses on code 13", ->
event =
keyCode: 13
preventDefault: ->
spyOn(event, 'preventDefault')
scope.searchKeypress(event)
expect(event.preventDefault).toHaveBeenCalled()