Skip to content

Commit

Permalink
Merge pull request #25 from itsfadnis/extract_collection_metadata
Browse files Browse the repository at this point in the history
Extract links & meta from a collection
  • Loading branch information
itsfadnis committed Oct 18, 2018
2 parents 6f6f555 + 7132da0 commit e0ae8be
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
9 changes: 8 additions & 1 deletion src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,14 @@ class Base {
.deserialize(response)
.then((data) => {
if (Array.isArray(data)) {
return data.map(object => new this(object));
const collection = data.map(object => new this(object));
if (data.links) {
collection.links = data.links;
}
if (data.meta) {
collection.meta = data.meta;
}
return collection;
}
return new this(data);
});
Expand Down
42 changes: 35 additions & 7 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,15 @@ describe('Model', () => {
});

describe('.deserialize(response)', () => {
test('it deserializes the response', () => {
class User extends Model {
constructor(args) {
super(args);
this.firstName = args.firstName;
this.lastName = args.lastName;
}
class User extends Model {
constructor(args) {
super(args);
this.firstName = args.firstName;
this.lastName = args.lastName;
}
}

test('it deserializes the response', () => {
return User.deserialize({
data: {
type: 'users',
Expand All @@ -837,6 +837,34 @@ describe('Model', () => {
expect(Deserializer).toHaveBeenCalledWith(User.deserializerOptions);
});
});

test('it extracts links & meta for a collection', () => {
const deserializedArray = [{
id: '123',
firstName: 'John',
lastName: 'Doe'
}, {
id: '456',
firstName: 'Jane',
lastName: 'Doe'
}];
deserializedArray.links = {
all: '/users'
};
deserializedArray.meta = {
count: 2
};
const deserializeSpy = jest
.spyOn(Deserializer.prototype, 'deserialize')
.mockResolvedValue(deserializedArray);
return User.deserialize('foo').then((array) => {
expect(array[0]).toEqual(new User({ id: '123', firstName: 'John', lastName: 'Doe' }));
expect(array[1]).toEqual(new User({ id: '456', firstName: 'Jane', lastName: 'Doe' }));
expect(array.links).toEqual({ all: '/users' });
expect(array.meta).toEqual({ count: 2 });
deserializeSpy.mockRestore();
});
});
});

describe('.keysForAttributes()', () => {
Expand Down
8 changes: 6 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,7 @@ json5@^0.5.0, json5@^0.5.1:

"jsonapi-serializer@git+https://github.com/itsfadnis/jsonapi-serializer.git":
version "3.5.6"
resolved "git+https://github.com/itsfadnis/jsonapi-serializer.git#86383eba1bd0e47521982fb3c2fcb72576c85590"
resolved "git+https://github.com/itsfadnis/jsonapi-serializer.git#58bd69a1b8cb58ca255591910fc970ee9de2631f"
dependencies:
inflected "^1.1.6"
lodash "^4.16.3"
Expand Down Expand Up @@ -2834,10 +2834,14 @@ lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"

lodash@^4.13.1, lodash@^4.14.0, lodash@^4.16.3, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.2.0, lodash@^4.3.0:
version "4.17.5"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511"

lodash@^4.16.3:
version "4.17.11"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.11.tgz#b39ea6229ef607ecd89e2c8df12536891cac9b8d"

longest@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
Expand Down

0 comments on commit e0ae8be

Please sign in to comment.