Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/mixins/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ const mixin = (superclass) => class extends superclass {
return model.fetch(options).then(() => model);
}

static fetchOneFromUrlAndOptions({ url, options = {} } = {}) {
const Model = this;

return this.sync({ method: 'get', url, options }).then((resp) => {
return new Model(nullToUndefined(resp.data));
});
}

static sync({ method, url, options }) {
return sync({ method, url, options });
}
Expand Down
72 changes: 72 additions & 0 deletions test/mixins/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,78 @@ describe('Http', () => {
});
});

describe('.fetchOneFromUrlAndOptions', () => {
const defaultValue = 'Mitaine';
let KlassWithAttributes, id, url, data, httpOptions, result;

function configureHttpMock() {
httpMock().onGet(url, httpOptions).reply(() => {
return [200, data];
});
}

beforeEach(async() => {
id = Math.random();
url = `${urlRoot}/${urlResource}/${id}`;
data = {
stuff: Math.random(),
};

KlassWithAttributes = class extends Klass {
buildFields() { return { id: new Attribute(), stuff: new Attribute({ value: defaultValue }) }; }
};
});

context('when data is undefined', () => {
beforeEach(async() => {
configureHttpMock();
data = undefined;
result = await KlassWithAttributes.fetchOneFromUrlAndOptions({ url });
});

it('should return an instance of model class', () => {
expect(result).to.be.instanceOf(KlassWithAttributes);
});

it('should return data with default value', async() => {
expect(result.fields.stuff.value).to.equal(defaultValue);
});
});

context('when data is null', () => {
beforeEach(async() => {
configureHttpMock();
data = undefined;
result = await KlassWithAttributes.fetchOneFromUrlAndOptions({ url });
});

it('should return an instance of model class', () => {
expect(result).to.be.instanceOf(KlassWithAttributes);
});

it('should return data with default value', async() => {
expect(result.fields.stuff.value).to.equal(defaultValue);
});
});

context('with options', () => {
beforeEach(async() => {
httpOptions = { params: { stuff: Math.random() } };
configureHttpMock();

result = await KlassWithAttributes.fetchOneFromUrlAndOptions({ url, options: httpOptions });
});

it('should return an instance of model class', () => {
expect(result).to.be.instanceOf(KlassWithAttributes);
});

it('should retrieve data according to specified url and options', async() => {
expect(result.fields.stuff.value).to.equal(data.stuff);
});
});
});

describe('#buildUrl', () => {
describe('when id is the primary key', () => {
let KlassWithAttributes;
Expand Down