|
1 | 1 | import { Page } from '@playwright/test'; |
2 | 2 | import { ReviewPage } from './review.page'; |
3 | | -import fs from 'fs'; |
4 | | -import path from 'path'; |
5 | 3 | import Papa from 'papaparse'; |
6 | 4 | import { NavigationPage } from './navigation.page'; |
7 | | -import { clearTmpDir } from '../utils/tmpdir'; |
| 5 | +import { |
| 6 | + prepareTmpDirForBulkUpload, |
| 7 | + prepareTmpDirForSingleUpload, |
| 8 | + uploadFilesFromTmpDir, |
| 9 | +} from '../utils/file-upload-helpers'; |
8 | 10 |
|
9 | 11 | class SubmissionPage { |
10 | 12 | protected page: Page; |
@@ -68,17 +70,6 @@ class SubmissionPage { |
68 | 70 | await reviewPage.waitForZeroProcessing(); |
69 | 71 | return reviewPage; |
70 | 72 | } |
71 | | - |
72 | | - protected async _uploadFilesFromTmpDir(testId: string, tmpDir: string, fileCount: number) { |
73 | | - await this.page.getByRole('heading', { name: 'Extra files' }).scrollIntoViewIfNeeded(); |
74 | | - // Trigger file upload (don't await) and wait for checkmarks to appear (indicates success) |
75 | | - void this.page.getByTestId(testId).setInputFiles(tmpDir); |
76 | | - return Promise.all( |
77 | | - Array.from({ length: fileCount }, (_, i) => |
78 | | - this.page.getByText('✓').nth(i).waitFor({ state: 'visible' }), |
79 | | - ), |
80 | | - ); |
81 | | - } |
82 | 73 | } |
83 | 74 |
|
84 | 75 | export class SingleSequenceSubmissionPage extends SubmissionPage { |
@@ -140,19 +131,9 @@ export class SingleSequenceSubmissionPage extends SubmissionPage { |
140 | 131 | fileContents: Record<string, string>, |
141 | 132 | tmpDir: string, |
142 | 133 | ) { |
143 | | - await this._prepareTmpDirWithFiles(fileContents, tmpDir); |
| 134 | + await prepareTmpDirForSingleUpload(fileContents, tmpDir); |
144 | 135 | const fileCount = Object.keys(fileContents).length; |
145 | | - await this._uploadFilesFromTmpDir(fileId, tmpDir, fileCount); |
146 | | - } |
147 | | - |
148 | | - private async _prepareTmpDirWithFiles(fileContents: Record<string, string>, tmpDir: string) { |
149 | | - await clearTmpDir(tmpDir); |
150 | | - |
151 | | - await Promise.all( |
152 | | - Object.entries(fileContents).map(([fileName, fileContent]) => |
153 | | - fs.promises.writeFile(path.join(tmpDir, fileName), fileContent), |
154 | | - ), |
155 | | - ); |
| 136 | + await uploadFilesFromTmpDir(this.page, fileId, tmpDir, fileCount); |
156 | 137 | } |
157 | 138 |
|
158 | 139 | async completeSubmission( |
@@ -229,43 +210,16 @@ export class BulkSubmissionPage extends SubmissionPage { |
229 | 210 | }); |
230 | 211 | } |
231 | 212 |
|
232 | | - /** |
233 | | - * The given file contents will be stored in a temp dir and then submitted |
234 | | - * for the given file ID. |
235 | | - * @param fileId For which file ID to upload the files. |
236 | | - * @param fileContents A struct: submissionID -> filename -> filecontent. |
237 | | - * @param tmpDir The temporary directory to use for storing files. |
238 | | - */ |
239 | 213 | async uploadExternalFiles( |
240 | 214 | fileId: string, |
241 | 215 | fileContents: Record<string, Record<string, string>>, |
242 | 216 | tmpDir: string, |
243 | 217 | ) { |
244 | | - await this._prepareTmpDirWithFiles(fileContents, tmpDir); |
| 218 | + await prepareTmpDirForBulkUpload(fileContents, tmpDir); |
245 | 219 | const fileCount = Object.values(fileContents).reduce( |
246 | 220 | (total, files) => total + Object.keys(files).length, |
247 | 221 | 0, |
248 | 222 | ); |
249 | | - await this._uploadFilesFromTmpDir(fileId, tmpDir, fileCount); |
250 | | - } |
251 | | - |
252 | | - private async _prepareTmpDirWithFiles( |
253 | | - fileContents: Record<string, Record<string, string>>, |
254 | | - tmpDir: string, |
255 | | - ) { |
256 | | - await clearTmpDir(tmpDir); |
257 | | - |
258 | | - // Create submission directories and write files |
259 | | - const submissionIds = Object.keys(fileContents); |
260 | | - await Promise.all( |
261 | | - submissionIds.map((submissionId) => fs.promises.mkdir(path.join(tmpDir, submissionId))), |
262 | | - ); |
263 | | - await Promise.all( |
264 | | - Object.entries(fileContents).flatMap(([submissionId, files]) => { |
265 | | - return Object.entries(files).map(([fileName, fileContent]) => |
266 | | - fs.promises.writeFile(path.join(tmpDir, submissionId, fileName), fileContent), |
267 | | - ); |
268 | | - }), |
269 | | - ); |
| 223 | + await uploadFilesFromTmpDir(this.page, fileId, tmpDir, fileCount); |
270 | 224 | } |
271 | 225 | } |
0 commit comments