2022-11-25 18:56:50 +01:00
|
|
|
import EditorJS from '../../../../types';
|
2023-05-12 19:50:48 +02:00
|
|
|
import { BlockChangedMutationType } from '../../../../types/events/block/BlockChanged';
|
2021-10-05 22:41:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* There will be described test cases of BlockAPI
|
|
|
|
*/
|
|
|
|
describe('BlockAPI', () => {
|
|
|
|
const firstBlock = {
|
|
|
|
id: 'bwnFX5LoX7',
|
|
|
|
type: 'paragraph',
|
|
|
|
data: {
|
|
|
|
text: 'The first block content mock.',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const editorDataMock = {
|
|
|
|
blocks: [
|
|
|
|
firstBlock,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EditorJS API is passed as the first parameter of the onChange callback
|
|
|
|
*/
|
|
|
|
const EditorJSApiMock = Cypress.sinon.match.any;
|
|
|
|
|
2023-05-12 19:50:48 +02:00
|
|
|
/**
|
|
|
|
* Creates Editor instance
|
|
|
|
*
|
|
|
|
* @param [data] - data to render
|
|
|
|
*/
|
|
|
|
function createEditor(data = undefined): void {
|
2022-11-25 18:56:50 +01:00
|
|
|
const config = {
|
2023-05-12 19:50:48 +02:00
|
|
|
onChange: (api, event): void => {
|
|
|
|
console.log('something changed', event);
|
2022-11-25 18:56:50 +01:00
|
|
|
},
|
2023-05-12 19:50:48 +02:00
|
|
|
data,
|
2022-11-25 18:56:50 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
cy.spy(config, 'onChange').as('onChange');
|
2021-10-05 22:41:03 +02:00
|
|
|
|
2023-05-12 19:50:48 +02:00
|
|
|
cy.createEditor(config).as('editorInstance');
|
|
|
|
}
|
2021-10-05 22:41:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* block.dispatchChange();
|
|
|
|
*/
|
|
|
|
describe('.dispatchChange()', () => {
|
|
|
|
/**
|
|
|
|
* Check that blocks.dispatchChange() triggers Editor 'onChange' callback
|
|
|
|
*/
|
|
|
|
it('should trigger onChange with corresponded block', () => {
|
2023-05-12 19:50:48 +02:00
|
|
|
createEditor(editorDataMock);
|
|
|
|
|
|
|
|
cy.get<EditorJS>('@editorInstance')
|
|
|
|
.then(async (editor) => {
|
|
|
|
const block = editor.blocks.getById(firstBlock.id);
|
2021-10-05 22:41:03 +02:00
|
|
|
|
2023-05-12 19:50:48 +02:00
|
|
|
block.dispatchChange();
|
2021-10-05 22:41:03 +02:00
|
|
|
|
2023-05-12 19:50:48 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockChangedMutationType,
|
|
|
|
detail: {
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
});
|
2021-10-05 22:41:03 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|