Skip to content

Instantly share code, notes, and snippets.

@fivetanley
Created August 10, 2012 06:04
Show Gist options
  • Save fivetanley/3311581 to your computer and use it in GitHub Desktop.
Save fivetanley/3311581 to your computer and use it in GitHub Desktop.
EVEN MORE SINON FUN
define( function( require ) {
var EntryListingView = require( 'views/EntryListingView' )
, $ = require( 'jquery' )
describe('EntryListingView', function() {
var entryListingView
, id = 'entry'
, entry
, stub
, entryView
, domElement
beforeEach(function() {
entry = new Backbone.Model()
stub = sinon.stub( entry, 'get' )
.withArgs( 'id' )
.returns( id )
entryView = new EntryListingView({ model: entry } )
entryView.render()
setFixtures( $( '</div>' ) )
domElement = entryView.$el
appendSetFixtures( domElement )
})
describe('rendering', function() {
it('should ask its model for information', function() {
expect( stub ).toHaveBeenCalledOnce()
})
it('should render the id', function() {
expect( domElement ).toHaveText( id )
})
})
describe('toggling visibility', function() {
it('should be able to be turned invisible', function() {
entryView.hide()
expect( domElement ).not.toBeVisible()
})
it('should be able to be turned visible', function() {
entryView.hide().show()
expect( domElement ).toBeVisible()
})
describe('toggling visibility with a query', function() {
it('will hide if the model\'s id matches the query', function() {
entryView.hide( id.substring( 0, id.length - 2 ) )
expect( domElement ).not.toBeVisible()
})
it('will not hide if the models id does not match query', function() {
entryView.hide( 'totallyNeverGonnaMatch' )
expect( domElement ).toBeVisible()
})
it('will show if models id matches the query', function() {
entryView.hide()
entryView.show( id.substring( 0, id.length -2 ) )
expect( domElement ).toBeVisible()
})
it('will not show if models id does not match query', function() {
entryView.hide()
entryView.show( 'totallyNeverGonnaMatch' )
expect( domElement ).not.toBeVisible()
})
})
})
describe('highlighting', function() {
it('changes its background color when told', function() {
expect( domElement.hasClass( 'highlight' ) ).toBe( false )
entryView.highlight()
var hasHighlightClass = domElement.hasClass( 'highlight' )
expect( hasHighlightClass ).toBe( true )
})
it('will be un-highlighted when told', function() {
entryView.highlight()
entryView.unhighlight()
var hasHighlightClass = domElement.hasClass( 'highlight' )
expect( hasHighlightClass ).toBe( false )
})
})
describe('handling click events', function() {
it('highlights when its click event fires', function() {
var stubbery = sinon.stub( EntryListingView.prototype , 'highlight' )
entryView = new EntryListingView( { model: entry })
entryView.trigger( 'click' )
expect( stubbery ).toHaveBeenCalled()
})
it('triggers a click event when its DOM element is clicked', function() {
var spy = sinon.spy()
entryView.on( 'click', spy )
domElement.click()
expect( spy ).toHaveBeenCalledOnce()
})
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment