| 273 | * with the resources provided in the responses argument. |
| 274 | */ |
| 275 | export const createTemplate = async ( |
| 276 | page: Page, |
| 277 | responses?: EchoProvisionerResponses | StarterTemplates, |
| 278 | orgName = defaultOrganizationName, |
| 279 | ): Promise<string> => { |
| 280 | let path = "/templates/new"; |
| 281 | if (isStarterTemplate(responses)) { |
| 282 | path += `?exampleId=${responses}`; |
| 283 | } else { |
| 284 | // The form page will read this value and use it as the default type. |
| 285 | path += "?provisioner_type=echo"; |
| 286 | } |
| 287 | |
| 288 | await page.goto(path, { waitUntil: "domcontentloaded" }); |
| 289 | await expectUrl(page).toHavePathName("/templates/new"); |
| 290 | |
| 291 | if (!isStarterTemplate(responses)) { |
| 292 | await page.getByTestId("file-upload").setInputFiles({ |
| 293 | buffer: await createTemplateVersionTar(responses), |
| 294 | mimeType: "application/x-tar", |
| 295 | name: "template.tar", |
| 296 | }); |
| 297 | // setInputFiles triggers the upload API call through React's |
| 298 | // onChange handler, but the call is fire-and-forget (not awaited |
| 299 | // in the component chain). Wait for the upload to finish so |
| 300 | // uploadedFile.hash is available when the form submits. |
| 301 | await expect( |
| 302 | page.getByRole("button", { name: "Remove file" }), |
| 303 | ).toBeVisible(); |
| 304 | } |
| 305 | |
| 306 | // If the organization picker is present on the page, select the default |
| 307 | // organization. |
| 308 | const orgPicker = page.getByLabel("Belongs to *"); |
| 309 | const organizationsEnabled = await orgPicker.isVisible(); |
| 310 | if (organizationsEnabled) { |
| 311 | if (orgName !== defaultOrganizationName) { |
| 312 | throw new Error( |
| 313 | `No provisioners registered for ${orgName}, creating this template will fail`, |
| 314 | ); |
| 315 | } |
| 316 | |
| 317 | // The organization picker will be disabled if there is only one option. |
| 318 | const pickerIsDisabled = await orgPicker.isDisabled(); |
| 319 | if (!pickerIsDisabled) { |
| 320 | await orgPicker.click(); |
| 321 | await page.getByText(orgName, { exact: true }).click(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | const name = randomName(); |
| 326 | await page.getByLabel("Name *").fill(name); |
| 327 | await page.getByRole("button", { name: /save/i }).click(); |
| 328 | await expectUrl(page).toHavePathName( |
| 329 | organizationsEnabled |
| 330 | ? `/templates/${orgName}/${name}/files` |
| 331 | : `/templates/${name}/files`, |
| 332 | { |