Skip to content

Instantly share code, notes, and snippets.

@pixelhandler
Forked from anonymous/index.html
Created April 10, 2014 04:24
Show Gist options
  • Save pixelhandler/10342403 to your computer and use it in GitHub Desktop.
Save pixelhandler/10342403 to your computer and use it in GitHub Desktop.
Mocking AJAX with ember-testing-httpRespond; attempt to create a simple jsbin using ember-testing-httpRespond; however not working and breaks jsbin. See: http://emberjs.jsbin.com/cexiy/1/edit Comment in PR here: https://github.com/emberjs/website/pull/1401/#issuecomment-39868759
<!DOCTYPE html>
<html>
<head>
<title>Posts - Mocking AJAX with ember-testing-httpRespond</title>
<meta name="description" content="Using All Helpers" />
<meta charset="utf-8">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/normalize/2.1.0/normalize.css">
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://builds.handlebarsjs.com.s3.amazonaws.com/handlebars-v1.3.0.js"></script>
<script src="http://builds.emberjs.com/beta/ember.js"></script>
<script src="http://builds.emberjs.com/beta/ember-data.js"></script>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="https://rawgithub.com/trek/FakeXMLHttpRequest/master/fake_xml_http_request.js"></script>
<script src="https://rawgithub.com/trek/fakehr/master/fakehr.js"></script>
<script src="https://rawgithub.com/trek/ember-testing-httpRespond/master/httpRespond-1.1.js"></script>
<!-- script type="text/javascript" charset="utf-8">
window.httpRespond = function (verb, url, body, status) {
if(typeof body !== 'string'){ body = JSON.stringify(body); }
var found = fakehr.match(verb.toUpperCase(), url)
if (found){
Ember.run(function() {
found.respond(status || 200, {'content-type': 'application/json'}, body);
});
} else {
throw new Ember.Error("No request intercepted for " + verb.toUpperCase() + " " + url + ". Intercepted requests were: " + fakehr.requests.map(function(r){ return r.method + " " + r.url}).join(", "));
}
};
</script -->
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<div id="ember-testing"></div>
<script type="text/x-handlebars">
<h2>My Blog</h2>
{{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="posts">
<ul class="posts">
{{#each post in model}}
<li>{{post.title}}</li>
{{/each}}
</ul>
{{outlet}}
</script>
<script src="./script.js"></script>
</body>
</html>
/*=================== APP ====================*/
App = Ember.Application.create();
App.ApplicationAdapter = DS.RestAdapter;
App.Post = DS.Model.extend({
title: DS.attr()
});
App.Router.map(function() {
this.resource('posts', { path: '/posts' }, function() {
this.route('show', { path: '/:id' });
});
});
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post').then(
function(posts) {
debugger;
},
function(error) {
debugger;
}
);
}
});
/*=================== TESTS ====================*/
App.setupForTesting();
App.injectTestHelpers();
App.rootElement = '#ember-testing';
module("Posts, Mocking AJAX with ember-testing-httpRespond", {
setup: function(){
fakehr.start();
},
teardown: function(){
fakehr.reset();
}
});
test('a', function() {
expect(3);
//Ember.run.later(function() {
//httpRespond('get', '/posts', mockPosts, 200);
//}, 150);
visit('/posts')
.httpRespond("get", "/posts", mockPosts, 200)
.then(function() {
equal(find('ul.posts li'), 3, 'There are three posts listed');
equal(find('ul.posts li:first').text(), mockPosts[0].title,
'First post title is "' + mockPosts[0].title + '"');
equal(find('ul.posts li:last').text(), mockPosts[2].title,
'Last post title is "' + mockPosts[2].title + '"');
});
});
var mockPosts = [
{ id: '0', title: 'Testing Ember Applications' },
{ id: '1', title: 'Ember Authentication' },
{ id: '2', title: 'Contributing to Ember' }
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment