Skip to content
30 changes: 30 additions & 0 deletions spec/ParseRole.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,4 +601,34 @@ describe('Parse Role testing', () => {
});
});
});

it('should trigger afterSave hook when using Parse.Role class reference', done => {
let afterSaveCalled = false;

Parse.Cloud.afterSave(Parse.Role, req => {
afterSaveCalled = true;
expect(req.object).toBeDefined();
expect(req.object.get('name')).toBe('AnotherTestRole');
});

const acl = new Parse.ACL();
acl.setPublicReadAccess(true);
const role = new Parse.Role('AnotherTestRole', acl);

role
.save({}, { useMasterKey: true })
.then(savedRole => {
expect(savedRole.id).toBeDefined();
// Give the afterSave hook some time to execute
return new Promise(resolve => setTimeout(resolve, 100));
})
.then(() => {
expect(afterSaveCalled).toBe(true);
done();
})
.catch(err => {
fail(`Should not have failed: ${err.message}`);
done();
});
});
});
6 changes: 6 additions & 0 deletions src/RestWrite.js
Original file line number Diff line number Diff line change
Expand Up @@ -1743,6 +1743,12 @@ RestWrite.prototype.buildParseObjects = function () {
const readOnlyAttributes = className.constructor.readOnlyAttributes
? className.constructor.readOnlyAttributes()
: [];
// For _Role class, the 'name' field is read-only after the object has been saved
// Since _handleSaveResponse is called after buildParseObjects and sets the objectId,
// we need to exclude 'name' from being set to avoid "A role's name can only be set before it has been saved" error
if (this.className === '_Role' && !readOnlyAttributes.includes('name')) {
readOnlyAttributes.push('name');
}
if (!this.originalData) {
for (const attribute of readOnlyAttributes) {
extraData[attribute] = this.data[attribute];
Expand Down
Loading