From 33a927388012d131d575abf4015d2b22835563f9 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Tue, 5 Nov 2024 18:57:05 -0500 Subject: [PATCH] Update tests --- test-app/tests/acceptance/root-test.js | 140 ------------------------- test-app/tests/unit/error-test.js | 23 ++-- test-app/tests/unit/headers-test.js | 5 +- 3 files changed, 13 insertions(+), 155 deletions(-) delete mode 100644 test-app/tests/acceptance/root-test.js diff --git a/test-app/tests/acceptance/root-test.js b/test-app/tests/acceptance/root-test.js deleted file mode 100644 index a8ee0365..00000000 --- a/test-app/tests/acceptance/root-test.js +++ /dev/null @@ -1,140 +0,0 @@ -import { module, test } from 'qunit'; -import { setupApplicationTest } from 'ember-qunit'; -import { visit, click, find, currentRouteName } from '@ember/test-helpers'; -import Pretender from 'pretender'; -import fetch from 'fetch'; - -var server; - -module('Acceptance: Root', function(hooks) { - setupApplicationTest(hooks); - - hooks.beforeEach(function() { - server = new Pretender(); - }); - - hooks.afterEach(function() { - server.shutdown(); - }); - - test('visiting /', async function(assert) { - server.get('/omg.json', function() { - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ name: 'World' }) - ]; - }); - - await visit('/'); - - assert.equal(currentRouteName(), 'index'); - assert.equal(this.element.querySelector('.fetch').textContent.trim(), 'Hello World! fetch'); - }); - - test('posting a string', function(assert) { - server.post('/upload', function(req) { - assert.equal(req.requestBody, 'foo'); - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ name: 'World' }) - ]; - }); - - return fetch('/upload', { - method: 'post', - body: 'foo' - }).then(function (res) { - assert.equal(res.status, 200); - return res.json(); - }).then(function (data) { - assert.equal(data.name, 'World'); - }); - }); - - test('posting a form', function(assert) { - server.post('/upload', function(req) { - assert.ok(req.requestBody instanceof window.FormData); - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ name: 'World' }) - ]; - }); - let form = new window.FormData(); - form.append('foo', 'bar'); - - return fetch('/upload', { - method: 'post', - body: form - }).then(function (res) { - assert.equal(res.status, 200); - return res.json(); - }).then(function (data) { - assert.equal(data.name, 'World'); - }); - }); - - test('posting an array buffer', function(assert) { - server.post('/upload', function(req) { - assert.ok(req.requestBody instanceof window.ArrayBuffer); - return [ - 200, - { 'Content-Type': 'text/json' }, - JSON.stringify({ name: 'World' }) - ]; - }); - - return fetch('/upload', { - method: 'post', - body: new window.ArrayBuffer() - }).then(function(res) { - assert.equal(res.status, 200); - return res.json(); - }).then(function(data) { - assert.equal(data.name, 'World'); - }); - }); - - test('tests await for fetch requests resolve', async function(assert) { - server.get('/omg.json', function() { - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ name: 'World' }) - ]; - }); - - server.get('/slow-data.json', function() { - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ content: 'This was slow' }) - ]; - }, 800); - - await visit('/'); - - await click('#fetch-slow-data-button'); - - assert.ok(find('.fetched-slow-data-span'), 'Test has waited for data to appear'); - }); - - test('tests await for fetch requests reject', async function(assert) { - server.get('/omg.json', function() { - return [ - 200, - { 'Content-Type': 'text/json'}, - JSON.stringify({ name: 'World' }) - ]; - }); - - await visit('/'); - - server.shutdown(); - await click('#fetch-broken-data-button'); - - assert.ok(find('.fetched-failed-span'), 'The fetch promise has rejected'); - }); -}); diff --git a/test-app/tests/unit/error-test.js b/test-app/tests/unit/error-test.js index 42626fc2..d78d84db 100644 --- a/test-app/tests/unit/error-test.js +++ b/test-app/tests/unit/error-test.js @@ -1,5 +1,4 @@ import { module, test } from 'qunit'; -import { Response } from 'fetch'; import { isUnauthorizedResponse, @@ -10,47 +9,47 @@ import { isBadRequestResponse, isServerErrorResponse, isAbortError, - isConflictResponse + isConflictResponse, } from 'ember-fetch/errors'; -module('Errors', function() { - test('isUnauthorizedResponse', function(assert) { +module('Errors', function () { + test('isUnauthorizedResponse', function (assert) { assert.ok(isUnauthorizedResponse(new Response(null, { status: 401 }))); }); - test('isForbiddenResponse', function(assert) { + test('isForbiddenResponse', function (assert) { assert.ok(isForbiddenResponse(new Response(null, { status: 403 }))); }); - test('isNotFoundResponse', function(assert) { + test('isNotFoundResponse', function (assert) { assert.ok(isNotFoundResponse(new Response(null, { status: 404 }))); assert.notOk(isNotFoundResponse(new Response(null, { status: 400 }))); }); - test('isGoneResponse', function(assert) { + test('isGoneResponse', function (assert) { assert.ok(isGoneResponse(new Response(null, { status: 410 }))); assert.notOk(isGoneResponse(new Response(null, { status: 400 }))); }); - test('isInvalidResponse', function(assert) { + test('isInvalidResponse', function (assert) { assert.ok(isInvalidResponse(new Response(null, { status: 422 }))); }); - test('isBadRequestResponse', function(assert) { + test('isBadRequestResponse', function (assert) { assert.ok(isBadRequestResponse(new Response(null, { status: 400 }))); }); - test('isServerErrorResponse', function(assert) { + test('isServerErrorResponse', function (assert) { assert.notOk(isServerErrorResponse(new Response(null, { status: 499 }))); assert.ok(isServerErrorResponse(new Response(null, { status: 500 }))); assert.ok(isServerErrorResponse(new Response(null, { status: 599 }))); }); - test('isAbortError', function(assert) { + test('isAbortError', function (assert) { assert.ok(isAbortError(new DOMException('AbortError', 'AbortError'))); }); - test('isConflictResponse', function(assert) { + test('isConflictResponse', function (assert) { assert.ok(isConflictResponse(new Response(null, { status: 409 }))); }); }); diff --git a/test-app/tests/unit/headers-test.js b/test-app/tests/unit/headers-test.js index 54bff0d9..8d6b2c90 100644 --- a/test-app/tests/unit/headers-test.js +++ b/test-app/tests/unit/headers-test.js @@ -1,8 +1,7 @@ import { module, test } from 'qunit'; -import { Headers } from 'fetch'; -module('Headers', function() { - test('iterator', function(assert) { +module('Headers', function () { + test('iterator', function (assert) { let headers = new Headers(); assert.ok(headers[Symbol.iterator]);