Created
August 22, 2014 13:10
-
-
Save beames/7f967499ca48f6470b40 to your computer and use it in GitHub Desktop.
test-suite mocha-ember-Pagenation // source http://jsbin.com/tacah/8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="mocha-ember-Pagenation" /> | |
<link rel="stylesheet" href="http://cdn.staticfile.org/mocha/1.20.1/mocha.css"> | |
<script src="http://cdn.staticfile.org/should.js/should.js/3.1.4/should.min.js"></script> | |
<script src="http://cdn.staticfile.org/mocha/1.20.1/mocha.js"></script> | |
<script src="http://cdn.staticfile.org/superagent/0.15.7/superagent.min.js"></script> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> | |
<script src="http://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.4/handlebars.js"></script> | |
<link href="http://twitter.github.io/bootstrap/assets/css/bootstrap.css" rel="stylesheet" type="text/css" /> | |
<script src="http://builds.emberjs.com.s3.amazonaws.com/ember-1.0.0-rc.6.js"></script> | |
<style> body { padding: 30px; }</style> | |
<meta charset=utf-8 /> | |
<title>test-suite</title> | |
</head> | |
<body> | |
<script type="text/x-handlebars" data-template-name="application"> | |
<h3>Pagination w/Ember.js</h3> | |
<p>Made by <a href="http://hawkins.io">Adam Hawkins</a></p> | |
{{outlet}} | |
<div id="mocha"></div> | |
</script> | |
<script type="text/x-handlebars" data-template-name="posts"> | |
<h3>Total Pages: {{totalPages}}</h3> | |
{{#each paginatedContent}} | |
<p>{{!title}}</p> | |
{{/each}} | |
{{#if hasPages}} | |
<p><a class='click-me' href="#" {{action toggleOrder}}>Toggle Order</a></p> | |
<div class="pagination"> | |
<ul> | |
{{#if prevPage}} | |
<li><a href="#" {{action "selectPage" prevPage}}>«</a></li> | |
{{else}} | |
<li class="disabled"><a>«</a></li> | |
{{/if}} | |
{{#each pages itemController="page"}} | |
<li {{bindAttr class="active disabled"}}><a href="#" {{action "selectPage" number}}>{{number}}</a></li> | |
{{/each}} | |
{{#if nextPage}} | |
<li><a href="#" {{action "selectPage" nextPage}}>»</a></li> | |
{{else}} | |
<li class="disabled"><a>»</a></li> | |
{{/if}} | |
</ul> | |
</div> | |
{{/if}} | |
</script> | |
<script id="test"> | |
onload = setTimeout(function(){ | |
mocha.checkLeaks(); | |
mocha.globals(['test-suite']); | |
var runner = mocha.run(); | |
},10); | |
</script> | |
<script id="jsbin-javascript"> | |
window.App = Ember.Application.create(); | |
App.PostsController = Ember.ArrayController.extend({ | |
sortProperties: ['id'], | |
sortAscending: false, | |
page: 1, | |
perPage: 10, | |
totalPages: (function() { | |
return Math.ceil(this.get('length') / this.get('perPage')); | |
}).property('length', 'perPage'), | |
pages: (function() { | |
var collection = Ember.A(); | |
for(var i = 0; i < this.get('totalPages'); i++) { | |
collection.pushObject(Ember.Object.create({ | |
number: i + 1 | |
})); | |
} | |
return collection; | |
}).property('totalPages'), | |
hasPages: (function() { | |
return this.get('totalPages') > 1; | |
}).property('totalPages'), | |
prevPage: (function() { | |
var page = this.get('page'); | |
var totalPages = this.get('totalPages'); | |
if(page > 1 && totalPages > 1) { | |
return page - 1; | |
} else { | |
return null; | |
} | |
}).property('page', 'totalPages'), | |
nextPage: (function() { | |
var page = this.get('page'); | |
var totalPages = this.get('totalPages'); | |
if(page < totalPages && totalPages > 1) { | |
return page + 1; | |
} else { | |
return null; | |
} | |
}).property('page', 'totalPages'), | |
paginatedContent: (function() { | |
var start = (this.get('page') - 1) * this.get('perPage'); | |
var end = start + this.get('perPage'); | |
return this.get('arrangedContent').slice(start, end); | |
}).property('page', 'totalPages', 'arrangedContent.[]'), | |
selectPage: function(number) { | |
this.set('page', number); | |
}, | |
toggleOrder: function() { | |
this.toggleProperty('sortAscending'); | |
} | |
}); | |
App.PageController = Ember.ObjectController.extend({ | |
currentPage: Ember.computed.alias('parentController.page'), | |
active: (function() { | |
return this.get('number') === this.get('currentPage'); | |
}).property('number', 'currentPage') | |
}); | |
App.Router.map(function() { | |
this.route('posts'); | |
}); | |
App.IndexRoute = Ember.Route.extend({ | |
redirect: function() { | |
this.transitionTo('posts'); | |
} | |
}); | |
App.PostsRoute = Ember.Route.extend({ | |
model: function() { | |
var collection = Ember.A(); | |
for(var i = 0; i < 45; i++) { | |
collection.pushObject(Ember.Object.create({ | |
title: "Post " + (i + 1), | |
id: i | |
})); | |
} | |
return collection; | |
} | |
}); | |
// /*=================== TESTS ====================*/ | |
mocha.setup('bdd'); | |
App.rootElement = 'html'; | |
App.injectTestHelpers(); | |
describe('Toggle Order Button', function () { | |
it('should clickable', function () { | |
visit('/'); | |
click('a.click-me'); | |
}); | |
}); | |
describe('Page Title', function () { | |
it('should equal test-suite', function () { | |
visit('/'); | |
find('title').text().should.equal('test-suite'); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.App = Ember.Application.create(); | |
App.PostsController = Ember.ArrayController.extend({ | |
sortProperties: ['id'], | |
sortAscending: false, | |
page: 1, | |
perPage: 10, | |
totalPages: (function() { | |
return Math.ceil(this.get('length') / this.get('perPage')); | |
}).property('length', 'perPage'), | |
pages: (function() { | |
var collection = Ember.A(); | |
for(var i = 0; i < this.get('totalPages'); i++) { | |
collection.pushObject(Ember.Object.create({ | |
number: i + 1 | |
})); | |
} | |
return collection; | |
}).property('totalPages'), | |
hasPages: (function() { | |
return this.get('totalPages') > 1; | |
}).property('totalPages'), | |
prevPage: (function() { | |
var page = this.get('page'); | |
var totalPages = this.get('totalPages'); | |
if(page > 1 && totalPages > 1) { | |
return page - 1; | |
} else { | |
return null; | |
} | |
}).property('page', 'totalPages'), | |
nextPage: (function() { | |
var page = this.get('page'); | |
var totalPages = this.get('totalPages'); | |
if(page < totalPages && totalPages > 1) { | |
return page + 1; | |
} else { | |
return null; | |
} | |
}).property('page', 'totalPages'), | |
paginatedContent: (function() { | |
var start = (this.get('page') - 1) * this.get('perPage'); | |
var end = start + this.get('perPage'); | |
return this.get('arrangedContent').slice(start, end); | |
}).property('page', 'totalPages', 'arrangedContent.[]'), | |
selectPage: function(number) { | |
this.set('page', number); | |
}, | |
toggleOrder: function() { | |
this.toggleProperty('sortAscending'); | |
} | |
}); | |
App.PageController = Ember.ObjectController.extend({ | |
currentPage: Ember.computed.alias('parentController.page'), | |
active: (function() { | |
return this.get('number') === this.get('currentPage'); | |
}).property('number', 'currentPage') | |
}); | |
App.Router.map(function() { | |
this.route('posts'); | |
}); | |
App.IndexRoute = Ember.Route.extend({ | |
redirect: function() { | |
this.transitionTo('posts'); | |
} | |
}); | |
App.PostsRoute = Ember.Route.extend({ | |
model: function() { | |
var collection = Ember.A(); | |
for(var i = 0; i < 45; i++) { | |
collection.pushObject(Ember.Object.create({ | |
title: "Post " + (i + 1), | |
id: i | |
})); | |
} | |
return collection; | |
} | |
}); | |
// /*=================== TESTS ====================*/ | |
mocha.setup('bdd'); | |
App.rootElement = 'html'; | |
App.injectTestHelpers(); | |
describe('Toggle Order Button', function () { | |
it('should clickable', function () { | |
visit('/'); | |
click('a.click-me'); | |
}); | |
}); | |
describe('Page Title', function () { | |
it('should equal test-suite', function () { | |
visit('/'); | |
find('title').text().should.equal('test-suite'); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment