3
3
* For licensing, see LICENSE.md.
4
4
*/
5
5
6
+ import Autoformat from '../src/autoformat' ;
6
7
import BlockAutoformatEditing from '../src/blockautoformatediting' ;
7
8
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph' ;
8
9
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor' ;
@@ -19,7 +20,7 @@ describe( 'BlockAutoformatEditing', () => {
19
20
beforeEach ( ( ) => {
20
21
return VirtualTestEditor
21
22
. create ( {
22
- plugins : [ Enter , Paragraph ]
23
+ plugins : [ Enter , Paragraph , Autoformat ]
23
24
} )
24
25
. then ( newEditor => {
25
26
editor = newEditor ;
@@ -28,13 +29,17 @@ describe( 'BlockAutoformatEditing', () => {
28
29
} ) ;
29
30
} ) ;
30
31
31
- describe ( 'Command name' , ( ) => {
32
+ describe ( 'command name' , ( ) => {
32
33
it ( 'should run a command when the pattern is matched' , ( ) => {
33
34
const spy = testUtils . sinon . spy ( ) ;
34
- editor . commands . add ( 'testCommand' , new TestCommand ( editor , spy ) ) ;
35
+ const testCommand = new TestCommand ( editor , spy ) ;
36
+
37
+ editor . commands . add ( 'testCommand' , testCommand ) ;
38
+
35
39
new BlockAutoformatEditing ( editor , / ^ [ * ] \s $ / , 'testCommand' ) ; // eslint-disable-line no-new
36
40
37
41
setData ( model , '<paragraph>*[]</paragraph>' ) ;
42
+
38
43
model . change ( writer => {
39
44
writer . insertText ( ' ' , doc . selection . getFirstPosition ( ) ) ;
40
45
} ) ;
@@ -44,20 +49,45 @@ describe( 'BlockAutoformatEditing', () => {
44
49
45
50
it ( 'should remove found pattern' , ( ) => {
46
51
const spy = testUtils . sinon . spy ( ) ;
47
- editor . commands . add ( 'testCommand' , new TestCommand ( editor , spy ) ) ;
52
+ const testCommand = new TestCommand ( editor , spy ) ;
53
+
54
+ editor . commands . add ( 'testCommand' , testCommand ) ;
55
+
48
56
new BlockAutoformatEditing ( editor , / ^ [ * ] \s $ / , 'testCommand' ) ; // eslint-disable-line no-new
49
57
50
58
setData ( model , '<paragraph>*[]</paragraph>' ) ;
59
+
51
60
model . change ( writer => {
52
61
writer . insertText ( ' ' , doc . selection . getFirstPosition ( ) ) ;
53
62
} ) ;
54
63
55
64
sinon . assert . calledOnce ( spy ) ;
56
65
expect ( getData ( model ) ) . to . equal ( '<paragraph>[]</paragraph>' ) ;
57
66
} ) ;
67
+
68
+ it ( 'should not autoformat if command is disabled' , ( ) => {
69
+ const spy = testUtils . sinon . spy ( ) ;
70
+ const testCommand = new TestCommand ( editor , spy ) ;
71
+
72
+ testCommand . refresh = function ( ) {
73
+ this . isEnabled = false ;
74
+ } ;
75
+
76
+ editor . commands . add ( 'testCommand' , testCommand ) ;
77
+
78
+ new BlockAutoformatEditing ( editor , / ^ [ * ] \s $ / , 'testCommand' ) ; // eslint-disable-line no-new
79
+
80
+ setData ( model , '<paragraph>*[]</paragraph>' ) ;
81
+
82
+ model . change ( writer => {
83
+ writer . insertText ( ' ' , doc . selection . getFirstPosition ( ) ) ;
84
+ } ) ;
85
+
86
+ sinon . assert . notCalled ( spy ) ;
87
+ } ) ;
58
88
} ) ;
59
89
60
- describe ( 'Callback ' , ( ) => {
90
+ describe ( 'callback ' , ( ) => {
61
91
it ( 'should run callback when the pattern is matched' , ( ) => {
62
92
const spy = testUtils . sinon . spy ( ) ;
63
93
new BlockAutoformatEditing ( editor , / ^ [ * ] \s $ / , spy ) ; // eslint-disable-line no-new
0 commit comments