Skip to content

Commit

Permalink
Fix snapshot record for Get requests (#410)
Browse files Browse the repository at this point in the history
* moved fetching store record for fake snapshot to store request class, and fixed it for get requests

* fixing linter
  • Loading branch information
cinkonaap authored and danielspaniel committed Nov 18, 2019
1 parent ea152dc commit 8b272ba
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 47 deletions.
18 changes: 0 additions & 18 deletions addon/mocks/mock-delete-request.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import FactoryGuy from '../factory-guy';
import MockStoreRequest from './mock-store-request';
import MaybeIdUrlMatch from './maybe-id-url-match';

Expand All @@ -14,21 +13,4 @@ export default class MockDeleteRequest extends MaybeIdUrlMatch(MockStoreRequest)
getType() {
return "DELETE";
}

/**
* Create fake snaphot with adapterOptions and record.
*
* Override the parent to find the model in the store if there is
* an id available
*
* @returns {{adapterOptions: (*|Object), record: (*|DS.Model)}}
*/
makeFakeSnapshot() {
let snapshot = super.makeFakeSnapshot();
if (this.id && !this.model) {
snapshot.record = FactoryGuy.store.peekRecord(this.modelName, this.id);
}
return snapshot;
}

}
1 change: 0 additions & 1 deletion addon/mocks/mock-get-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class MockGetRequest extends MockStoreRequest {
this.responseJson = json;
this.setupHandler();
}

}

export default MockGetRequest;
9 changes: 7 additions & 2 deletions addon/mocks/mock-store-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ export default class extends MockRequest {
* @returns {{adapterOptions: (*|Object), record: (*|DS.Model)}}
*/
makeFakeSnapshot() {
return {adapterOptions: this.adapterOptions, record: this.model};
}
let record = this.model;

if (!record && this.get('id')) {
record = FactoryGuy.store.peekRecord(this.modelName, this.get('id'));
}

return {adapterOptions: this.adapterOptions, record};
}
}
16 changes: 0 additions & 16 deletions addon/mocks/mock-update-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,6 @@ export default class MockUpdateRequest extends MaybeIdUrlMatch(AttributeMatcher(
return FactoryGuy.updateHTTPMethod(this.modelName);
}

/**
* Create fake snaphot with adapterOptions and record.
*
* Override the parent to find the model in the store if there is
* an id available
*
* @returns {{adapterOptions: (*|Object), record: (*|DS.Model)}}
*/
makeFakeSnapshot() {
let snapshot = super.makeFakeSnapshot();
if (this.id && !this.model) {
snapshot.record = FactoryGuy.store.peekRecord(this.modelName, this.id);
}
return snapshot;
}

/**
This returns only accepts attrs key
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/mocks/mock-find-record-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ module('MockFindRecord', function(hooks) {
adapter = FactoryGuy.store.adapterFor('user'),
findRecordStub = sinon.stub(adapter, 'urlForFindRecord');

const user = FactoryGuy.store.peekRecord('user', 1);

mock.getUrl();

assert.ok(findRecordStub.calledOnce);
assert.ok(findRecordStub.calledWith(1, 'user', {adapterOptions: options, record: undefined}), 'adapterOptions passed to urlForFindRecord');
assert.ok(findRecordStub.calledWith(1, 'user', {adapterOptions: options, record: user}), 'adapterOptions passed to urlForFindRecord');

adapter.urlForFindRecord.restore();
});
Expand Down
62 changes: 53 additions & 9 deletions tests/unit/mocks/mock-request-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import FactoryGuy, {
build, make, mockFindAll, mockQueryRecord, mockUpdate
} from 'ember-data-factory-guy';
import { inlineSetup } from '../../helpers/utility-methods';
import MockRequest from 'ember-data-factory-guy/mocks/mock-store-request';
import MockStoreRequest from 'ember-data-factory-guy/mocks/mock-store-request';
import sinon from 'sinon';

const serializerType = '-json-api';
Expand All @@ -15,20 +15,20 @@ module('MockRequest', function(hooks) {

module('#fails', function() {
test("status must be 3XX, 4XX or 5XX", function(assert) {
const mock = new MockRequest('user');
const mock = new MockStoreRequest('user');

assert.throws(() => mock.fails({status: 201}));
assert.throws(() => mock.fails({status: 292}));
assert.throws(() => mock.fails({status: 104}));

assert.ok(mock.fails({status: 300}) instanceof MockRequest);
assert.ok(mock.fails({status: 303}) instanceof MockRequest);
assert.ok(mock.fails({status: 401}) instanceof MockRequest);
assert.ok(mock.fails({status: 521}) instanceof MockRequest);
assert.ok(mock.fails({status: 300}) instanceof MockStoreRequest);
assert.ok(mock.fails({status: 303}) instanceof MockStoreRequest);
assert.ok(mock.fails({status: 401}) instanceof MockStoreRequest);
assert.ok(mock.fails({status: 521}) instanceof MockStoreRequest);
});

test("with convertErrors not set, the errors are converted to JSONAPI formatted errors", function(assert) {
const mock = new MockRequest('user');
const mock = new MockStoreRequest('user');
let errors = {errors: {phrase: 'poorly worded'}};

mock.fails({response: errors});
Expand All @@ -44,15 +44,15 @@ module('MockRequest', function(hooks) {
});

test("with convertErrors set to false, does not convert errors", function(assert) {
const mock = new MockRequest('user');
const mock = new MockStoreRequest('user');
let errors = {errors: {phrase: 'poorly worded'}};

mock.fails({response: errors, convertErrors: false});
assert.deepEqual(mock.errorResponse, errors);
});

test("with errors response that will be converted but does not have errors as object key", function(assert) {
const mock = new MockRequest('user');
const mock = new MockStoreRequest('user');
let errors = {phrase: 'poorly worded'};

assert.throws(() => mock.fails({response: errors, convertErrors: true}));
Expand Down Expand Up @@ -138,4 +138,48 @@ module('MockRequest', function(hooks) {
assert.equal(data.get('id'), json2.get('id'), "the destroyed first mock doesn't work");
});
});

module('#makeFakeSnapshot', function() {

test('with model set on request', async function(assert) {
const model = FactoryGuy.make('user');
const mock = new MockStoreRequest('user', 'findRecord');
mock.model = model;

const snapshot = mock.makeFakeSnapshot();

assert.deepEqual(snapshot, { adapterOptions: undefined, record: model });
});

test('without mocked model in the store', async function(assert) {
const json = FactoryGuy.build('user');
const mock = new MockStoreRequest('user', 'findRecord');
mock.returns({ json });

const snapshot = mock.makeFakeSnapshot();

assert.deepEqual(snapshot, { adapterOptions: undefined, record: undefined });
});

test('with mocked model in the store', async function(assert) {
const model = FactoryGuy.make('user');
const mock = new MockStoreRequest('user', 'findRecord');
mock.returns({ model });
mock.id = model.id;

const snapshot = mock.makeFakeSnapshot();

assert.deepEqual(snapshot, { adapterOptions: undefined, record: model });
});

test("without id", async function(assert) {
const model = FactoryGuy.make('user');
const mock = new MockStoreRequest('user', 'findRecord');
mock.returns({ model });

const snapshot = mock.makeFakeSnapshot();

assert.deepEqual(snapshot, { adapterOptions: undefined, record: undefined });
});
});
});

0 comments on commit 8b272ba

Please sign in to comment.