|
1 | 1 | import type EditorJS from '../../../../types/index'; |
2 | | -import type { ConversionConfig, ToolboxConfig } from '../../../../types'; |
3 | | -import ToolMock from '../../fixtures/tools/ToolMock'; |
| 2 | +import type { ConversionConfig, ToolboxConfig, ToolConfig } from '../../../../types'; |
| 3 | +import ToolMock, { type MockToolData } from '../../fixtures/tools/ToolMock'; |
4 | 4 | import { nanoid } from 'nanoid'; |
5 | 5 |
|
6 | 6 | /** |
@@ -444,5 +444,84 @@ describe('api.blocks', () => { |
444 | 444 | }); |
445 | 445 | }); |
446 | 446 | }); |
| 447 | + |
| 448 | + it('should pass tool config to the conversionConfig.import method of the tool', function () { |
| 449 | + const existingBlock = { |
| 450 | + id: 'test-id-123', |
| 451 | + type: 'paragraph', |
| 452 | + data: { |
| 453 | + text: 'Some text', |
| 454 | + }, |
| 455 | + }; |
| 456 | + |
| 457 | + const conversionTargetToolConfig = { |
| 458 | + defaultStyle: 'defaultStyle', |
| 459 | + }; |
| 460 | + |
| 461 | + /** |
| 462 | + * Mock of Tool with conversionConfig |
| 463 | + */ |
| 464 | + class ToolWithConversionConfig extends ToolMock { |
| 465 | + /** |
| 466 | + * Specify conversion config of the tool |
| 467 | + */ |
| 468 | + public static get conversionConfig(): { |
| 469 | + /** |
| 470 | + * Method that is responsible for conversion from data to string |
| 471 | + */ |
| 472 | + export: (data: string) => string; |
| 473 | + |
| 474 | + /** |
| 475 | + * Method that is responsible for conversion from string to data |
| 476 | + * Should return stringified config to see, if Editor actually passed tool config to it |
| 477 | + */ |
| 478 | + import: (content: string, config: ToolConfig) => MockToolData; |
| 479 | + } { |
| 480 | + return { |
| 481 | + export: (data) => data, |
| 482 | + /** |
| 483 | + * Passed config should be returned |
| 484 | + */ |
| 485 | + import: (_content, config) => { |
| 486 | + return { text: JSON.stringify(config) }; |
| 487 | + }, |
| 488 | + }; |
| 489 | + } |
| 490 | + } |
| 491 | + |
| 492 | + cy.createEditor({ |
| 493 | + tools: { |
| 494 | + conversionTargetTool: { |
| 495 | + class: ToolWithConversionConfig, |
| 496 | + config: conversionTargetToolConfig, |
| 497 | + }, |
| 498 | + }, |
| 499 | + data: { |
| 500 | + blocks: [ |
| 501 | + existingBlock, |
| 502 | + ], |
| 503 | + }, |
| 504 | + }).then(async (editor) => { |
| 505 | + const { convert } = editor.blocks; |
| 506 | + |
| 507 | + await convert(existingBlock.id, 'conversionTargetTool'); |
| 508 | + |
| 509 | + // wait for block to be converted |
| 510 | + cy.wait(100).then(async () => { |
| 511 | + /** |
| 512 | + * Check that block was converted |
| 513 | + */ |
| 514 | + const { blocks } = await editor.save(); |
| 515 | + |
| 516 | + expect(blocks.length).to.eq(1); |
| 517 | + expect(blocks[0].type).to.eq('conversionTargetTool'); |
| 518 | + |
| 519 | + /** |
| 520 | + * Check that tool converted returned config as a result of import |
| 521 | + */ |
| 522 | + expect(blocks[0].data.text).to.eq(JSON.stringify(conversionTargetToolConfig)); |
| 523 | + }); |
| 524 | + }); |
| 525 | + }); |
447 | 526 | }); |
448 | 527 | }); |
0 commit comments