Skip to content

Commit

Permalink
#11 [WIP] - added interation tests of modal and growl for login
Browse files Browse the repository at this point in the history
  • Loading branch information
umairabid-tkxel committed Dec 9, 2014
1 parent d9898e4 commit 4c37d4d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 8 deletions.
32 changes: 31 additions & 1 deletion app/mixins/groovy_response_handler.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ mixin = Ember.Mixin.create

error_group : null # to hold namespace of errors we are dealing with

proxy : null
proxy : null # holds the proxy object of controller to add dynamic properties

modalOpen: false # modal flag to toggle modal in case of modal errors

##
# modalOpen observer
# hides modal if it is open
##

modalChanged: Ember.observer ->
unless @get("modalOpen")
@set("modalOpen", false)
.observes("modalOpen")

##
# acts like a sort of constructor for
# initializing errors, inspired from eVisit's form_state_mixin
##

errorCallback : (response, controller) ->
@resetErrors()
@setupProxy(controller)
if typeof response.jqXHR isnt "undefined"
response = response.jqXHR.responseJSON
Expand All @@ -21,15 +35,31 @@ mixin = Ember.Mixin.create
switch @get("error_group")
when "inline"
@handleInlineErrors()
when "modal"
@handleModalErrors()
when "growl"
@handleGrowlErrors()

handleInlineErrors : ->
fields = Object.keys(@errors.fields)
for field in fields
@proxy.set("#{field}Error", true)
@proxy.set("#{field}Messages", @errors.fields[field])

handleModalErrors : ->
@proxy.set("modal_error_title", @errors.title)
@proxy.set("modal_error_message", @errors.message)
@set("modalOpen", true)

handleGrowlErrors : ->
sweetAlert(@errors.title, @errors.message, @errors.error)


resetErrors : () ->
if @get("proxy") isnt null
@get("proxy").destroy()
@set("modalOpen", false)

setupProxy : (controller) ->
proxy = Ember.ObjectProxy.create
content: controller
Expand Down
11 changes: 10 additions & 1 deletion app/templates/login.emblem
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
.col-md-6
button.btn.btn-success.col-md-12 id="login-button" Login
.col-md-6
link-to "register" class="btn btn-default col-md-12" | Register Now
link-to "register" class="btn btn-default col-md-12" | Register Now

if modalOpen
.modal
Ember.Checkbox class="modal-state" id="modal-1" checkedBinding="modalOpen"
.modal-window
.modal-inner
label.modal-close for="modal-1"
h1 = modal_title
p.intro = modal_message
77 changes: 71 additions & 6 deletions tests/integration/login-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
`import { test } from "ember-qunit"`
`import startApp from "../helpers/start-app"`
`import assetModalPresent from "../helpers/assert-modal-present"`
`import assertAlertPresent from "../helpers/assert-alert-present"`


App = null
Expand Down Expand Up @@ -32,18 +33,55 @@ inline_response = {
}
}

modal_response = {
'errors' : {
'error_group' : 'modal',
'title' : 'Some Error Occurred',
'message' : 'We are sorry that some error occurred'
}
}

growl_response = {
'errors' : {
'error_group' : 'growl',
'title' : "Sorry, Your account isn't verified yet",
'message' : "Check back later when your account will be verified by our admin",
'type' : "error"
}
}

inlineErrors = ->

data = {}
data["v#{config.apiVersion}_user"] = {"email" : "abc" : "password" : "123"}
data["v#{config.apiVersion}_user"] = {"email" : "abc@abc.com" : "password" : "123"}
Ember.$.mockjax
url: "#{config.apiNamespace}/users/sign_in.json"
type: 'POST'
#data: data
status: 500
responseText: inline_response

test "Inline errors occured", ->
modalErrors =->
data = {}
data["v#{config.apiVersion}_user"] = {"email" : "[email protected]" : "password" : "123"}
Ember.$.mockjax
url: "#{config.apiNamespace}/users/sign_in.json"
type: 'POST'
#data: data
status: 500
responseText: modal_response

growlErrors =->
data = {}
data["v#{config.apiVersion}_user"] = {"email" : "[email protected]" : "password" : "123"}
Ember.$.mockjax
url: "#{config.apiNamespace}/users/sign_in.json"
type: 'POST'
#data: data
status: 500
responseText: growl_response

test "Inline errors are shown on inline error response", ->
inlineErrors()

visit('/login').then(
Expand All @@ -52,11 +90,38 @@ test "Inline errors occured", ->
$("#login-button").simulate("click")
setTimeout(
->
ok $("#email").closest('.form-group').hasClass('has-error')
ok $("#password").closest('.form-group').hasClass('has-error')
ok $("ul#email-messages li").length > 0
ok $("ul#password-messages li").length > 0
ok $("#email").closest('.form-group').hasClass('has-error'), 'Email has class "has-error"'
ok $("#password").closest('.form-group').hasClass('has-error'), 'Password has class "has-error"'
ok $("ul#email-messages li").length > 0, 'Email errors are listed'
ok $("ul#password-messages li").length > 0, 'Password errors are listed'
start()
, 200)
)

test "modal is shown on modal error response", ->
growlErrors()

visit('/login').then(
->
stop()
$("#login-button").simulate("click")
setTimeout(
->
assetModalPresent()
start()
, 200)
)

test "alert is shown on growl error response", ->
growlErrors()

visit('/login').then(
->
stop()
$("#login-button").simulate("click")
setTimeout(
->
assertAlertPresent()
start()
, 200)
)

0 comments on commit 4c37d4d

Please sign in to comment.