/
editor.native.js
132 lines (116 loc) · 2.97 KB
/
editor.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
/**
* External dependencies
*/
import {
act,
addBlock,
fireEvent,
getBlock,
getEditorHtml,
initializeEditor,
screen,
setupCoreBlocks,
} from 'test/helpers';
/**
* WordPress dependencies
*/
import {
requestMediaImport,
subscribeMediaAppend,
subscribeParentToggleHTMLMode,
} from '@wordpress/react-native-bridge';
setupCoreBlocks();
let toggleModeCallback;
subscribeParentToggleHTMLMode.mockImplementation( ( callback ) => {
toggleModeCallback = callback;
} );
let mediaAppendCallback;
subscribeMediaAppend.mockImplementation( ( callback ) => {
mediaAppendCallback = callback;
} );
const MEDIA = [
{
localId: 1,
mediaUrl: 'file:///local-image-1.jpeg',
mediaType: 'image',
serverId: 2000,
serverUrl: 'https://test-site.files.wordpress.com/local-image-1.jpeg',
},
{
localId: 2,
mediaUrl: 'file:///local-file-1.pdf',
mediaType: 'other',
serverId: 2001,
serverUrl: 'https://test-site.files.wordpress.com/local-file-1.pdf',
},
{
localId: 3,
mediaUrl: 'file:///local-image-3.jpeg',
mediaType: 'image',
serverId: 2002,
serverUrl: 'https://test-site.files.wordpress.com/local-image-3.jpeg',
},
{
localId: 4,
mediaUrl: 'file:///local-video-4.mp4',
mediaType: 'video',
serverId: 2003,
serverUrl: 'https://test-site.files.wordpress.com/local-video-4.mp4',
},
];
describe( 'Editor', () => {
afterEach( () => {
jest.clearAllMocks();
} );
it( 'toggles the editor from Visual to HTML mode', async () => {
// Arrange
await initializeEditor();
await addBlock( screen, 'Paragraph' );
// Act
const paragraphBlock = getBlock( screen, 'Paragraph' );
fireEvent.press( paragraphBlock );
act( () => {
toggleModeCallback();
} );
// Assert
const htmlEditor = screen.getByLabelText( 'html-view-content' );
expect( htmlEditor ).toBeVisible();
act( () => {
toggleModeCallback();
} );
} );
it( 'appends media correctly for allowed types', async () => {
// Arrange
requestMediaImport
.mockImplementationOnce( ( _, callback ) =>
callback( MEDIA[ 0 ].id, MEDIA[ 0 ].serverUrl )
)
.mockImplementationOnce( ( _, callback ) =>
callback( MEDIA[ 2 ].id, MEDIA[ 2 ].serverUrl )
);
await initializeEditor();
// Act
await act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
await act( () => mediaAppendCallback( MEDIA[ 2 ] ) );
// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );
it( 'appends media correctly for allowed types and skips unsupported ones', async () => {
// Arrange
requestMediaImport
.mockImplementationOnce( ( _, callback ) =>
callback( MEDIA[ 0 ].id, MEDIA[ 0 ].serverUrl )
)
.mockImplementationOnce( ( _, callback ) =>
callback( MEDIA[ 3 ].id, MEDIA[ 3 ].serverUrl )
);
await initializeEditor();
// Act
await act( () => mediaAppendCallback( MEDIA[ 0 ] ) );
// Unsupported type (PDF file)
await act( () => mediaAppendCallback( MEDIA[ 1 ] ) );
await act( () => mediaAppendCallback( MEDIA[ 3 ] ) );
// Assert
expect( getEditorHtml() ).toMatchSnapshot();
} );
} );