Skip to content

Commit

Permalink
Merge branch 'master' of github.com:danielspaniel/ember-data-factory-guy
Browse files Browse the repository at this point in the history
  • Loading branch information
danielspaniel committed Apr 29, 2018
2 parents b4cb056 + 165edc3 commit e34e692
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
7 changes: 4 additions & 3 deletions addon/factory-guy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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);
Expand All @@ -405,7 +406,7 @@ class FactoryGuy {

delete fixture.id;

return Ember.run(() => this.store.createRecord(modelName, fixture));
return join(() => this.store.createRecord(modelName, fixture));
}

/**
Expand Down Expand Up @@ -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
);
Expand Down
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 e34e692

Please sign in to comment.