Skip to content

Commit

Permalink
Move global beforeEach and afterEach in tests to test/_config.coffee
Browse files Browse the repository at this point in the history
  • Loading branch information
jtomaszewski authored and Max Edmands committed Aug 25, 2014
1 parent 190a2c2 commit 3ff9006
Show file tree
Hide file tree
Showing 27 changed files with 21 additions and 147 deletions.
1 change: 1 addition & 0 deletions karma.conf.coffee
Expand Up @@ -14,6 +14,7 @@ module.exports = (config) ->

'src/index.coffee'

'test/_config.coffee'
'test/*.test.coffee'
]

Expand Down
1 change: 1 addition & 0 deletions karma.full.conf.coffee
Expand Up @@ -15,6 +15,7 @@ module.exports = (config) ->

'angular-cached-resource.js'

'test/_config.coffee'
'test/*.test.coffee'
]

Expand Down
10 changes: 10 additions & 0 deletions test/_config.coffee
@@ -0,0 +1,10 @@
# This file is ran before every *.test.coffee .
beforeEach ->
localStorage.clear() # TODO this should not be actually necessary
module('ngCachedResource')

afterEach ->
inject ($httpBackend) ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()
6 changes: 0 additions & 6 deletions test/bound_params_nonexistent.test.coffee
Expand Up @@ -2,18 +2,12 @@ describe 'bound params do not match array response', ->
{villains, $httpBackend, $log} = {}

beforeEach ->
module 'ngCachedResource'
inject ($injector) ->
$log = $injector.get '$log'
$httpBackend = $injector.get '$httpBackend'
$cachedResource = $injector.get '$cachedResource'
Villain = $cachedResource('villain', '/villain/:name', { name: '@name'})
villains = Villain.query()

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()
$log.reset()

