Skip to content

Commit

Permalink
Chat: Finalize initials rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
marker-dao authored Sep 18, 2024
1 parent 9582e85 commit 85bb96d
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 25 deletions.
6 changes: 3 additions & 3 deletions e2e/testcafe-devextreme/tests/chat/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fixture.disablePageReloads`ChatAvatar`

test('Chat: avatar', async (t) => {
const { takeScreenshot, compareResults } = createScreenshotsComparer(t);
await testScreenshot(t, takeScreenshot, 'Avatar with initials.png', { element: '#chat' });
await testScreenshot(t, takeScreenshot, 'Avatar with two word initials.png', { element: '#chat' });

const chat = new Chat('#chat');

Expand All @@ -30,8 +30,8 @@ test('Chat: avatar', async (t) => {
}).before(async () => {
await appendElementTo('#container', 'div', 'chat');

const userFirst = createUser(1, 'First');
const userSecond = createUser(2, 'Second');
const userFirst = createUser(1, 'First User');
const userSecond = createUser(2, 'Second User');

const items = generateMessages(2, userFirst, userSecond, false, false, 2);

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 15 additions & 8 deletions packages/devextreme/js/__internal/ui/chat/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Avatar extends Widget<Properties> {
_getDefaultOptions(): Properties {
return {
...super._getDefaultOptions(),
name: '',
name: 'Unknown User',
url: '',
};
}
Expand Down Expand Up @@ -52,12 +52,8 @@ class Avatar extends Widget<Properties> {
}

_renderInitials(): void {
const { name } = this.option();

if (name) {
this._renderInitialsElement();
this._updateInitials();
}
this._renderInitialsElement();
this._updateInitials();
}

_renderImageElement(): void {
Expand Down Expand Up @@ -101,12 +97,23 @@ class Avatar extends Widget<Properties> {

_getInitials(name: string | undefined): string {
if (isDefined(name)) {
return String(name).charAt(0).toUpperCase();
const splitValue = String(name).trim().split(/\s+/);

const firstInitial = this._getFirstChar(splitValue[0]);
const secondInitial = this._getFirstChar(splitValue[1]);

const result = `${firstInitial}${secondInitial}`;

return result;
}

return '';
}

_getFirstChar(value: string | undefined): string {
return value?.charAt(0).toUpperCase() ?? '';
}

_optionChanged(args: OptionChanged<Properties>): void {
const { name } = args;

Expand Down
2 changes: 1 addition & 1 deletion packages/devextreme/js/__internal/ui/chat/messagegroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class MessageGroup extends Widget<Properties> {
const $information = $('<div>')
.addClass(CHAT_MESSAGEGROUP_INFORMATION_CLASS);

const authorName = author?.name ?? '';
const authorName = author?.name ?? 'Unknown User';
const authorNameText = alignment === 'start' ? authorName : '';

$('<div>')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,15 @@ QUnit.module('Avatar classes', moduleConfig, () => {

assert.strictEqual(this.$element.children().first().hasClass(AVATAR_INITIALS_CLASS), true);
});

QUnit.test('text element should be rendered if name is null, undefined or empty', function(assert) {
this.reinit({ name: null });
assert.strictEqual(this.$element.children().first().length, 1);

this.reinit({ name: undefined });
assert.strictEqual(this.$element.children().first().length, 1);

this.reinit({ name: '' });
assert.strictEqual(this.$element.children().first().length, 1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,37 @@ QUnit.module('ChatAvatar', moduleConfig, () => {
assert.ok(this.instance instanceof ChatAvatar);
});

QUnit.test('should be rendered with empty value by default', function(assert) {
assert.strictEqual(this.$element.text(), '');
QUnit.test('should be rendered with UU value by default', function(assert) {
assert.strictEqual(this.$element.text(), 'UU');
});
});

QUnit.module('Name option', () => {
QUnit.test('should be rendered with correct initials according passed name option value', function(assert) {
this.reinit({ name: 'User name' });

assert.strictEqual(this.$element.text(), 'U');
assert.strictEqual(this.$element.text(), 'UN');
});

QUnit.test('name option should be updatable at runtime', function(assert) {
this.instance.option('name', 'New Value');

assert.strictEqual(this.$element.text(), 'N');
assert.strictEqual(this.$element.text(), 'NV');
});

[
{ name: 'onewordname', expectedInitials: 'O' },
{ name: 'Three word name', expectedInitials: 'TW' },
{ name: ' New Value', expectedInitials: 'NV' },
{ name: ' New Value. ', expectedInitials: 'NV' },
{ name: '', expectedInitials: '' },
{ name: 888, expectedInitials: '8' },
{ name: undefined, expectedInitials: '' },
{ name: null, expectedInitials: '' },
// TODO: consider scenarios
// { name: ' New Name', expectedInitials: 'N' },
// { name: NaN, expectedInitials: '' },
// { name: Infinity, expectedInitials: '' },
// { name: -Infinity, expectedInitials: '' },
// { name: { firstName: 'name' }, expectedInitials: '' }
{ name: NaN, expectedInitials: 'N' },
{ name: Infinity, expectedInitials: 'I' },
{ name: -Infinity, expectedInitials: '-' },
{ name: { firstName: 'name' }, expectedInitials: '[O' }
].forEach(({ name, expectedInitials }) => {
QUnit.test(`name option is ${name}`, function(assert) {
this.reinit({ name });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,15 @@ QUnit.module('MessageGroup', moduleConfig, () => {
assert.strictEqual($information.length, 1);
});

QUnit.test('name element should not be rendered if autor.name property is not passed', function(assert) {
QUnit.test('name element should be rendered with correct text if autor.name property is not passed', function(assert) {
this.reinit({
items: [{ author: {} }],
});

const $name = this.$element.find(`.${CHAT_MESSAGEGROUP_AUTHOR_NAME_CLASS}`);

assert.strictEqual($name.length, 1);
assert.strictEqual($name.text(), '', 'name text is empty');
assert.strictEqual($name.text(), 'Unknown User', 'name text is Unknown User');
});

QUnit.test('time element should be rendered with empty text if timestamp property is not passed', function(assert) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ testComponentDefaults(Chat,
testComponentDefaults(Avatar,
{},
{
name: '',
name: 'Unknown User',
url: '',
}
);
Expand Down

0 comments on commit 85bb96d

Please sign in to comment.