Skip to content

Instantly share code, notes, and snippets.

@regedarek
Created June 4, 2015 09:36
Show Gist options
  • Save regedarek/01ae30f1940ecd746d5c to your computer and use it in GitHub Desktop.
Save regedarek/01ae30f1940ecd746d5c to your computer and use it in GitHub Desktop.
@reloadWindow = ->
# window cannot be reloaded in jasmine tests
{ }
class FileUploadStub
constructor: ($el, options) ->
@done = options.done
@$el = $el
@addEventListeners
addEventListeners: ->
@$el.on
@fakeSuccessfulFileUpload = ($el, e, data) ->
spyOn($el, 'fileupload').and.callFake ->
args = $el.fileupload.mostRecentCall.args
args.done(e, data)
@fakeSuccessfulAjaxCall = (data) ->
spyOn($, 'ajax').and.callFake (req) ->
d = $.Deferred()
d.resolve(data)
d.promise()
@fakeUnsuccessfulAjaxCall = (data) ->
spyOn($, 'ajax').and.callFake (req) ->
d = $.Deferred()
d.reject(data)
d.promise()
class CoachesApp.AvatarUploader
constructor: ($el) ->
@$el = $el
@addEventListeners()
@modal = new CoachesApp.StandardModal
addEventListeners: ->
@setFileUpload()
@onImageHover()
setFileUpload: =>
@$el.fileupload
dataType: 'json',
done: (e, data) =>
if data.result.redirect_to
@modal.openModal
href: data.result.redirect_to
new CoachesApp.Jcrop($('#cropbox'))
if data.result.html
CoachesApp.EventBus.trigger('flash_message:added', data.result.html)
onImageHover: ->
$('.coach_profile-image').hover(
(e) ->
$(@).find('.overlay').animate {'opacity' : 1}
(e) ->
$(@).find('.overlay').animate {'opacity' : 0}
)
$ ->
if $('#new_editing_profile_avatar_form').length > 0
new CoachesApp.AvatarUploader($('#new_editing_profile_avatar_form'))
describe 'AvatarUploader', ->
$fixture = null
describe 'on file upload', ->
beforeEach ->
$fixture = $(readFixtures('avatar.html'))
it 'opens modal', ->
@fakeSuccessfulFileUpload($fixture, null, { result: { redirect_to: 'coaches/crop' } })
@uploader = new CoachesApp.AvatarUploader($('#new_editing_profile_avatar_form'))
spyOn(@uploader.modal, 'onModalOpened')
it 'replaces avatar image'
it 'shows flash message'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment