Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
feat(spec): add test coverages
Browse files Browse the repository at this point in the history
* add confirm directive test
* change runs, waitsFor to use $rootScope.$digest
  • Loading branch information
tomchentw committed Jan 24, 2014
1 parent fe95ac3 commit 35b186a
Showing 1 changed file with 81 additions and 33 deletions.
114 changes: 81 additions & 33 deletions src/angular-ujs.spec.ls
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ beforeEach inject !(_$compile_, _$rootScope_, _$document_, _$httpBackend_, _$sni
afterEach !(...) ->
$httpBackend.verifyNoOutstandingExpectation!
$httpBackend.verifyNoOutstandingRequest!
$rootScope.$destroy!

it 'should start test' !(...) ->
expect true .toBeTruthy!
Expand Down Expand Up @@ -194,6 +195,38 @@ describe 'RailsRemoteFormCtrl' !(...) ->
$httpBackend.flush!
$element.remove!

describe 'confirm directive' !(...) ->
$scope = confirmSpy = void

beforeEach !(...) ->
$scope := $rootScope.$new!
confirmSpy := spyOn window, 'confirm'

afterEach !(...) ->
$scope.$destroy!

it 'should show confirm dialog' !(...) ->
const $element = $compile('''
<button data-confirm="confirm..."></button>
''')($scope)

$document.find 'body' .append $element
$element.click!

expect confirmSpy .toHaveBeenCalled!

it 'should allow confirm' !(...) ->
confirmSpy.andReturn true

const $element = $compile('''
<button data-confirm="confirm..."></button>
''')($scope)

$document.find 'body' .append $element
$element.click!

expect confirmSpy .toHaveBeenCalled!

describe 'remote directive' !(...) ->
$scope = void

Expand Down Expand Up @@ -289,51 +322,66 @@ describe 'method directive with remote directive' !(...) ->
afterEach !(...) ->
$scope.$destroy!

it "should submit with remote form" !(...) ->
it 'should submit and emit success with remote form' !(...) ->
response = false
runs !->
$httpBackend.expectPOST '/users/sign_out' do
_method: 'DELETE'
.respond 201
$httpBackend.expectPOST '/users/sign_out' do
_method: 'DELETE'
.respond 201

const $element = $compile('''
<a href="/users/sign_out" data-method="DELETE" data-remote="true">SignOut</a>
''')($scope)
$document.find 'body' .append $element

const $element = $compile('''
<a href="/users/sign_out" data-method="DELETE" data-remote="true">SignOut</a>
''')($scope)
$document.find 'body' .append $element
$scope.$on 'rails:remote:success' !->
response := true

$scope.$on 'rails:remote:success' !->
response := true
$element.click!
$httpBackend.flush!
$rootScope.$digest!

expect response .toBeTruthy!

it 'should submit and emit error with remote form' !(...) ->
error = false
$httpBackend.expectPOST '/users/sign_out' do
_method: 'PUT'
.respond 404

const $element = $compile('''
<a href="/users/sign_out" data-method="PUT" data-remote="true">SignOut</a>
''')($scope)
$document.find 'body' .append $element

$element.click!
$httpBackend.flush!
$scope.$on 'rails:remote:error' !->
error := true

waitsFor ->
response
, 'response should be returned', 500
$element.click!
$httpBackend.flush!
$rootScope.$digest!

expect error .toBeTruthy!

it 'should work with confirm and remote form' !(...) ->
response = false
spyOn window, 'confirm' .andReturn true
$httpBackend.expectPOST '/users/sign_out' do
_method: 'DELETE'
.respond 201

runs !->
spyOn window, 'confirm' .andReturn true
$httpBackend.expectPOST '/users/sign_out' do
_method: 'DELETE'
.respond 201

const $element = $compile('''
<a href="/users/sign_out" data-method="DELETE" data-remote="true" data-confirm="Are u sure?">SignOut</a>
''')($scope)
$document.find 'body' .append $element
const $element = $compile('''
<a href="/users/sign_out" data-method="DELETE" data-remote="true" data-confirm="Are u sure?">SignOut</a>
''')($scope)
$document.find 'body' .append $element

$scope.$on 'rails:remote:success' !->
response := true
$scope.$on 'rails:remote:success' !->
response := true

$element.click!
$httpBackend.flush!
$element.click!
$httpBackend.flush!
$rootScope.$digest!

waitsFor ->
response
, 'response should be returned', 500
expect response .toBeTruthy!



Expand Down

0 comments on commit 35b186a

Please sign in to comment.