mirror of
https://github.com/codex-team/editor.js
synced 2024-05-16 21:36:48 +02:00
9c0fc48d89
The new `dispatchChange()` method allows to manually trigger the 'onChange' callback. Useful when Tool made a state mutation that is invisible for editor core.
64 lines
1.5 KiB
TypeScript
64 lines
1.5 KiB
TypeScript
import { BlockMutationType } from '../../../../types/events/block/mutation-type';
|
|
|
|
/**
|
|
* 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;
|
|
|
|
beforeEach(() => {
|
|
if (this && this.editorInstance) {
|
|
this.editorInstance.destroy();
|
|
} else {
|
|
const config = {
|
|
data: editorDataMock,
|
|
onChange: (): void => { console.log('something changed'); },
|
|
};
|
|
|
|
cy.createEditor(config).as('editorInstance');
|
|
|
|
cy.spy(config, 'onChange').as('onChange');
|
|
}
|
|
});
|
|
|
|
/**
|
|
* block.dispatchChange();
|
|
*/
|
|
describe('.dispatchChange()', () => {
|
|
/**
|
|
* Check that blocks.dispatchChange() triggers Editor 'onChange' callback
|
|
*/
|
|
it('should trigger onChange with corresponded block', () => {
|
|
cy.get('@editorInstance').then(async (editor: any) => {
|
|
const block = editor.blocks.getById(firstBlock.id);
|
|
|
|
block.dispatchChange();
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
type: BlockMutationType.Changed,
|
|
detail: {
|
|
index: 0,
|
|
},
|
|
}));
|
|
});
|
|
});
|
|
});
|
|
|
|
});
|