Replacing local storage library with actively maintained one: grevory/angular-local-storage

This commit is contained in:
Rob Harrington
2016-09-21 12:21:11 +10:00
committed by Bing Xie
parent b59a1cc232
commit e57a25d05f
8 changed files with 562 additions and 193 deletions

View File

@@ -6,7 +6,7 @@ describe "CheckoutCtrl", ->
CurrentHubMock =
hub:
id: 1
storage = null
localStorageService = null
beforeEach ->
module("Darkswarm")
@@ -23,14 +23,16 @@ describe "CheckoutCtrl", ->
id: 1
email: "public"
user_id: 1
bill_address: 'bill_address'
ship_address: 'ship address'
secrets:
card_number: "this is a secret"
describe "with user", ->
beforeEach ->
inject ($controller, $rootScope, _storage_) ->
storage = _storage_
spyOn(storage, "bind").and.callThrough()
inject ($controller, $rootScope, _localStorageService_) ->
localStorageService = _localStorageService_
spyOn(localStorageService, "bind").and.callThrough()
scope = $rootScope.$new()
CurrentUser = { id: 1 }
ctrl = $controller 'CheckoutCtrl', {$scope: scope, Checkout: Checkout, CurrentUser: CurrentUser }
@@ -57,23 +59,24 @@ describe "CheckoutCtrl", ->
expect(scope.enabled).toEqual true
describe "Local storage", ->
it "binds to localStorage when given a scope", ->
it "binds to localStorage when given a scope", inject ($timeout) ->
prefix = "order_#{scope.order.id}#{CurrentUser.id or ""}#{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})
expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.default_bill_address", {storeName: "#{prefix}_defaultasbilladdress", defaultValue: false})
expect(storage.bind).toHaveBeenCalledWith(scope, "Checkout.default_ship_address", {storeName: "#{prefix}_defaultasshipaddress", defaultValue: false})
expect(localStorageService.bind).toHaveBeenCalledWith(scope, "Checkout.order.#{field}", Checkout.order[field], "#{prefix}_#{field}")
expect(localStorageService.bind).toHaveBeenCalledWith(scope, "Checkout.ship_address_same_as_billing", true, "#{prefix}_sameasbilling")
expect(localStorageService.bind).toHaveBeenCalledWith(scope, "Checkout.default_bill_address", false, "#{prefix}_defaultasbilladdress")
expect(localStorageService.bind).toHaveBeenCalledWith(scope, "Checkout.default_ship_address", false, "#{prefix}_defaultasshipaddress")
it "it can retrieve data from localstorage", ->
prefix = "order_#{scope.order.id}#{CurrentUser.id or ""}#{CurrentHubMock.hub.id}"
expect(localStorage.getItem("#{prefix}_email")).toMatch "public"
scope.$digest()
expect(localStorage.getItem("ls.#{prefix}_email")).toMatch "public"
it "does not store secrets in local storage", ->
Checkout.secrets =
card_number: "superfuckingsecret"
scope.$digest()
keys = (localStorage.key(i) for i in [0..localStorage.length])
for key in keys
expect(localStorage.getItem(key)).not.toMatch Checkout.secrets.card_number
@@ -88,6 +91,9 @@ describe "CheckoutCtrl", ->
expect(scope.enabled).toEqual false
it "does not store secrets in local storage", ->
Checkout.secrets =
card_number: "superfuckingsecret"
scope.$digest()
keys = (localStorage.key(i) for i in [0..localStorage.length])
for key in keys
expect(localStorage.getItem(key)).not.toMatch Checkout.secrets.card_number