it 'should display a warning message', ->
Expand Down
6 changes: 0 additions & 6 deletions test/cache_adding.test.coffee
Expand Up @@ -3,17 +3,11 @@ describe 'adding resources to cache', ->
{Computer, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
Computer = $cachedResource 'computer', '/computer/:sn', {sn: '@sn'},

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'with a class method', ->

it '$addToCache', ->
Expand Down
21 changes: 9 additions & 12 deletions test/class_clear_cache.test.coffee
Expand Up @@ -2,31 +2,28 @@ describe 'CachedResource.$clearCache()', ->
{CachedResource, $cachedResource, $httpBackend, rabbits, combos} = {}

beforeEach ->
localStorage.clear() # TODO this should not be actually necessary
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'class-clear-test', '/animals/:name', {name: '@name'}

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'with a populated cache', ->

beforeEach ->
CachedResource.$addArrayToCache type: 'fictional-rabbits', [
CachedResource.$addArrayToCache type: 'fictional-rabbits', rabbits = [
{ name: 'white-rabbit', source: 'Alice In Wonderland' }
{ name: 'peppy-hare', source: 'Starfox' }
{ name: 'energizer-bunny', source: 'Energizer' }
{ name: 'frank', source: 'Donnie Darko' }
]
CachedResource.$addArrayToCache type: 'combos', [
CachedResource.$addArrayToCache type: 'combos', combos = [
{ name: 'liger', from: ['Lion', 'Tiger'] }
{ name: 'groler-bear', from: ['Grizzly Bear', 'Polar Bear'] }
]
$httpBackend.expectGET('/animals?type=fictional-rabbits').respond rabbits
$httpBackend.expectGET('/animals?type=combos').respond combos
rabbits = CachedResource.query type: 'fictional-rabbits'
combos = CachedResource.query type: 'combos'
$httpBackend.flush()

it 'should remove all entries from the cache', ->
CachedResource.$clearCache()
Expand Down Expand Up @@ -88,11 +85,11 @@ describe 'CachedResource.$clearCache()', ->
$httpBackend.flush()

it 'should remove all entries from the cache', ->
RouteResource.$clearAll()
RouteResource.$clearCache()
expect(localStorage.length).to.equal 0

it 'should remove all entries from the cache except for those specified by a key', ->
RouteResource.$clearAll exceptFor: {type: 'easy'}
RouteResource.$clearCache exceptFor: {type: 'easy'}
expect(localStorage.length).to.equal 3
expect(localStorage.getItem('cachedResource://route-resource/array?type=easy')).to.contain 1
expect(localStorage.getItem('cachedResource://route-resource?routeId=1')).to.contain 'direct'
Expand Down
6 changes: 0 additions & 6 deletions test/class_get.test.coffee
Expand Up @@ -2,17 +2,11 @@ describe 'resource instance returned by CachedResource.get', ->
{CachedResource, $httpBackend, resourceInstance} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource('class-get-test', '/mock/:id')

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'when cache is empty', ->
expectSuccessfulGET = ->
$httpBackend.expectGET('/mock/1').respond magic: 'Here is the response'
Expand Down
6 changes: 0 additions & 6 deletions test/class_get_array.test.coffee
Expand Up @@ -2,18 +2,12 @@ describe 'CachedResource.get array resource collections', ->
{$httpBackend, CachedResource, resourceCollection} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'class-get-array-test', '/colors/:color',
color: '@color'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'when cache is empty', ->

expectSuccessfulGET = ->
Expand Down
6 changes: 0 additions & 6 deletions test/class_get_array_cache_populate.test.coffee
Expand Up @@ -2,17 +2,11 @@ describe 'CachedResource.get cache population from an isArray request', ->
{$httpBackend, CachedResource} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'class-get-array-test', '/colors/:color', {color: '@color'}

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'when cache is populated by a parameterless query', ->
beforeEach ->
$httpBackend.expectGET('/colors').respond [
Expand Down
6 changes: 0 additions & 6 deletions test/class_get_array_callback.test.coffee
Expand Up @@ -2,18 +2,12 @@ describe 'CachedResource.get array resource collections with callback', ->
{$httpBackend, CachedResource} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'class-get-array-callback-test', '/colors/:color',
color: '@color'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

it 'only calls the callback once', ->
$httpBackend.expectGET('/colors').respond [
{ color: 'red', hex: '#FF0000' }
Expand Down
6 changes: 0 additions & 6 deletions test/class_get_without_params.test.coffee
Expand Up @@ -2,17 +2,11 @@ describe 'CachedResource.get without params', ->
{$httpBackend, CachedResource} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource('class-paramless-test', '/monster')

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

it 'should call the callback after it makes the initial request', (done) ->
$httpBackend.expectGET('/monster').respond {sound: 'rarrrr'}
CachedResource.get (response) ->
Expand Down
6 changes: 0 additions & 6 deletions test/class_post.test.coffee
Expand Up @@ -2,18 +2,12 @@ describe 'CachedResource.post', ->
{CachedResource, $httpBackend, $timeout} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
$timeout = $injector.get '$timeout'
CachedResource = $cachedResource 'class-save-test', '/mock/:id'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'while online', ->
it 'saves the resource normally', (done) ->
$httpBackend.expectPOST('/mock/1', magic: 'This is a saved resource').respond
Expand Down
6 changes: 0 additions & 6 deletions test/class_with_custom_actions.test.coffee
Expand Up @@ -2,7 +2,6 @@ describe 'class with custom actions', ->
{CachedResource, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
Expand All @@ -12,11 +11,6 @@ describe 'class with custom actions', ->
kiai:
method: "put"

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

it "has default actions", ->
for action in ['get', 'query', 'save', 'remove', 'delete']
expect( CachedResource ).to.have.property action
Expand Down
6 changes: 0 additions & 6 deletions test/class_writes_then_reads.test.coffee
Expand Up @@ -2,17 +2,11 @@ describe 'Class writes, then reads', ->
{CachedResource, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'class-writes-then-reads-test', '/mock/:id', {id: '@id'}

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'when the write was unsuccessful', ->
beforeEach ->
$httpBackend.expectPOST('/mock/1').respond 503
Expand Down
6 changes: 0 additions & 6 deletions test/custom_actions_with_additional_url_params.test.coffee
Expand Up @@ -2,7 +2,6 @@ describe 'custom actions with addtional URL parameters', ->
{Group, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
Expand All @@ -11,11 +10,6 @@ describe 'custom actions with addtional URL parameters', ->
method: "PUT"
url: '/contacts/:contactId/groups/:id'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

it 'sends a request to the custom URL', ->
$httpBackend.expectPUT('/contacts/C-3PO/groups/cybot_galactica', { id: 'cybot_galactica', location: 'Etti IV'}).respond 200
Group.saveByContact { contactId: 'C-3PO' }, { id: 'cybot_galactica', location: 'Etti IV' }
Expand Down
6 changes: 0 additions & 6 deletions test/disable_cache.test.coffee
Expand Up @@ -2,19 +2,13 @@ describe 'disabling cache for certain actions', ->
{Goat, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
Goat = $cachedResource 'disable-cache-test', '/goats/:name', {name: '@name'},
bleat: { url: '/goats/:name/bleat', method: 'PUT', cache: off }
kick: { url: '/goats/:name/kick', method: 'PUT' }

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'Goat class', ->
it 'does not cache when bleating', ->
$httpBackend.expectPUT('/goats/houdini/bleat').respond 500
Expand Down
6 changes: 0 additions & 6 deletions test/existing_write_queue.test.coffee
Expand Up @@ -5,17 +5,11 @@ describe 'an existing write queue', ->
localStorage.setItem 'cachedResource://existing-write-queue-test/write', angular.toJson [{params: {id: 1}, resourceParams: {id: 1}, action: 'save'}]
localStorage.setItem 'cachedResource://existing-write-queue-test?id=1', angular.toJson value: magic: 'from the cache'

module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
$timeout = $injector.get '$timeout'

afterEach ->
localStorage.clear()
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()

it 'saves the item that was stored in the cache', ->
$httpBackend.expectPOST('/mock/1', magic: 'from the cache').respond 200
$cachedResource 'existing-write-queue-test', '/mock/:id'
Expand Down
3 changes: 0 additions & 3 deletions test/full_local_storage_cache.test.coffee
Expand Up @@ -15,16 +15,13 @@ conditionallyDescribe 'a full localStorage cache', ->

beforeEach ->
sinon.stub(window.localStorage, 'setItem').throws 'QuotaExceededError'
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
CachedResource = $cachedResource 'full-local-storage-cache', '/mock/:id', id: '@id'

afterEach ->
window.localStorage.setItem.restore()
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()

it 'still succeeds at GET requests', ->
$httpBackend.expectGET('/mock/42').respond question: 'The ultimate question of life, the universe, and everything'
Expand Down
6 changes: 0 additions & 6 deletions test/global_clear_cache.test.coffee
Expand Up @@ -2,16 +2,10 @@ describe '$cachedResource.clearCache()', ->
{$cachedResource, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'with a populated cache', ->

beforeEach ->
Expand Down
6 changes: 0 additions & 6 deletions test/global_clear_undefined.test.coffee
Expand Up @@ -2,16 +2,10 @@ describe '$cachedResource.clearUndefined()', ->
{$cachedResource, $httpBackend} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'with an item corresponding to a resource in the cache', ->

beforeEach ->
Expand Down
6 changes: 0 additions & 6 deletions test/instance_post.test.coffee
Expand Up @@ -2,19 +2,13 @@ describe 'CachedResource::post', ->
{resourceInstance, $httpBackend, $timeout} = {}

beforeEach ->
module('ngCachedResource')
inject ($injector) ->
$cachedResource = $injector.get '$cachedResource'
$httpBackend = $injector.get '$httpBackend'
$timeout = $injector.get '$timeout'
CachedResource = $cachedResource 'instance-post-test', '/mock/:id', {id: '@id'}
resourceInstance = new CachedResource id: 1, notes: 'this is a saved note', list: [1,2,3]

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()
localStorage.clear()

describe 'while online', ->
it 'POSTS the entire body of the resource', ->
$httpBackend.expectPOST('/mock/1', { id: 1, notes: 'this is a saved note', list: [1,2,3] }).respond 200
Expand Down

0 comments on commit 3ff9006

Please sign in to comment.