Skip to content

Commit

Permalink
Merge pull request #437 from alexlafroscia/setup-pretender
Browse files Browse the repository at this point in the history
Add `setupPretender` test helper
  • Loading branch information
Turbo87 authored Apr 9, 2020
2 parents 9534196 + 8df6a78 commit ff24733
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"ember-cli-github-pages": "0.2.2",
"ember-cli-htmlbars": "4.3.1",
"ember-cli-inject-live-reload": "2.0.2",
"ember-cli-pretender": "3.1.1",
"ember-cli-pretender": "3.2.0",
"ember-cli-template-lint": "1.0.0-beta.3",
"ember-cli-test-loader": "3.0.0",
"ember-cli-tslint": "0.1.4",
Expand Down
39 changes: 16 additions & 23 deletions tests/acceptance/index-test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
import { click, visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import Pretender from 'pretender';
import { module, test } from 'qunit';
import setupPretender from '../helpers/setup-pretender';

module('Acceptance | index2', hooks => {
setupApplicationTest(hooks);
let server: any;
hooks.beforeEach(() => {
server = new Pretender();
});
hooks.afterEach(() => {
server.shutdown();
});
setupPretender(hooks);

test('visiting /', async assert => {
test('visiting /', async function(assert) {
await visit('/');
assert.expect(8);

server.put('/fruits/:id/doRipen', (request: { url: string; requestBody: string }) => {
this.server.put('/fruits/:id/doRipen', (request) => {
const data = JSON.parse(request.requestBody);
assert.deepEqual(data, { id: '1', name: 'apple' }, 'member action - request payload is correct');
assert.equal(request.url, '/fruits/1/doRipen', 'request was made to "doRipen"');
assert.equal(request.params.id, '1', 'request made to the right URL');
return [200, {}, '{"status": "ok"}'];
});

server.put('/fruits/ripenEverything', (request: { url: string; requestBody: string }) => {
this.server.put('/fruits/ripenEverything', (request) => {
const data = JSON.parse(request.requestBody);
assert.deepEqual(data, { test: 'ok' }, 'collection action - request payload is correct');
assert.ok(true, 'request was made to "ripenEverything"');
return [200, {}, '{"status": "ok"}'];
});

server.get('/fruits/:id/info', (request: { url: string; requestBody: string }) => {
assert.equal(request.url, '/fruits/1/info?fruitId=1');
assert.ok(true, 'request was made to "ripenEverything"');
this.server.get('/fruits/:id/info', (request) => {
assert.equal(request.params.id, '1', 'request made to the right URL');
assert.equal(request.queryParams.fruitId, '1', 'request made with the right query params');
return [200, {}, '{"status": "ok"}'];
});

server.get('/fruits/fresh', (request: { url: string; requestBody: string }) => {
assert.equal(request.url, '/fruits/fresh?month=July');
this.server.get('/fruits/fresh', (request) => {
assert.equal(request.queryParams.month, 'July', 'request made with the right query params');
assert.ok(true, 'request was made to "ripenEverything"');
return [200, {}, '{"status": "ok"}'];
});
Expand All @@ -52,11 +46,11 @@ module('Acceptance | index2', hooks => {
await click('.all-fruit .fresh-type-button');
});

test('before/after hooks and serializeAndPush helper', async assert => {
test('before/after hooks and serializeAndPush helper', async function(assert) {
await visit('/');
assert.expect(7);
assert.expect(6);

server.put('/fruits/:id/doEat', (request: { url: string; requestBody: string }) => {
this.server.put('/fruits/:id/doEat', (request) => {
const data = JSON.parse(request.requestBody);

const expectedData = {
Expand All @@ -70,7 +64,7 @@ module('Acceptance | index2', hooks => {
};

assert.deepEqual(data, expectedData, 'collection action - request payload run through serialize function');
assert.equal(request.url, '/fruits/1/doEat', 'request was made to "doEat"');
assert.equal(request.params.id, '1', 'request was made to the right URL');
const response = {
jsonapi: { version: '1.0' },
data: {
Expand All @@ -84,7 +78,7 @@ module('Acceptance | index2', hooks => {
return [200, {}, JSON.stringify(response)];
});

server.put('/fruits/doEatAll', (request: { url: string; requestBody: string }) => {
this.server.put('/fruits/doEatAll', (request) => {
const data = JSON.parse(request.requestBody);

const expectedData = {
Expand All @@ -98,7 +92,6 @@ module('Acceptance | index2', hooks => {
};

assert.deepEqual(data, expectedData, 'collection action - request payload run through serialize function');
assert.equal(request.url, '/fruits/doEatAll', 'request was made to "doEatAll"');

const response = {
jsonapi: { version: '1.0' },
Expand Down
18 changes: 18 additions & 0 deletions tests/helpers/setup-pretender.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { TestContext } from 'ember-test-helpers';
import Pretender from 'pretender';

export default function setupPretender(hooks: NestedHooks) {
hooks.beforeEach(function(this: TestContext) {
this.server = new Pretender();
});

hooks.afterEach(function(this: TestContext) {
this.server.shutdown();
});
}

declare module 'ember-test-helpers' {
interface TestContext {
server: Pretender;
}
}
34 changes: 12 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1973,11 +1973,6 @@
resolved "https://registry.yarnpkg.com/@types/symlink-or-copy/-/symlink-or-copy-1.2.0.tgz#4151a81b4052c80bc2becbae09f3a9ec010a9c7a"
integrity sha512-Lja2xYuuf2B3knEsga8ShbOdsfNOtzT73GyJmZyY7eGl2+ajOqrs8yM5ze0fsSoYwvA6bw7/Qr7OZ7PEEmYwWg==

"@xg-wang/whatwg-fetch@^3.0.0":
version "3.0.0"
resolved "https://registry.yarnpkg.com/@xg-wang/whatwg-fetch/-/whatwg-fetch-3.0.0.tgz#f7b222c012a238e7d6e89ed3d72a1e0edb58453d"
integrity sha512-ULtqA6L75RLzTNW68IiOja0XYv4Ebc3OGMzfia1xxSEMpD0mk/pMvkQX0vbCFyQmKc5xGp80Ms2WiSlXLh8hbA==

JSONStream@^1.0.4, JSONStream@^1.3.4, JSONStream@^1.3.5:
version "1.3.5"
resolved "https://registry.yarnpkg.com/JSONStream/-/JSONStream-1.3.5.tgz#3208c1f08d3a4d99261ab64f92302bc15e111ca0"
Expand Down Expand Up @@ -5187,17 +5182,18 @@ ember-cli-preprocess-registry@^3.3.0:
debug "^3.0.1"
process-relative-require "^1.0.0"

ember-cli-pretender@3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/ember-cli-pretender/-/ember-cli-pretender-3.1.1.tgz#289c41683de266fec8bfaf5b7b7f6026aaefc8cf"
integrity sha512-RGGj9la0138bgHUxyaGDHCZydmdpW+BFN9v0vMBzNPeXsaexCZotaFTIZDCNcKWPx8jtRHR8AXf318VRGXLJsw==
ember-cli-pretender@3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/ember-cli-pretender/-/ember-cli-pretender-3.2.0.tgz#8d91a20bb5f57092a328317591ec1dd622fcadf1"
integrity sha512-3q7a0bjbHAevWaQQE447McT1neQIMgBjllfbOVX27D1qQqCZ5h6qMsRe7fU44B6/UQcCcghpbNoeEcNcI56wOg==
dependencies:
abortcontroller-polyfill "^1.1.9"
broccoli-funnel "^2.0.1"
broccoli-merge-trees "^3.0.0"
chalk "^2.4.2"
ember-cli-babel "^6.6.0"
fake-xml-http-request "^2.0.0"
pretender "^2.1.0"
pretender "^3.0.1"
route-recognizer "^0.3.3"
whatwg-fetch "^3.0.0"

Expand Down Expand Up @@ -6082,16 +6078,11 @@ extsprintf@^1.2.0:
resolved "https://registry.yarnpkg.com/extsprintf/-/extsprintf-1.4.0.tgz#e2689f8f356fad62cca65a3a91c5df5f9551692f"
integrity sha1-4mifjzVvrWLMplo6kcXfX5VRaS8=

fake-xml-http-request@^2.0.0:
fake-xml-http-request@^2.0.0, fake-xml-http-request@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-2.1.1.tgz#279fdac235840d7a4dff77d98ec44bce9fc690a6"
integrity sha512-Kn2WYYS6cDBS5jq/voOfSGCA0TafOYAUPbEp8mUVpD/DVV5bQIDjlq+MLLvNUokkbTpjBVlLDaM5PnX+PwZMlw==

fake-xml-http-request@~2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/fake-xml-http-request/-/fake-xml-http-request-2.0.1.tgz#e4a7f256af055d8059deb23c9d7ae721d28cf078"
integrity sha512-KzT+G4aLM1Btg25QRGxB6yGLGOVZXXzrH8I4OG3KHwsdoqFclyW3alieqh5NaYGcmbQvNOn/ldGO1rGKf7CNdA==

fast-deep-equal@^3.1.1:
version "3.1.1"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.1.tgz#545145077c501491e33b15ec408c294376e94ae4"
Expand Down Expand Up @@ -10249,13 +10240,12 @@ prepend-http@^2.0.0:
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=

pretender@^2.1.0:
version "2.1.2"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-2.1.2.tgz#02d7c0a3f18cb0ce376dfc4fb0043ca288f50316"
integrity sha512-5Jx7kBalWDn8oEKfw6nAcx2KK4GkDSQXG3WhgaPsDtak6Rv6nTeQjOdvOM9PEvauS+9Ur+DLfZTDWBtqK6lFVA==
pretender@^3.0.1:
version "3.4.0"
resolved "https://registry.yarnpkg.com/pretender/-/pretender-3.4.0.tgz#0ccd943eccff48126bcbb912acaf9c33b0829785"
integrity sha512-BxmCnuVOchuP1am8p038XhSDDrRXX0bNqp6GRJPu/VcnWNFssy/BIHBefMcB535lHfMeelonkIP1ETtHEHjOVQ==
dependencies:
"@xg-wang/whatwg-fetch" "^3.0.0"
fake-xml-http-request "~2.0.0"
fake-xml-http-request "^2.1.1"
route-recognizer "^0.3.3"

printf@^0.5.1:
Expand Down

0 comments on commit ff24733

Please sign in to comment.