|
| 1 | +import { expect, Locator, Page } from '@playwright/test'; |
| 2 | +import { DotCMSClazzes } from '@utils/dot-clazzes'; |
| 3 | + |
| 4 | +import { FieldsTypes, SiteorHostField, TextField } from '@models/newContentType.model'; |
| 5 | + |
| 6 | +/** |
| 7 | + * Page object for the Angular content-type builder |
| 8 | + * (`/dotAdmin/#/content-types-angular/edit/{id}`). |
| 9 | + * |
| 10 | + * Recovers the drag-and-drop "add field" logic from the legacy |
| 11 | + * `e2e/dotcms-e2e-node/.../contentTypeForm.page.ts` and adds edit + persistence |
| 12 | + * verification used to guard the `DotFieldService` (`saveFields`/`updateField`) flow. |
| 13 | + */ |
| 14 | +export class ContentTypeBuilderPage { |
| 15 | + constructor(private page: Page) {} |
| 16 | + |
| 17 | + /** A placed field card inside the drop zone, located by its display name. */ |
| 18 | + private placedField(name: string): Locator { |
| 19 | + return this.page.locator('dot-content-type-field-dragabble-item').filter({ hasText: name }); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * Open the builder for an existing content type and wait until the layout is ready |
| 24 | + * (drop zone rendered and the field palette available for dragging). |
| 25 | + */ |
| 26 | + async goToBuilder(contentTypeId: string) { |
| 27 | + await this.page.goto(`/dotAdmin/#/content-types-angular/edit/${contentTypeId}`); |
| 28 | + await this.page.getByTestId('fields-bag-0').first().waitFor({ state: 'visible' }); |
| 29 | + await this.page.getByTestId(DotCMSClazzes.TEXT).first().waitFor({ state: 'visible' }); |
| 30 | + } |
| 31 | + |
| 32 | + /** Add a field through the builder UI (drag from the palette, then fill the dialog). */ |
| 33 | + async addField(field: FieldsTypes) { |
| 34 | + switch (field.fieldType) { |
| 35 | + case 'text': |
| 36 | + await this.addTextField(field); |
| 37 | + break; |
| 38 | + case 'siteOrFolder': |
| 39 | + await this.addSiteOrFolderField(field); |
| 40 | + break; |
| 41 | + default: |
| 42 | + throw new Error(`addField does not support field type "${field.fieldType}" yet`); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + async addTextField(field: TextField) { |
| 47 | + await this.dragFieldToZone(DotCMSClazzes.TEXT); |
| 48 | + await this.fillFieldDialogAndSave(field.title); |
| 49 | + } |
| 50 | + |
| 51 | + async addSiteOrFolderField(field: SiteorHostField) { |
| 52 | + await this.dragFieldToZone(DotCMSClazzes.HOST_FOLDER); |
| 53 | + await this.fillFieldDialogAndSave(field.title); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Open an existing field's dialog (the whole card is clickable), rename it and save. |
| 58 | + * Exercises `DotFieldService.updateField` (`PUT .../fields/{id}`). |
| 59 | + */ |
| 60 | + async editField(currentName: string, newName: string) { |
| 61 | + await this.placedField(currentName).click(); |
| 62 | + await this.fillFieldDialogAndSave(newName); |
| 63 | + } |
| 64 | + |
| 65 | + async expectFieldPresent(name: string) { |
| 66 | + await expect(this.placedField(name)).toBeVisible(); |
| 67 | + } |
| 68 | + |
| 69 | + async expectFieldAbsent(name: string) { |
| 70 | + await expect(this.placedField(name)).toHaveCount(0); |
| 71 | + } |
| 72 | + |
| 73 | + /** Drag a palette item (by field clazz) onto the first drop zone. */ |
| 74 | + private async dragFieldToZone(clazz: string) { |
| 75 | + const source = this.page.getByTestId(clazz); |
| 76 | + const dropZone = this.page.getByTestId('fields-bag-0').first(); |
| 77 | + await source.waitFor({ state: 'visible' }); |
| 78 | + await source.dragTo(dropZone); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Fill the field-properties dialog name input and accept, waiting for the |
| 83 | + * fields persistence request to complete before returning. |
| 84 | + */ |
| 85 | + private async fillFieldDialogAndSave(name: string) { |
| 86 | + // Scope to the field-edit DynamicDialog so we never match an unrelated `.p-dialog`. |
| 87 | + const dialog = this.page.locator('p-dynamicdialog:has(dot-edit-field-dialog) .p-dialog'); |
| 88 | + await dialog.first().waitFor({ state: 'visible' }); |
| 89 | + |
| 90 | + const nameInput = this.page.locator('input#name'); |
| 91 | + await nameInput.waitFor({ state: 'visible' }); |
| 92 | + await nameInput.fill(name); |
| 93 | + |
| 94 | + const acceptButton = this.page.getByTestId('dotDialogAcceptAction'); |
| 95 | + await expect(acceptButton).toBeEnabled(); |
| 96 | + |
| 97 | + // Set up the wait BEFORE the click that triggers persistence (saveFields / updateField). |
| 98 | + const responsePromise = this.page.waitForResponse( |
| 99 | + (response) => |
| 100 | + /\/api\/v\d\/contenttype\/.+\/fields/.test(response.url()) && |
| 101 | + ['POST', 'PUT'].includes(response.request().method()) |
| 102 | + ); |
| 103 | + await acceptButton.click(); |
| 104 | + // Fail fast if persistence errored — otherwise a 4xx/5xx would still resolve |
| 105 | + // the wait and the test could pass without the field being saved. |
| 106 | + const response = await responsePromise; |
| 107 | + expect(response.ok()).toBeTruthy(); |
| 108 | + |
| 109 | + await expect(dialog).toBeHidden(); |
| 110 | + } |
| 111 | +} |
0 commit comments