From 932000ea90381c90d2c43aa53278d2bf9866f014 Mon Sep 17 00:00:00 2001 From: garlicnation Date: Wed, 5 Nov 2014 15:09:30 -0800 Subject: [PATCH] Bring test suite to a minimal level of coverage. --- test/core-ajax.html | 204 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 191 insertions(+), 13 deletions(-) diff --git a/test/core-ajax.html b/test/core-ajax.html index 0081cb8..41bad64 100644 --- a/test/core-ajax.html +++ b/test/core-ajax.html @@ -26,9 +26,6 @@ suiteSetup(function() { xhr = sinon.useFakeXMLHttpRequest(); ajax = document.querySelector("core-ajax"); - }); - setup(function() { - requests = []; xhr.onCreate = function (xhr) { requests.push(xhr); }; @@ -39,6 +36,9 @@ ajax.handleAs = 'text'; ajax.body = ''; }); + setup(function() { + requests = []; + }); suite('handleAs', function() { suite('text', function(){ var headers = { @@ -66,7 +66,7 @@ }); test('Raw text should pass through', function(){ assert.equal(ajax.response, "test text") - }) + }); }); suite('xml', function(){ var headers = { @@ -99,10 +99,11 @@ ], done); }); test('XML should be returned with queryable structure', function(){ + debugger; var q = ajax.response.querySelector("note body q"); - assert.equal(q.innerHTML, "Feed me!"); + assert.equal(q.childNodes[0].textContent, "Feed me!"); var to = ajax.response.querySelector("to"); - assert.equal(to.innerHTML, "AJ"); + assert.equal(to.childNodes[0].textContent, "AJ"); })}); suite('json', function(){ var headers = { @@ -135,19 +136,196 @@ assert.equal(r.object.list[2].key, "value"); }); }); - suite('arraybuffer', function(){}); + suite('arraybuffer', function(){ + var headers = { + "Content-Type": "text/plain" + }; + setup(function(done){ + async.series([ + function(cb){ + ajax.handleAs = 'arraybuffer'; + ajax.url = "http://example.com/data" + ajax.auto = true; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + }, + function(cb){ + var buf = new ArrayBuffer(8*4); + var resp = new Int32Array(buf); + resp[3] = 12; + resp[6] = 21; + requests[0].response = buf; + requests[0].respond(200, headers, 'blahblahblah'); + cb(); + } + ], done); + }); + test('arraybuffer response should be passed through', function(){ + var r = ajax.response; + var ints = new Int32Array(r); + assert.equal(ints[3], 12); + assert.equal(ints[6], 21); + }); + }); suite('blob', function(){}); suite('document', function(){}); }); suite('auto', function() { - test('url', function(){}); - test('params', function(){}); - test('body', function(){}); + suiteSetup(function(){ + ajax.url = "http://example.com/" + ajax.auto = true; + }); + test('url change should trigger request', function(done){ + async.series([ + function(cb){ + ajax.url = "http://example.com/auto"; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + }, + function(cb){ + assert.equal(requests.length, 1); + cb(); + } + ], done); + }); + test('params change should trigger request', function(done){ + async.series([ + function(cb){ + ajax.params = {param: "value"}; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + }, + function(cb){ + assert.equal(requests.length, 1); + cb(); + } + ], done); + }); + test('body change should trigger request', function(done){ + async.series([ + function(cb){ + ajax.method = "POST"; + ajax.body = "bodystuff"; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + }, + function(cb){ + assert.equal(requests.length, 1); + cb(); + } + ], done); + }); }); suite('events', function(){ - suite('core-response', function(){}); - suite('core-error', function(){}); - suite('core-complete', function(){}); + var headers = { + "Content-Type": "text/plain" + }; + var body = "somebodytext"; + var responded; + setup(function(done){ + async.series([ + function(cb){ + ajax.auto = false; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + }, + function(cb){; + ajax.handleAs = 'text'; + ajax.url = "http://example.com/text" + ajax.auto = true; + cb(); + }, + flush, + function(cb) { + requestAnimationFrame(function(){ + cb(); + }); + } + ], done); + responded = false; + }); + suite('core-response', function(){ + test('core-response should be fired on success', function(done){ + window.addEventListener('core-response', function(response, xhr){ + responded = true; + }); + requests[0].respond(200, headers, body); + assert.isTrue(responded); + done(); + }); + test('core-response should not be fired on failure', function(done){ + window.addEventListener('core-response', function(response, xhr){ + responded = true; + }); + requests[0].respond(404, headers, body); + assert.isFalse(responded); + done(); + }); + }); + suite('core-error', function(){ + test('core-error should be fired on failure', function(done){ + window.addEventListener('core-error', function(response, xhr){ + responded = true; + }); + requests[0].respond(404, headers, body); + assert.isTrue(responded); + done(); + }); + test('core-error should not be fired on success', function(done){ + var responded = false; + window.addEventListener('core-error', function(response, xhr){ + responded = true; + }); + requests[0].respond(200, headers, body); + assert.isFalse(responded); + done(); + }); + }); + suite('core-complete', function(){ + test('core-complete should be fired on success', function(done){ + window.addEventListener('core-complete', function(response, xhr){ + responded = true; + done(); + }); + requests[0].respond(200, headers, body); + assert.isTrue(responded); + done(); + }); + test('core-complete should be fired on failure', function(done){ + var responded = false; + window.addEventListener('core-complete', function(response, xhr){ + responded = true; + }); + requests[0].respond(404, headers, body); + assert.isTrue(responded); + done(); + }); + }); }); });