2021-11-24 19:14:24 +01:00
|
|
|
import Header from '@editorjs/header';
|
2022-01-13 17:12:08 +01:00
|
|
|
import Code from '@editorjs/code';
|
2022-04-07 11:03:09 +02:00
|
|
|
import Delimiter from '@editorjs/delimiter';
|
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,
|
2022-01-13 17:12:08 +01:00
|
|
|
code: Code,
|
2021-10-05 19:40:44 +02:00
|
|
|
},
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2022-04-07 11:03:09 +02:00
|
|
|
/**
|
|
|
|
* Creates Editor instance with save inside the onChange event.
|
|
|
|
*
|
|
|
|
* @param blocks - list of blocks to prefill the editor
|
|
|
|
*/
|
|
|
|
function createEditorWithSave(blocks = null): void {
|
|
|
|
const config = {
|
|
|
|
tools: {
|
|
|
|
header: Header,
|
|
|
|
code: Code,
|
|
|
|
delimiter: Delimiter,
|
|
|
|
},
|
|
|
|
onChange: (api, event): void => {
|
|
|
|
console.log('something changed', api, event);
|
|
|
|
api.saver.save();
|
|
|
|
},
|
|
|
|
data: blocks ? {
|
|
|
|
blocks,
|
|
|
|
} : null,
|
|
|
|
};
|
|
|
|
|
|
|
|
cy.spy(config, 'onChange').as('onChange');
|
|
|
|
|
|
|
|
cy.createEditor(config).as('editorInstance');
|
|
|
|
}
|
|
|
|
|
2021-10-05 19:40:44 +02:00
|
|
|
/**
|
|
|
|
* 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: {
|
2022-01-13 17:12:08 +01:00
|
|
|
name: 'paragraph',
|
2021-10-05 19:40:44 +02:00
|
|
|
},
|
|
|
|
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: {
|
2022-01-13 17:12:08 +01:00
|
|
|
name: 'paragraph',
|
2021-10-05 19:40:44 +02:00
|
|
|
},
|
|
|
|
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: {
|
2022-01-13 17:12:08 +01:00
|
|
|
index: 0,
|
2021-10-05 19:40:44 +02:00
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
|
|
|
|
2022-04-07 11:03:09 +02:00
|
|
|
it('should fire onChange callback on block insertion with save inside onChange', () => {
|
|
|
|
createEditorWithSave();
|
|
|
|
|
|
|
|
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]')
|
2022-04-25 17:28:58 +02:00
|
|
|
.get('div.ce-popover__item[data-item-name=delimiter]')
|
2022-04-07 11:03:09 +02:00
|
|
|
.click();
|
|
|
|
|
|
|
|
cy.get('@onChange').should('be.calledThrice');
|
|
|
|
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: 'delimiter',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Added,
|
|
|
|
detail: {
|
|
|
|
index: 1,
|
|
|
|
target: {
|
|
|
|
name: 'paragraph',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
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]')
|
2022-04-25 17:28:58 +02:00
|
|
|
.get('div.ce-popover__item[data-item-name=header]')
|
2021-05-26 17:59:32 +02:00
|
|
|
.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();
|
|
|
|
|
2022-04-25 17:28:58 +02:00
|
|
|
/**
|
|
|
|
* The only block does not have Tune menu, so need to create at least 2 blocks to test deleting
|
|
|
|
*/
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div.ce-block')
|
|
|
|
.click()
|
|
|
|
.type('some text');
|
|
|
|
|
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]')
|
2022-11-03 18:52:33 +01:00
|
|
|
.get('div[data-item-name=delete]')
|
|
|
|
.click();
|
|
|
|
|
|
|
|
/** Second click for confirmation */
|
|
|
|
cy.get('[data-cy=editorjs]')
|
|
|
|
.get('div[data-item-name=delete]')
|
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.Removed,
|
|
|
|
detail: {
|
2022-01-13 17:12:08 +01:00
|
|
|
index: 0,
|
2021-10-05 19:40:44 +02:00
|
|
|
},
|
|
|
|
}));
|
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]')
|
2022-11-03 18:52:33 +01:00
|
|
|
.get('div[data-item-name=move-up]')
|
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.Moved,
|
|
|
|
detail: {
|
|
|
|
fromIndex: 1,
|
|
|
|
toIndex: 0,
|
|
|
|
},
|
|
|
|
}));
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|
2022-01-13 17:12:08 +01:00
|
|
|
|
|
|
|
it('should fire onChange if something changed inside native input', () => {
|
|
|
|
createEditor([ {
|
|
|
|
type: 'code',
|
|
|
|
data: {
|
|
|
|
code: '',
|
|
|
|
},
|
|
|
|
} ]);
|
|
|
|
|
|
|
|
cy.get('[data-cy=editorjs')
|
|
|
|
.get('textarea')
|
|
|
|
.type('Some input to the textarea');
|
|
|
|
|
|
|
|
cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({
|
|
|
|
type: BlockMutationType.Changed,
|
|
|
|
detail: {
|
|
|
|
index: 0,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
});
|
2021-05-26 17:59:32 +02:00
|
|
|
});
|