test: add tests for i18n

This commit is contained in:
JackUait 2025-11-10 22:13:35 +03:00
commit 3ec36a5d7c
4 changed files with 1479 additions and 246 deletions

View file

@ -7,6 +7,7 @@ import SelectionUtils from '../selection';
import { getConvertibleToolsForBlock } from '../utils/blocks';
import I18nInternal from '../i18n';
import { I18nInternalNS } from '../i18n/namespace-internal';
import type BlockToolAdapter from '../tools/block';
/**
* Inline tools for converting blocks
@ -58,13 +59,18 @@ export default class ConvertInlineTool implements InlineTool {
*/
public async render(): Promise<MenuConfig> {
const currentSelection = SelectionUtils.get();
if (currentSelection === null) {
return [];
}
const currentBlock = this.blocksAPI.getBlockByElement(currentSelection.anchorNode as HTMLElement);
if (currentBlock === undefined) {
return [];
}
const allBlockTools = this.toolsAPI.getBlockTools();
const allBlockTools = this.toolsAPI.getBlockTools() as BlockToolAdapter[];
const convertibleTools = await getConvertibleToolsForBlock(currentBlock, allBlockTools);
if (convertibleTools.length === 0) {
@ -73,6 +79,10 @@ export default class ConvertInlineTool implements InlineTool {
const convertToItems = convertibleTools.reduce<MenuConfigItem[]>((result, tool) => {
tool.toolbox?.forEach((toolboxItem) => {
if (toolboxItem.title === undefined) {
return;
}
result.push({
icon: toolboxItem.icon,
title: I18nInternal.t(I18nInternalNS.toolNames, toolboxItem.title),
@ -97,7 +107,7 @@ export default class ConvertInlineTool implements InlineTool {
icon,
name: 'convert-to',
hint: {
title: this.i18nAPI.t('Convert to'),
title: I18nInternal.ui(I18nInternalNS.ui.inlineToolbar.converter, 'Convert to'),
},
children: {
searchable: isDesktop,

View file

@ -57,6 +57,7 @@ export class SearchInput extends EventsDispatcher<SearchInputEventMap> {
});
this.input = Dom.make('input', css.input, {
type: 'search',
placeholder,
/**
* Used to prevent focusing on the input by Tab key

View file

@ -1,244 +0,0 @@
import Header from '@editorjs/header';
import type { ToolboxConfig } from '../../../types';
import { EDITOR_SELECTOR } from '../support/constants';
describe('Editor i18n', () => {
context('Toolbox', () => {
it('should translate tool title in a toolbox', function () {
if (this != null && this.editorInstance != null) {
this.editorInstance.destroy();
}
const toolNamesDictionary = {
Heading: 'Заголовок',
};
cy.createEditor({
tools: {
header: Header,
},
i18n: {
messages: {
toolNames: toolNamesDictionary,
},
},
}).as('editorInstance');
cy.get(EDITOR_SELECTOR)
.get('div.ce-block')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-toolbar__plus')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-popover-item[data-item-name=header]')
.should('contain.text', toolNamesDictionary.Heading);
});
it('should translate titles of toolbox entries', function () {
if (this != null && this.editorInstance != null) {
this.editorInstance.destroy();
}
const toolNamesDictionary = {
Title1: 'Название 1',
Title2: 'Название 2',
};
/**
* Tool with several toolbox entries configured
*/
class TestTool {
/**
* Returns toolbox config as list of entries
*/
public static get toolbox(): ToolboxConfig {
return [
{
title: 'Title1',
icon: 'Icon 1',
},
{
title: 'Title2',
icon: 'Icon 2',
},
];
}
}
cy.createEditor({
tools: {
testTool: TestTool,
},
i18n: {
messages: {
toolNames: toolNamesDictionary,
},
},
}).as('editorInstance');
cy.get(EDITOR_SELECTOR)
.get('div.ce-block')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-toolbar__plus')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-popover-item[data-item-name=testTool]')
.first()
.should('contain.text', toolNamesDictionary.Title1);
cy.get(EDITOR_SELECTOR)
.get('div.ce-popover-item[data-item-name=testTool]')
.last()
.should('contain.text', toolNamesDictionary.Title2);
});
it('should use capitalized tool name as translation key if toolbox title is missing', function () {
if (this != null && this.editorInstance != null) {
this.editorInstance.destroy();
}
/**
* Tool class allowing to test case when capitalized tool name is used as translation key if toolbox title is missing
*/
class TestTool {
/**
* Returns toolbox config without title
*/
public static get toolbox(): ToolboxConfig {
return {
title: '',
icon: '<svg width="17" height="15" viewBox="0 0 336 276" xmlns="http://www.w3.org/2000/svg"><path d="M291 150V79c0-19-15-34-34-34H79c-19 0-34 15-34 34v42l67-44 81 72 56-29 42 30zm0 52l-43-30-56 30-81-67-66 39v23c0 19 15 34 34 34h178c17 0 31-13 34-29zM79 0h178c44 0 79 35 79 79v118c0 44-35 79-79 79H79c-44 0-79-35-79-79V79C0 35 35 0 79 0z"/></svg>',
};
}
}
const toolNamesDictionary = {
TestTool: 'ТестТул',
};
cy.createEditor({
tools: {
testTool: TestTool,
},
i18n: {
messages: {
toolNames: toolNamesDictionary,
},
},
});
cy.get(EDITOR_SELECTOR)
.get('div.ce-block')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-toolbar__plus')
.click();
cy.get(EDITOR_SELECTOR)
.get('div.ce-popover-item[data-item-name=testTool]')
.should('contain.text', toolNamesDictionary.TestTool);
});
});
context('Block Tunes', () => {
it('should translate tool name in Convert To', () => {
const toolNamesDictionary = {
Heading: 'Заголовок',
};
cy.createEditor({
tools: {
header: Header,
},
i18n: {
messages: {
toolNames: toolNamesDictionary,
},
},
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Some text',
level: 1,
},
},
],
},
});
cy.get(EDITOR_SELECTOR)
.get('div.ce-block')
.click();
/** Open block tunes menu */
cy.get(EDITOR_SELECTOR)
.get('.ce-block')
.click();
cy.get(EDITOR_SELECTOR)
.get('.ce-toolbar__settings-btn')
.click();
/** Open "Convert to" menu */
cy.get(EDITOR_SELECTOR)
.get('[data-item-name=convert-to]')
.click();
/** Check item in convert to menu is internationalized */
cy.get(EDITOR_SELECTOR)
.get('.ce-popover--nested .ce-popover-item[data-item-name=header]')
.should('contain.text', toolNamesDictionary.Heading);
});
});
context('Inline Toolbar', () => {
it('should translate tool name in Convert To', () => {
const toolNamesDictionary = {
Heading: 'Заголовок',
};
cy.createEditor({
tools: {
header: Header,
},
i18n: {
messages: {
toolNames: toolNamesDictionary,
},
},
data: {
blocks: [
{
type: 'paragraph',
data: {
text: 'Some text',
level: 1,
},
},
],
},
});
/** Open Inline Toolbar */
cy.get(EDITOR_SELECTOR)
.find('.ce-paragraph')
.selectText('Some text');
/** Open "Convert to" menu */
cy.get(EDITOR_SELECTOR)
.get('[data-item-name=convert-to]')
.click();
/** Check item in convert to menu is internationalized */
cy.get(EDITOR_SELECTOR)
.get('.ce-popover--nested .ce-popover-item[data-item-name=header]')
.should('contain.text', toolNamesDictionary.Heading);
});
});
});

File diff suppressed because it is too large Load diff