Skip to content

Commit

Permalink
Fix and enhance specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucassus committed Jul 14, 2013
1 parent afb4e5a commit 1fc9d53
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@ class DocumentsShowCtrl

# Scope methods
$scope.isLoading = => @loading
$scope.refresh = =>
@alerts.info("Document was refreshed")
$scope.refresh = => @refreshDocument()

@fetchDocument()
@fetchDocument =>
@loading = false

fetchDocument: ->
fetchDocument: (onSuccess = ->) ->
@loading = true
onSuccess = => @loading = false
@document.$get(onSuccess, @handleNotFoundDocument)

refreshDocument: ->
@fetchDocument =>
@loading = false
@alerts.info("Document was refreshed")

# Redirects to the collection page
# alert.error will be set by the `httpErrorsInterceptor`
handleNotFoundDocument: (response) =>
return unless response.status is 404
# TODO use filter here
@$location.path("/databases/#{@dbName}/collections/#{@collectionName}/documents")

angular.module("mb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe "documents show controller", ->

$httpBackend = $injector.get("$httpBackend")
$httpBackend.whenGET("/api/databases/test_database/collections/test_collection/documents/document_id")
.respond({})
.respond(dbName: "test_database", collectionName: "test_collection", id: "document_id")

$scope = $rootScope.$new()
controller = $controller "documents.show",
Expand Down Expand Up @@ -52,17 +52,26 @@ describe "documents show controller", ->
alerts = $injector.get("alerts")

it "displays a flash message", ->
# Given
spyOn(alerts, "info")

# When
$scope.refresh()
$httpBackend.flush()

# Then
expect(alerts.info).toHaveBeenCalledWith("Document was refreshed")

# TODO think how to dry it
it "fetches a document from the database", ->
# Given
$httpBackend.whenGET("/api/databases/test_database/collections/test_collection/documents/document_id")
.respond({})

# When
$scope.refresh()
$httpBackend.flush()

# Then
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()

Expand Down
52 changes: 26 additions & 26 deletions spec/javascripts/app/directives_spec.js.coffee
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
describe "directives", ->
beforeEach module("mb.directives")

$scope = null
$rootScope = null
element = null

describe "osEsc", ->
beforeEach inject ($compile, $rootScope) ->
$scope = $rootScope
$scope.bar = ->
beforeEach inject ($compile, _$rootScope_) ->
$rootScope = _$rootScope_
$rootScope.bar = ->

element = angular.element """
<input type="text" on-esc="bar()" />
"""
$compile(element)($scope)
$scope.$digest()
$compile(element)($rootScope)
$rootScope.$digest()

it "calls the given function when the ESC was pressed", ->
spyOn($scope, 'bar')
spyOn($rootScope, 'bar')

event = jQuery.Event("keyup", keyCode: 27)
element.trigger(event)

expect($scope.bar).toHaveBeenCalled()
expect($rootScope.bar).toHaveBeenCalled()

it "does nothing on other keys", ->
spyOn($scope, 'bar')
spyOn($rootScope, 'bar')

event = jQuery.Event("keyup", keyCode: 13)
element.trigger(event)

expect($scope.bar).not.toHaveBeenCalled()
expect($rootScope.bar).not.toHaveBeenCalled()

describe "showButton", ->
renderedButton = null
Expand Down Expand Up @@ -86,16 +86,16 @@ describe "directives", ->

describe "deleteButton", ->
beforeEach inject ($compile, $rootScope) ->
$scope = $rootScope
$scope.bar = ->
$rootScope = $rootScope
$rootScope.bar = ->

element = angular.element """
<div>
<delete-button ng-click="bar()" />
</div>
"""
$compile(element)($scope)
$scope.$digest()
$compile(element)($rootScope)
$rootScope.$digest()

it "renders delete button", ->
button = element.find("a")
Expand All @@ -106,25 +106,24 @@ describe "directives", ->
expect(button.text()).toContain("Delete")

it "ng-click", ->
spyOn($scope, 'bar')
spyOn($rootScope, 'bar')

button = element.find("button")
button.click()

expect($scope.bar).not.toHaveBeenCalled()
expect($rootScope.bar).not.toHaveBeenCalled()

describe "refreshButton", ->
beforeEach inject ($compile, $rootScope) ->
$scope = $rootScope
$scope.refresh = ->
beforeEach inject ($compile) ->
$rootScope.refresh = ->

element = angular.element """
<div>
<refresh-button ng-click="refresh()" />
</div>
"""
$compile(element)($scope)
$scope.$digest()
$compile(element)($rootScope)
$rootScope.$digest()

it "renders delete button", ->
button = element.find("a")
Expand All @@ -133,10 +132,11 @@ describe "directives", ->
expect(button.hasClass("btn")).toBeTruthy()
expect(button.text()).toContain("Refresh")

it "ng-click", ->
spyOn($scope, "refresh")
describe "ng-click on the button", ->
it "calls #refresh() method", ->
spyOn($rootScope, "refresh")

button = element.find("button")
button.click()
button = element.find("a")
button.click()

expect($scope.refresh).not.toHaveBeenCalled()
expect($rootScope.refresh).toHaveBeenCalled()

0 comments on commit 1fc9d53

Please sign in to comment.