fix: 🐛 handle empty member_ids in group responses

This commit is contained in:
Randall Morey committed 2020-09-12 11:47:27 -04:00
1 parent bcd46cec1f
commit 156dc08ff5
3 files changed
+36 -2

No files matched your search

+5 -1
View File
@@ -11,7 +11,11 @@ export default class GroupModel extends GeneratedGroupModel {
* Members is read-only under normal circumstances. But members can
* be persisted via calls to `addMembers()` or `removeMembers()`.
*/
@attr({readOnly: true, defaultValue: () => A()}) member_ids;
@attr({
readOnly: true,
defaultValue: () => A(),
emptyArrayIfMissing: true
}) member_ids;
/**
* An array of resolved user instances.
@@ -45,4 +45,34 @@ module('Unit | Serializer | group', function(hooks) {
});
});
test('it normalizes missing member_ids to empty array', function (assert) {
assert.expect(1);
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('group');
const group = store.createRecord('group').constructor;
const payload = {
id: '1',
name: 'Group 1',
scope: { id: 'o_123' }
};
const normalized = serializer.normalizeSingleResponse(
store,
group,
payload
);
assert.deepEqual(normalized, {
included: [],
data: {
id: '1',
type: 'group',
attributes: {
name: 'Group 1',
scope: { scope_id: 'o_123' },
member_ids: []
},
relationships: {},
},
});
});
});
+1 -1
View File
@@ -12,7 +12,7 @@ export default ApplicationSerializer.extend({
_hashForModel(model) {
const json = ApplicationSerializer.prototype._hashForModel.apply(this, arguments);
json.member_ids = model.memberIds;
if (model.memberIds.length) json.member_ids = model.memberIds;
return json;
}