Skip to content

Commit

Permalink
Add test for empty bio field and modify mocha configuration (incomplete)
Browse files Browse the repository at this point in the history
- Added a new test file to check the behavior when updating a user's bio to an empty field.
- Altered .mocharc.js configuration to include the new test file for execution.
- The update is still a work in progress and may need further adjustments.
  • Loading branch information
Curious-Goblin committed Nov 23, 2024
1 parent 0c4fcec commit cec3761
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
1 change: 1 addition & 0 deletions apps/meteor/.mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ module.exports = {
'tests/unit/server/**/*.tests.ts',
'tests/unit/server/**/*.spec.ts',
'app/api/**/*.spec.ts',
'apps/meteor/tests/**/*.spec.ts',
],
};
39 changes: 39 additions & 0 deletions apps/meteor/tests/server/methods/users/saveUserProfile.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { expect } from 'chai';
import proxyquire from 'proxyquire';
import sinon from 'sinon';

const stubs = {
Users: {
findOneById: sinon.stub(),
setBio: sinon.stub(),
},
rcSettings: {
get: sinon.stub(),
},
};
const AccountsMock = { setPasswordAsync: sinon.stub() };
const checkMock = sinon.stub();
const MatchMock = { Maybe: sinon.stub() };
const MeteorMock = { call: sinon.stub(), Error: sinon.stub() };

const { saveUserProfile } = proxyquire.noCallThru().load('../../../../server/methods/saveUserProfile', {
'meteor/accounts-base': { 'Accounts': AccountsMock, '@global': true },
'meteor/check': { 'check': checkMock, 'Match': MatchMock, '@global': true },
'@rocket.chat/models': { Users: stubs.Users },
'../../../../app/settings/server': { settings: stubs.rcSettings },
'meteor/meteor': { 'Meteor': MeteorMock, '@global': true },
});

describe('Users - saveUserProfile (Bio Update)', () => {
beforeEach(() => {
sinon.restore();
stubs.rcSettings.get.withArgs('Accounts_AllowUserProfileChange').returns(true);
});

it('should update the bio to an empty string and call setBio', async () => {
stubs.Users.findOneById.resolves({ _id: 'userId', bio: 'Old bio text' });
stubs.Users.setBio.resolves(true);
await saveUserProfile.call({ userId: 'userId' }, { bio: '' }, {});
expect(stubs.Users.setBio.calledWith('userId', '')).to.be.true;
});
});
14 changes: 0 additions & 14 deletions apps/meteor/tests/unit/server/users/saveUserIdentity.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const stubs = {
updateUserReferences: sinon.stub(),
setUsername: sinon.stub(),
setRealName: sinon.stub(),
setBio: sinon.stub(),
validateName: sinon.stub(),
FileUpload: sinon.stub(),
};
Expand Down Expand Up @@ -132,17 +131,4 @@ describe('Users - saveUserIdentity', () => {
expect(stubs.updateUserReferences.called).to.be.true;
expect(result).to.be.true;
});

it('should update bio to an empty string and call setBio', async () => {
stubs.findOneUserById.returns({ bio: 'Some bio text', username: 'oldUsername' });
stubs.setRealName.returns(true);
stubs.setUsername.returns(true);
stubs.setBio = sinon.stub();
const result = await saveUserIdentity({ _id: 'valid_id', bio: '' });
expect(stubs.setRealName.called).to.be.false;
expect(stubs.setUsername.called).to.be.false;
expect(stubs.updateUsernameOfEditByUserId.called).to.be.false;
expect(stubs.setBio.calledWith('valid_id', '')).to.be.true;
expect(result).to.be.true;
});
});

0 comments on commit cec3761

Please sign in to comment.