forked from WordPress/gutenberg
/
register-format-type.js
245 lines (218 loc) · 7.58 KB
/
register-format-type.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* WordPress dependencies
*/
import { dispatch, select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { registerFormatType } from '../register-format-type';
import { unregisterFormatType } from '../unregister-format-type';
import { getFormatType } from '../get-format-type';
import { store as richTextStore } from '../store';
const UNKNOWN_FORMAT = {
name: 'core/unknown',
title: 'Clear Unknown Formatting',
tagName: '*',
className: null,
edit: () => null,
};
describe( 'registerFormatType', () => {
beforeAll( () => {
// Initialize the rich-text store.
require( '../store' );
// Register "core/unknown" format
dispatch( richTextStore ).addFormatTypes( UNKNOWN_FORMAT );
} );
afterEach( () => {
select( richTextStore )
.getFormatTypes()
.forEach( ( { name } ) => {
unregisterFormatType( name );
} );
} );
const validName = 'plugin/test';
const validSettings = {
tagName: 'test',
className: null,
title: 'Test',
edit() {},
};
it( 'should register format', () => {
const settings = registerFormatType( validName, validSettings );
expect( settings ).toEqual( { ...validSettings, name: validName } );
expect( console ).not.toHaveErrored();
} );
it( 'should error without arguments', () => {
const format = registerFormatType();
expect( console ).toHaveErroredWith( 'Format names must be strings.' );
expect( format ).toBeUndefined();
} );
it( 'should reject format types without a namespace', () => {
const format = registerFormatType( 'doing-it-wrong' );
expect( console ).toHaveErroredWith(
'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
);
expect( format ).toBeUndefined();
} );
it( 'should reject format types with too many namespaces', () => {
const format = registerFormatType( 'doing/it/wrong' );
expect( console ).toHaveErroredWith(
'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
);
expect( format ).toBeUndefined();
} );
it( 'should reject format types with invalid characters', () => {
const format = registerFormatType( 'still/_doing_it_wrong' );
expect( console ).toHaveErroredWith(
'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
);
expect( format ).toBeUndefined();
} );
it( 'should reject format types with uppercase characters', () => {
const format = registerFormatType( 'Core/Bold' );
expect( console ).toHaveErroredWith(
'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
);
expect( format ).toBeUndefined();
} );
it( 'should reject format types not starting with a letter', () => {
const format = registerFormatType( 'my-plugin/4-fancy-format' );
expect( console ).toHaveErroredWith(
'Format names must contain a namespace prefix, include only lowercase alphanumeric characters or dashes, and start with a letter. Example: my-plugin/my-custom-format'
);
expect( format ).toBeUndefined();
} );
it( 'should reject numbers', () => {
const format = registerFormatType( 42 );
expect( console ).toHaveErroredWith( 'Format names must be strings.' );
expect( format ).toBeUndefined();
} );
it( 'should accept valid format names', () => {
const format = registerFormatType(
'my-plugin/fancy-format-4',
validSettings
);
expect( console ).not.toHaveErrored();
expect( format ).toEqual( {
name: 'my-plugin/fancy-format-4',
...validSettings,
} );
} );
it( 'should error on already registered name', () => {
registerFormatType( validName, validSettings );
const duplicateFormat = registerFormatType( validName, validSettings );
expect( console ).toHaveErroredWith(
'Format "plugin/test" is already registered.'
);
expect( duplicateFormat ).toBeUndefined();
} );
it( 'should reject formats without tag name', () => {
const settings = { ...validSettings };
delete settings.tagName;
const format = registerFormatType( validName, settings );
expect( console ).toHaveErroredWith(
'Format tag names must be a string.'
);
expect( format ).toBeUndefined();
} );
it( 'should error on empty tagName property', () => {
const format = registerFormatType( validName, {
...validSettings,
tagName: '',
} );
expect( console ).toHaveErroredWith(
'Format tag names must be a string.'
);
expect( format ).toBeUndefined();
} );
it( 'should reject formats without class name', () => {
const settings = { ...validSettings };
delete settings.className;
const format = registerFormatType( validName, settings );
expect( console ).toHaveErroredWith(
'Format class names must be a string, or null to handle bare elements.'
);
expect( format ).toBeUndefined();
} );
it( 'should error on invalid empty className property', () => {
const format = registerFormatType( validName, {
...validSettings,
className: '',
} );
expect( console ).toHaveErroredWith(
'Format class names must be a string, or null to handle bare elements.'
);
expect( format ).toBeUndefined();
} );
it( 'should error on invalid className property', () => {
const format = registerFormatType( validName, {
...validSettings,
className: 'invalid class name',
} );
expect( console ).toHaveErroredWith(
'A class name must begin with a letter, followed by any number of hyphens, letters, or numbers.'
);
expect( format ).toBeUndefined();
} );
it( 'should error on already registered tagName', () => {
registerFormatType( validName, validSettings );
const duplicateTagNameFormat = registerFormatType(
'plugin/second',
validSettings
);
expect( console ).toHaveErroredWith(
'Format "plugin/test" is already registered to handle bare tag name "test".'
);
expect( duplicateTagNameFormat ).toBeUndefined();
} );
it( 'should error on already registered className', () => {
registerFormatType( validName, {
...validSettings,
className: 'test',
} );
const duplicateClassNameFormat = registerFormatType( 'plugin/second', {
...validSettings,
className: 'test',
} );
expect( console ).toHaveErroredWith(
'Format "plugin/test" is already registered to handle class name "test".'
);
expect( duplicateClassNameFormat ).toBeUndefined();
} );
it( 'should reject formats without title', () => {
const settings = { ...validSettings };
delete settings.title;
const format = registerFormatType( validName, settings );
expect( console ).toHaveErroredWith(
`The format "${ validName }" must have a title.`
);
expect( format ).toBeUndefined();
} );
it( 'should error on empty title property', () => {
const format = registerFormatType( validName, {
...validSettings,
title: '',
} );
expect( console ).toHaveErroredWith(
'The format "plugin/test" must have a title.'
);
expect( format ).toBeUndefined();
} );
it( 'should reject titles which are not strings', () => {
const format = registerFormatType( validName, {
...validSettings,
title: 1337,
} );
expect( console ).toHaveErroredWith( 'Format titles must be strings.' );
expect( format ).toBeUndefined();
} );
it( 'should store a copy of the format type', () => {
const formatType = { ...validSettings };
registerFormatType( validName, formatType );
formatType.mutated = true;
expect( getFormatType( validName ) ).toEqual( {
name: validName,
...validSettings,
} );
} );
} );