2021-05-26 17:59:32 +02:00
|
|
|
import Header from '../../../example/tools/header';
|
2021-10-05 19:40:44 +02:00
|
|
|
import { BlockMutationType } from '../../../types/events/block/mutation-type';
|
2021-05-26 17:59:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @todo Add checks that correct block API object is passed to onChange
|
|
|
|
* @todo Add cases for native inputs changes
|
2021-10-05 19:40:44 +02:00
|
|
|
* @todo debug onChange firing on Block Tune toggling (see below)
|
2021-05-26 17:59:32 +02:00
|
|
|
*/
|
|
|
|
describe('onChange callback', () => {
|
2021-10-05 19:40:44 +02:00
|
|
|
/**
|
|
|
|
* Creates Editor instance
|
|
|
|
*
|
|
|
|
* @param blocks - list of blocks to prefill the editor
|
|
|
|
*/
|
|
|
|
function createEditor(blocks = null): void {
|
|
|
|
const config = {
|
|
|
|
tools: {
|
|
|
|
header: Header,
|
|
|
|
},
|
|
|
|
onChange: (api, event): void => {
|
|
|
|
console.log('something changed', api, event);
|
|
|
|
},
|
|
|
|
data: blocks ? {
|
|
|
|
blocks,
|
|
|
|
} : null,
|
|
|
|
};
|
|
|
|
|
|
|
|
cy.spy(config, 'onChange').as('onChange');
|
|
|
|
|
|
|
|
cy.createEditor(config).as('editorInstance');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EditorJS API is passed as the first parameter of the onChange callback
|
|
|
|
*/
|
|
|
|
const EditorJSApiMock = Cypress.sinon.match.any;
|
|
|
|
|
|
|
|
it('should fire onChange callback with correct index on block insertion above the current (by pressing Enter at the start)', () => {
|
|
|
|
createEditor();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click()
|
|
|
|
.type('{enter}');
|
|
|
|
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Added,
|
|
|
|
detail: {
|
|
|
|
target: {
|
|
|
|
name: 'paragraph'
|
|
|
|
},
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
it('should fire onChange callback with correct index on block insertion below the current (by pressing enter at the end)', () => {
|
|
|
|
createEditor();
|
|
|
|
|
2021-05-26 17:59:32 +02:00
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click()
|
2021-10-05 19:40:44 +02:00
|
|
|
.type('some text')
|
2021-05-26 17:59:32 +02:00
|
|
|
.type('{enter}');
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Added,
|
|
|
|
detail: {
|
|
|
|
target: {
|
|
|
|
name: 'paragraph'
|
|
|
|
},
|
|
|
|
index: 1,
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire onChange callback on typing into block', () => {
|
2021-10-05 19:40:44 +02:00
|
|
|
createEditor();
|
|
|
|
|
2021-05-26 17:59:32 +02:00
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click()
|
|
|
|
.type('some text');
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Changed,
|
|
|
|
detail: {
|
|
|
|
index: 0
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
it('should fire onChange callback on block replacement for both of blocks', () => {
|
|
|
|
createEditor();
|
|
|
|
|
2021-05-26 17:59:32 +02:00
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-toolbar__plus')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('li.ce-toolbox__button[data-tool=header]')
|
|
|
|
.click();
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledTwice');
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Removed,
|
|
|
|
detail: {
|
|
|
|
index: 0,
|
|
|
|
target: {
|
|
|
|
name: 'paragraph',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Added,
|
|
|
|
detail: {
|
|
|
|
index: 0,
|
|
|
|
target: {
|
|
|
|
name: 'header',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
it('should fire onChange callback on tune modifying', () => {
|
|
|
|
createEditor([
|
|
|
|
{
|
|
|
|
type: 'header',
|
|
|
|
data: {
|
|
|
|
text: 'Header block',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]);
|
2021-05-26 17:59:32 +02:00
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('span.ce-toolbar__settings-btn')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
2021-10-05 19:40:44 +02:00
|
|
|
.get('span.cdx-settings-button[data-level=4]')
|
|
|
|
.click()
|
|
|
|
/**
|
|
|
|
* For some reason, the first click fires the mutation of removeFakeCursor only, so we need to click again.
|
|
|
|
* Reproduced only in Cypress.
|
|
|
|
*
|
|
|
|
* @todo debug it later
|
|
|
|
*/
|
2021-05-26 17:59:32 +02:00
|
|
|
.click();
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Changed,
|
|
|
|
detail: {
|
|
|
|
index: 0,
|
|
|
|
target: {
|
|
|
|
name: 'header',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire onChange callback when block is removed', () => {
|
2021-10-05 19:40:44 +02:00
|
|
|
createEditor();
|
|
|
|
|
2021-05-26 17:59:32 +02:00
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('span.ce-toolbar__settings-btn')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-settings__button--delete')
|
|
|
|
.click()
|
|
|
|
.click();
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Removed,
|
|
|
|
detail: {
|
|
|
|
index: 0
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should fire onChange callback when block is moved', () => {
|
2021-10-05 19:40:44 +02:00
|
|
|
createEditor();
|
|
|
|
|
2021-05-26 17:59:32 +02:00
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click()
|
|
|
|
.type('{enter}');
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.last()
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('span.ce-toolbar__settings-btn')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-tune-move-up')
|
|
|
|
.click();
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Moved,
|
|
|
|
detail: {
|
|
|
|
fromIndex: 1,
|
|
|
|
toIndex: 0,
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
});
|