diff --git a/addon/factory-guy.js b/addon/factory-guy.js index 4759de5d..ca86b7b0 100644 --- a/addon/factory-guy.js +++ b/addon/factory-guy.js @@ -5,6 +5,7 @@ import FixtureBuilderFactory from './builder/fixture-builder-factory'; import RequestManager from './mocks/request-manager'; import require from 'require'; import { assign } from '@ember/polyfills'; +import { join } from '@ember/runloop'; let modelDefinitions = {}; @@ -380,7 +381,7 @@ class FactoryGuy { } let data = this.fixtureBuilder(modelName).convertForMake(modelName, fixture), - model = Ember.run(() => this.store.push(data)); + model = join(() => this.store.push(data)); if (definition.hasAfterMake()) { definition.applyAfterMake(model, args.opts); @@ -405,7 +406,7 @@ class FactoryGuy { delete fixture.id; - return Ember.run(() => this.store.createRecord(modelName, fixture)); + return join(() => this.store.createRecord(modelName, fixture)); } /** @@ -452,7 +453,7 @@ class FactoryGuy { ensureNameInArguments(method, args) { Ember.assert( - `[ember-data-factory-guy] ${method} needs at least a name + `[ember-data-factory-guy] ${method} needs at least a name ( of model or named factory definition )`, args.length > 0 ); diff --git a/addon/index.js b/addon/index.js index c16ba1e5..a096474d 100644 --- a/addon/index.js +++ b/addon/index.js @@ -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'; @@ -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) { diff --git a/addon/mocks/exposed-request-functions.js b/addon/mocks/exposed-request-functions.js index 963e8cb5..ac61b021 100644 --- a/addon/mocks/exposed-request-functions.js +++ b/addon/mocks/exposed-request-functions.js @@ -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`, @@ -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(); +} diff --git a/tests/unit/get-pretenter-test.js b/tests/unit/get-pretenter-test.js new file mode 100644 index 00000000..2d6a8883 --- /dev/null +++ b/tests/unit/get-pretenter-test.js @@ -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"); + }); +});