Skip to content

Commit

Permalink
Merge pull request #338 from AutoCloud/public_pretender
Browse files Browse the repository at this point in the history
getPretender and setPretender functions as public exports
  • Loading branch information
danielspaniel authored Apr 28, 2018
2 parents 35ed52a + 0da7676 commit 165edc3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
6 changes: 4 additions & 2 deletions addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import FactoryGuy, {
import {
mockSetup, mockTeardown,
mockFind, mockFindRecord, mockFindAll, mockReload, mockQuery,
mockQueryRecord, mockCreate, mockUpdate, mockDelete, mock, mockLinks
mockQueryRecord, mockCreate, mockUpdate, mockDelete, mock, mockLinks,
getPretender
} from './mocks/exposed-request-functions';

import manualSetup from './utils/manual-setup';
Expand All @@ -30,7 +31,8 @@ export { make, makeNew, makeList, build, buildList, attributesFor, manualSetup,
export {
mockSetup, mockTeardown,
mockFind, mockFindRecord, mockFindAll, mockReload, mockQuery,
mockQueryRecord, mockCreate, mockUpdate, mockDelete, mock, mockLinks
mockQueryRecord, mockCreate, mockUpdate, mockDelete, mock, mockLinks,
getPretender
};

export function setupFactoryGuy(hooks) {
Expand Down
8 changes: 8 additions & 0 deletions addon/mocks/exposed-request-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MockFindAllRequest from './mock-find-all-request';
import MockDeleteRequest from './mock-delete-request';
import MockAnyRequest from './mock-any-request';
import MockLinksRequest from './mock-links-request';
import RequestManager from './request-manager';

export function mockSetup({responseTime, logLevel = 0} = {}) {
Ember.deprecate(`[ember-data-factory-guy] mockSetup is no longer needed. If you want to set logLevel or responseTime, use FactoryGuy.settings instead. If you don't need to set anything, it is safe to remove mockSetup`,
Expand Down Expand Up @@ -384,3 +385,10 @@ export function mockDelete(...args) {

return new MockDeleteRequest(modelName, id);
}

/**
Returns the Pretender instance used for the mocks.
*/
export function getPretender() {
return RequestManager.getPretender();
}
22 changes: 22 additions & 0 deletions tests/unit/get-pretenter-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
import { mock, manualSetup, getPretender } from 'ember-data-factory-guy';
import Pretender from 'pretender'

module('Unit | getPretender', function(hooks) {
setupTest(hooks);

hooks.beforeEach(function() {
manualSetup(this);
});

test('getPretender basic usage', function(assert) {
assert.expect(3);
let pretender = getPretender();

assert.ok(pretender instanceof Pretender, "getPretender returns an instance of Pretender");
assert.equal(pretender.handlers.length, 0, "the handlers array is initially empty");
mock({url: "/api/whatever"});
assert.equal(pretender.handlers.length, 1, "the created mock is added to the handlers");
});
});

0 comments on commit 165edc3

Please sign in to comment.