Skip to content

Instantly share code, notes, and snippets.

@hakunin
Created August 30, 2012 16:01
Show Gist options
  • Select an option

  • Save hakunin/3531714 to your computer and use it in GitHub Desktop.

Select an option

Save hakunin/3531714 to your computer and use it in GitHub Desktop.
ImageSwapping TDD session
class Swapper
constructor: (root) ->
@root = $(root)
swapTo: (data_suffix) ->
self = @
@each ->
self.swap(this, data_suffix)
each: (callback) ->
throw new Error('Implement me!')
swap: (element, data_suffix) ->
throw new Error('Implement me!')
class ImageSwapper extends Swapper
each: (callback) ->
@root.find('img').each(callback)
swap: (element, data_suffix) ->
if newres = element.getAttribute("data-#{data_suffix}")
element.src = newres
describe "Imageswapper swapping", ->
root = null
beforeEach ->
root = document.createElement 'div'
it "should swap by given data attribute", ->
image = $('
<img src="NORMALRES"
data-medium="MEDRES"
data-large="LARGERES" />
')
$(root).append(image)
swapper = new ImageSwapper(root)
swapper.swapTo 'medium'
eq image.attr('src'), 'MEDRES'
swapper.swapTo 'large'
eq image.attr('src'), 'LARGERES'
it "should not swap image to another resolution when correspoding data attribute is not there", ->
image = $('<img src="NORMAL" />')
$(root).append(image)
swapper = new ImageSwapper(root)
swapper.swapTo 'low'
eq image.attr('src'), 'NORMAL'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment