-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
edit.native.js
200 lines (162 loc) · 5.21 KB
/
edit.native.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
/**
* External dependencies
*/
import {
addBlock,
fireEvent,
getEditorHtml,
initializeEditor,
within,
getBlock,
waitFor,
} from 'test/helpers';
/**
* WordPress dependencies
*/
import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks';
import { registerCoreBlocks } from '@wordpress/block-library';
beforeAll( () => {
// Register all core blocks
registerCoreBlocks();
} );
afterAll( () => {
// Clean up registered blocks
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );
describe( 'Social links block', () => {
it( 'inserts block with the default icons and the WordPress link set as active', async () => {
const screen = await initializeEditor();
// Add block
await addBlock( screen, 'Social Icons' );
// Get block
const socialLinksBlock = await getBlock( screen, 'Social Icons' );
// Trigger inner blocks layout
const innerBlockListWrapper = await waitFor( () =>
within( socialLinksBlock ).getByTestId( 'block-list-wrapper' )
);
fireEvent( innerBlockListWrapper, 'layout', {
nativeEvent: {
layout: {
width: 300,
},
},
} );
// Check the WordPress icon has a URL set (active)
const firstLinkBlock = await getBlock( screen, 'Social Icon' );
fireEvent.press( firstLinkBlock );
const firstLink = within( socialLinksBlock ).getByAccessibilityHint(
/WordPress has URL set/
);
expect( firstLink ).toBeVisible();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'shows active links correctly when not selected', async () => {
const screen = await initializeEditor();
// Add Social Icons block
await addBlock( screen, 'Social Icons' );
// Get block
const socialLinksBlock = await getBlock( screen, 'Social Icons' );
// Trigger inner blocks layout
const innerBlockListWrapper = await waitFor( () =>
within( socialLinksBlock ).getByTestId( 'block-list-wrapper' )
);
fireEvent( innerBlockListWrapper, 'layout', {
nativeEvent: {
layout: {
width: 300,
},
},
} );
// Add Paragraph block
await addBlock( screen, 'Paragraph' );
// Check there's only one active social link
const socialLinks =
within( socialLinksBlock ).getAllByLabelText( / social icon/ );
expect( socialLinks.length ).toBe( 1 );
// Check the WordPress link is shown when unselected
const firstLinkBlock = await getBlock( screen, 'Social Icon' );
fireEvent.press( firstLinkBlock );
const firstLink = within( socialLinksBlock ).getByAccessibilityHint(
/WordPress has URL set/
);
expect( firstLink ).toBeVisible();
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'shows the social links bottom sheet when tapping on the inline appender', async () => {
const screen = await initializeEditor();
// Add block
await addBlock( screen, 'Social Icons' );
// Get block
const socialLinksBlock = await getBlock( screen, 'Social Icons' );
fireEvent.press( socialLinksBlock );
// Trigger inner blocks layout
const innerBlockListWrapper = await waitFor( () =>
within( socialLinksBlock ).getByTestId( 'block-list-wrapper' )
);
fireEvent( innerBlockListWrapper, 'layout', {
nativeEvent: {
layout: {
width: 300,
},
},
} );
// Open the links bottom sheet
const appenderButton =
within( socialLinksBlock ).getByTestId( 'appender-button' );
fireEvent.press( appenderButton );
// Find a social link in the inserter
const blockList = screen.getByTestId( 'InserterUI-Blocks' );
// onScroll event used to force the FlatList to render all items
fireEvent.scroll( blockList, {
nativeEvent: {
contentOffset: { y: 0, x: 0 },
contentSize: { width: 100, height: 100 },
layoutMeasurement: { width: 100, height: 100 },
},
} );
// Add the Amazon link
const amazonBlock = await screen.findByText( 'Amazon' );
expect( amazonBlock ).toBeVisible();
fireEvent.press( amazonBlock );
await screen.findByTestId( 'navigation-screen-LinkSettingsScreen' );
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'shows the ghost placeholder when no icon is active', async () => {
const screen = await initializeEditor();
const { getByLabelText } = screen;
// Add block
await addBlock( screen, 'Social Icons' );
// Get block
const socialLinksBlock = await getBlock( screen, 'Social Icons' );
// Trigger inner blocks layout
const innerBlockListWrapper = await waitFor( () =>
within( socialLinksBlock ).getByTestId( 'block-list-wrapper' )
);
fireEvent( innerBlockListWrapper, 'layout', {
nativeEvent: {
layout: {
width: 300,
},
},
} );
// Get the first social link
const firstLinkBlock = await getBlock( screen, 'Social Icon' );
fireEvent.press( firstLinkBlock );
// Open block actions menu
const blockActionsButton = getByLabelText( /Open Block Actions Menu/ );
fireEvent.press( blockActionsButton );
// Delete the social link
const deleteButton = getByLabelText( /Remove block/ );
fireEvent.press( deleteButton );
// Add Paragraph block
await addBlock( screen, 'Paragraph' );
// Check the ghost placeholders are visible
const socialLinks = within( socialLinksBlock ).getAllByTestId(
'social-links-placeholder'
);
expect( socialLinks.length ).toBe( 3 );
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );