Skip to content

Commit

Permalink
chore: add setupPretender test helper
Browse files Browse the repository at this point in the history
* Add `setupPretender` helper that provides Pretender to the test context (with correct types!)
* Update tests so that there are no TypeScript errors in Pretender handlers
  • Loading branch information
alexlafroscia committed Apr 9, 2020
1 parent 9cd93ae commit 8df6a78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
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;
}
}

0 comments on commit 8df6a78

Please sign in to comment.