fix: combine sanitizeDisplayNameOrToast and displayNameIsValid into one function

pull/3281/head
yougotwill 3 months ago
parent dbb455e278
commit ff85660aeb

@ -1,37 +1,12 @@
import { Dispatch } from '@reduxjs/toolkit'; import { EmptyDisplayNameError } from '../../../session/utils/errors';
import { sanitizeSessionUsername } from '../../../session/utils/String'; import { sanitizeSessionUsername } from '../../../session/utils/String';
export function sanitizeDisplayNameOrToast( export function sanitizeDisplayNameOrToast(displayName: string) {
displayName: string, const sanitizedName = sanitizeSessionUsername(displayName).trim();
// can be a useState or redux function
onDisplayNameError: (error: string | undefined) => any, if (!sanitizedName) {
dispatch?: Dispatch throw new EmptyDisplayNameError();
) {
const sanitizedName = sanitizeSessionUsername(displayName);
const errorString = !sanitizedName ? window.i18n('displayNameErrorDescription') : undefined;
if (dispatch) {
dispatch(onDisplayNameError(errorString));
} else {
onDisplayNameError(errorString); // this is is either calling dispatch in the caller or just `setDisplayNameError`
} }
return sanitizedName; return sanitizedName;
} }
/**
* Returns undefined if an error happened, or the trim userName.
*
* Be sure to use the trimmed userName for creating the account.
*/
export const displayNameIsValid = (displayName?: string): string => {
if (!displayName) {
throw new Error(window.i18n('displayNameErrorDescription'));
}
const trimName = displayName.trim();
if (!trimName) {
throw new Error(window.i18n('displayNameErrorDescription'));
}
return trimName;
};

@ -1,6 +1,5 @@
import { expect } from 'chai'; import { expect } from 'chai';
import Sinon from 'sinon'; import Sinon from 'sinon';
import { displayNameIsValid } from '../../../../components/registration/utils';
import { getSwarmPollingInstance } from '../../../../session/apis/snode_api'; import { getSwarmPollingInstance } from '../../../../session/apis/snode_api';
import { PubKey } from '../../../../session/types'; import { PubKey } from '../../../../session/types';
import { import {
@ -10,6 +9,8 @@ import {
} from '../../../../util/accountManager'; } from '../../../../util/accountManager';
import { TestUtils } from '../../../test-utils'; import { TestUtils } from '../../../test-utils';
import { stubWindow } from '../../../test-utils/utils'; import { stubWindow } from '../../../test-utils/utils';
import { sanitizeDisplayNameOrToast } from '../../../../components/registration/utils';
import { EmptyDisplayNameError } from '../../../../session/utils/errors';
describe('Onboarding', () => { describe('Onboarding', () => {
const polledDisplayName = 'Hello World'; const polledDisplayName = 'Hello World';
@ -28,31 +29,28 @@ describe('Onboarding', () => {
Sinon.restore(); Sinon.restore();
}); });
describe('displayNameIsValid', () => { describe('sanitizeDisplayNameOrToast', () => {
it('should throw an error if the display name is undefined', async () => { it('should throw an error if the display name is undefined', async () => {
try { try {
displayNameIsValid(undefined); sanitizeDisplayNameOrToast('');
} catch (error) { } catch (error) {
error.should.be.an.instanceOf(Error); error.should.be.an.instanceOf(EmptyDisplayNameError);
error.message.should.equal(window.i18n('displayNameErrorDescription'));
} }
}); });
it('should throw an error if the display name is empty after trimming', async () => { it('should throw an error if the display name is empty after trimming', async () => {
try { try {
displayNameIsValid(' '); sanitizeDisplayNameOrToast(' ');
} catch (error) { } catch (error) {
error.should.be.an.instanceOf(Error); error.should.be.an.instanceOf(EmptyDisplayNameError);
error.message.should.equal(window.i18n('displayNameErrorDescription'));
} }
}); });
it('if the display name is valid it should be returned', async () => { it('if the display name is valid it should be returned', async () => {
try { try {
const displayName = 'Hello World'; const displayName = 'Hello World';
const validDisplayName = displayNameIsValid(displayName); const validDisplayName = sanitizeDisplayNameOrToast(displayName);
expect(validDisplayName, `should equal ${displayName}`).to.equal(displayName); expect(validDisplayName, `should equal ${displayName}`).to.equal(displayName);
} catch (error) { } catch (error) {
error.should.not.be.an.instanceOf(Error); error.should.not.be.an.instanceOf(EmptyDisplayNameError);
error.message.should.not.equal(window.i18n('displayNameErrorDescription'));
} }
}); });
}); });

Loading…
Cancel
Save