11/**
2- * WordPress dependencies
2+ * External dependencies
33 */
4- import { test , expect } from '@wordpress/e2e-test-utils-playwright ' ;
4+ import path from 'path ' ;
55
66/**
7- * External dependencies
7+ * Internal dependencies
88 */
9- import path from 'path' ;
9+ import { test , expect } from '../fixtures' ;
10+ import { TEST_SVG_PATH , trackUploads } from '../utils/media-pipeline' ;
1011
11- test ( 'Test dismissing failed upload works correctly' , async ( { page, admin, requestUtils } ) => {
12+ test ( 'Test dismissing failed upload works correctly' , async ( { page, admin } ) => {
1213 await admin . visitAdminPage ( '/media-new.php' ) ;
1314
1415 // It takes a moment for the multi-file uploader to become available.
1516 const input = page . locator ( '#plupload-upload-ui input[type="file"]' ) ;
1617 await expect ( input ) . toBeAttached ( { timeout : 10_000 } ) ;
1718
18- const testImagePath = path . join ( __dirname , '../assets/sample.svg' ) ;
19-
2019 // Upload a file that will fail.
21- await input . setInputFiles ( testImagePath ) ;
20+ await input . setInputFiles ( TEST_SVG_PATH ) ;
2221
2322 // Ensure the error message is visible.
2423 await expect (
@@ -35,75 +34,69 @@ test( 'Test dismissing failed upload works correctly', async ({ page, admin, req
3534test ( 'uploads a JPEG and shows the completed attachment' , async ( {
3635 page,
3736 admin,
38- requestUtils ,
37+ attachments ,
3938} ) => {
40- let attachmentId ;
41-
42- try {
43- await admin . visitAdminPage ( '/media-new.php' ) ;
44- const fileChooserPromise = page . waitForEvent ( 'filechooser' ) ;
45- await page
46- . getByRole ( 'button' , { name : 'Select Files' , exact : true } )
47- . click ( ) ;
48- const fileChooser = await fileChooserPromise ;
49- const uploadResponsePromise = page . waitForResponse ( ( response ) => {
50- if ( response . request ( ) . method ( ) !== 'POST' ) {
51- return false ;
52- }
39+ trackUploads ( page , attachments ) ;
40+ await admin . visitAdminPage ( '/media-new.php' ) ;
41+ const fileChooserPromise = page . waitForEvent ( 'filechooser' ) ;
42+ await page
43+ . getByRole ( 'button' , { name : 'Select Files' , exact : true } )
44+ . click ( ) ;
45+ const fileChooser = await fileChooserPromise ;
46+ const uploadResponsePromise = page . waitForResponse ( ( response ) => {
47+ if ( response . request ( ) . method ( ) !== 'POST' ) {
48+ return false ;
49+ }
5350
54- const url = new URL ( response . url ( ) ) ;
51+ const url = new URL ( response . url ( ) ) ;
5552
56- // Client-side processing creates attachments through the REST API.
57- if (
58- url . pathname . endsWith ( '/wp/v2/media' ) ||
59- url . searchParams . get ( 'rest_route' ) === '/wp/v2/media'
60- ) {
61- return true ;
62- }
53+ // Client-side processing creates attachments through the REST API.
54+ if (
55+ url . pathname . endsWith ( '/wp/v2/media' ) ||
56+ url . searchParams . get ( 'rest_route' ) === '/wp/v2/media'
57+ ) {
58+ return true ;
59+ }
6360
64- // Ignore the separate request that fetches completed attachment markup.
65- return (
66- url . pathname . endsWith ( '/async-upload.php' ) &&
67- response
68- . request ( )
69- . headers ( )
70- [ 'content-type' ] ?. startsWith ( 'multipart/form-data' )
71- ) ;
72- } ) ;
73- await fileChooser . setFiles (
74- path . join ( __dirname , '../../phpunit/data/images/test-image.jpg' )
61+ // Ignore the separate request that fetches completed attachment markup.
62+ return (
63+ url . pathname . endsWith ( '/async-upload.php' ) &&
64+ response
65+ . request ( )
66+ . headers ( )
67+ [ 'content-type' ] ?. startsWith ( 'multipart/form-data' )
7568 ) ;
76- const uploadResponse = await uploadResponsePromise ;
69+ } ) ;
70+ await fileChooser . setFiles (
71+ path . join ( __dirname , '../../phpunit/data/images/test-image.jpg' )
72+ ) ;
73+ const uploadResponse = await uploadResponsePromise ;
74+ let attachmentId = Number (
75+ uploadResponse . headers ( ) [ 'x-wp-upload-attachment-id' ]
76+ ) ;
77+ if ( ! attachmentId ) {
7778 attachmentId = Number (
78- uploadResponse . headers ( ) [ 'x-wp-upload-attachment-id' ]
79- ) ;
80- if ( ! attachmentId ) {
81- attachmentId = Number (
82- uploadResponse . headers ( ) [ 'content-type' ] ?. includes (
83- 'application/json'
84- )
85- ? ( await uploadResponse . json ( ) ) . id
86- : await uploadResponse . text ( )
87- ) ;
88- }
89- expect ( uploadResponse . ok ( ) ) . toBe ( true ) ;
90- expect ( attachmentId ) . toBeGreaterThan ( 0 ) ;
91-
92- await expect (
93- page . getByText ( 'test-image' , { exact : true } )
94- ) . toBeVisible ( ) ;
95- const editLink = page . getByRole ( 'link' , {
96- name : 'Edit' ,
97- exact : true ,
98- } ) ;
99- await expect ( editLink ) . toBeVisible ( ) ;
100- await expect ( editLink ) . toHaveAttribute (
101- 'href' ,
102- new RegExp ( `[?&]post=${ attachmentId } &` )
79+ uploadResponse . headers ( ) [ 'content-type' ] ?. includes (
80+ 'application/json'
81+ )
82+ ? ( await uploadResponse . json ( ) ) . id
83+ : await uploadResponse . text ( )
10384 ) ;
104- } finally {
105- if ( Number . isInteger ( attachmentId ) && attachmentId > 0 ) {
106- await requestUtils . deleteMedia ( attachmentId ) ;
107- }
10885 }
86+ expect ( uploadResponse . ok ( ) ) . toBe ( true ) ;
87+ expect ( attachmentId ) . toBeGreaterThan ( 0 ) ;
88+ attachments . track ( attachmentId ) ;
89+
90+ await expect (
91+ page . getByText ( 'test-image' , { exact : true } )
92+ ) . toBeVisible ( ) ;
93+ const editLink = page . getByRole ( 'link' , {
94+ name : 'Edit' ,
95+ exact : true ,
96+ } ) ;
97+ await expect ( editLink ) . toBeVisible ( ) ;
98+ await expect ( editLink ) . toHaveAttribute (
99+ 'href' ,
100+ new RegExp ( `[?&]post=${ attachmentId } &` )
101+ ) ;
109102} ) ;
0 commit comments