|
1 | 1 | import Header from '@editorjs/header'; |
2 | 2 | import Code from '@editorjs/code'; |
| 3 | +import Delimiter from '@editorjs/delimiter'; |
3 | 4 | import { BlockMutationType } from '../../../types/events/block/mutation-type'; |
4 | 5 |
|
5 | 6 | /** |
@@ -32,6 +33,32 @@ describe('onChange callback', () => { |
32 | 33 | cy.createEditor(config).as('editorInstance'); |
33 | 34 | } |
34 | 35 |
|
| 36 | + /** |
| 37 | + * Creates Editor instance with save inside the onChange event. |
| 38 | + * |
| 39 | + * @param blocks - list of blocks to prefill the editor |
| 40 | + */ |
| 41 | + function createEditorWithSave(blocks = null): void { |
| 42 | + const config = { |
| 43 | + tools: { |
| 44 | + header: Header, |
| 45 | + code: Code, |
| 46 | + delimiter: Delimiter, |
| 47 | + }, |
| 48 | + onChange: (api, event): void => { |
| 49 | + console.log('something changed', api, event); |
| 50 | + api.saver.save(); |
| 51 | + }, |
| 52 | + data: blocks ? { |
| 53 | + blocks, |
| 54 | + } : null, |
| 55 | + }; |
| 56 | + |
| 57 | + cy.spy(config, 'onChange').as('onChange'); |
| 58 | + |
| 59 | + cy.createEditor(config).as('editorInstance'); |
| 60 | + } |
| 61 | + |
35 | 62 | /** |
36 | 63 | * EditorJS API is passed as the first parameter of the onChange callback |
37 | 64 | */ |
@@ -92,6 +119,53 @@ describe('onChange callback', () => { |
92 | 119 | })); |
93 | 120 | }); |
94 | 121 |
|
| 122 | + it('should fire onChange callback on block insertion with save inside onChange', () => { |
| 123 | + createEditorWithSave(); |
| 124 | + |
| 125 | + cy.get('[data-cy=editorjs]') |
| 126 | + .get('div.ce-block') |
| 127 | + .click(); |
| 128 | + |
| 129 | + cy.get('[data-cy=editorjs]') |
| 130 | + .get('div.ce-toolbar__plus') |
| 131 | + .click(); |
| 132 | + |
| 133 | + cy.get('[data-cy=editorjs]') |
| 134 | + .get('li.ce-toolbox__button[data-tool=delimiter]') |
| 135 | + .click(); |
| 136 | + |
| 137 | + cy.get('@onChange').should('be.calledThrice'); |
| 138 | + cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({ |
| 139 | + type: BlockMutationType.Removed, |
| 140 | + detail: { |
| 141 | + index: 0, |
| 142 | + target: { |
| 143 | + name: 'paragraph', |
| 144 | + }, |
| 145 | + }, |
| 146 | + })); |
| 147 | + |
| 148 | + cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({ |
| 149 | + type: BlockMutationType.Added, |
| 150 | + detail: { |
| 151 | + index: 0, |
| 152 | + target: { |
| 153 | + name: 'delimiter', |
| 154 | + }, |
| 155 | + }, |
| 156 | + })); |
| 157 | + |
| 158 | + cy.get('@onChange').should('be.calledWithMatch', EditorJSApiMock, Cypress.sinon.match({ |
| 159 | + type: BlockMutationType.Added, |
| 160 | + detail: { |
| 161 | + index: 1, |
| 162 | + target: { |
| 163 | + name: 'paragraph', |
| 164 | + }, |
| 165 | + }, |
| 166 | + })); |
| 167 | + }); |
| 168 | + |
95 | 169 | it('should fire onChange callback on block replacement for both of blocks', () => { |
96 | 170 | createEditor(); |
97 | 171 |
|
|
0 commit comments