-
Notifications
You must be signed in to change notification settings - Fork 4k
/
edit.js
127 lines (103 loc) · 2.87 KB
/
edit.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
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';
/**
* WordPress dependencies
*/
import {
registerBlockType,
unregisterBlockType,
getBlockTypes,
} from '@wordpress/blocks';
/**
* Internal dependencies
*/
import { Edit } from '../edit';
import { BlockContextProvider } from '../../block-context';
const noop = () => {};
describe( 'Edit', () => {
afterEach( () => {
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );
it( 'should return null if block type not defined', () => {
const { container } = render( <Edit name="core/test-block" /> );
expect( container ).toBeEmptyDOMElement();
} );
it( 'should use edit implementation of block', () => {
const edit = () => <div data-testid="foo-bar" />;
registerBlockType( 'core/test-block', {
save: noop,
category: 'text',
title: 'block title',
edit,
} );
render( <Edit name="core/test-block" /> );
expect( screen.getByTestId( 'foo-bar' ) ).toBeVisible();
} );
it( 'should use save implementation of block as fallback', () => {
const save = () => <div data-testid="foo-bar" />;
registerBlockType( 'core/test-block', {
save,
category: 'text',
title: 'block title',
} );
render( <Edit name="core/test-block" /> );
expect( screen.getByTestId( 'foo-bar' ) ).toBeVisible();
} );
it( 'should combine the default class name with a custom one', () => {
const edit = ( { className } ) => (
<div data-testid="foo-bar" className={ className } />
);
const attributes = {
className: 'my-class',
};
registerBlockType( 'core/test-block', {
edit,
save: noop,
category: 'text',
title: 'block title',
} );
render( <Edit name="core/test-block" attributes={ attributes } /> );
const editElement = screen.getByTestId( 'foo-bar' );
expect( editElement ).toHaveClass( 'wp-block-test-block' );
expect( editElement ).toHaveClass( 'my-class' );
} );
it( 'should assign context', () => {
const edit = ( { context } ) => context.value;
registerBlockType( 'core/test-block', {
category: 'text',
title: 'block title',
usesContext: [ 'value' ],
edit,
save: noop,
} );
const { container } = render(
<BlockContextProvider value={ { value: 'Ok' } }>
<Edit name="core/test-block" />
</BlockContextProvider>
);
expect( container ).toHaveTextContent( 'Ok' );
} );
describe( 'light wrapper', () => {
it( 'should assign context', () => {
const edit = ( { context } ) => context.value;
registerBlockType( 'core/test-block', {
apiVersion: 3,
category: 'text',
title: 'block title',
usesContext: [ 'value' ],
edit,
save: noop,
} );
const { container } = render(
<BlockContextProvider value={ { value: 'Ok' } }>
<Edit name="core/test-block" />
</BlockContextProvider>
);
expect( container ).toHaveTextContent( 'Ok' );
} );
} );
} );