const { test, expect, settings } = require('./helpers/fixtures'); /** * Tests for cms.forms.widgets.UserSelectAdminWidget. * * The widget is rendered inside the PagePermissionInlineAdmin on the page * advanced-settings view when CMS_PERMISSION = True. It adds an "add another" * link next to the user (same id stem, without the `add_` prefix) must be // attached to the DOM. Use an attribute selector to avoid needing // CSS.escape() (a browser-only global not available in Node). const selectId = linkId.replace(/^add_/, ''); await expect(page.locator(`[id="${selectId}"]`)).toBeAttached(); }); test('clicking the add link opens a popup to cms_pageuser_add', async ({ page, cms, context }) => { const pageId = await getTestPageId(page, cms); test.skip(!pageId, 'Could not provision a CMS page for the test'); await page.goto( `${settings.baseUrl}/en/admin/cms/page/${pageId}/advanced-settings/`, { waitUntil: 'domcontentloaded' } ); const pagePermissionInline = page .locator('div.inline-group, fieldset.module') .filter({ has: page.locator('h2, .inline-heading', { hasText: /page permissions/i }) }) .first(); // Clone the __prefix__ template into a real, interactive row by clicking // Django admin's "Add another Page permission" button (rendered by // admin/js/inlines.js as `.add-row a`). await pagePermissionInline.locator('.add-row a').first().click(); // The cloned row has an add-related link whose id no longer contains // __prefix__ and whose href still points at cms_pageuser_add. const addLink = pagePermissionInline .locator('a.related-widget-wrapper-link.add-related[href*="/cms/pageuser/add/"]') .filter({ hasNot: page.locator('[id*="__prefix__"]') }) .first(); await expect(addLink).toBeAttached(); // Django's RelatedObjectLookups.js attaches its click listener to // document.body via event delegation (`.related-widget-wrapper-link`). // The cloned row's parent / may be visually hidden by the // tabular-inline layout on this wide permissions table, so we dispatch a // click event directly on the element. The delegated handler on body // still fires and calls showRelatedObjectPopup(this), which opens the // popup window via window.open(). const popupPromise = context.waitForEvent('page'); await addLink.dispatchEvent('click'); const popup = await popupPromise; await popup.waitForLoadState('domcontentloaded'); expect(popup.url()).toMatch(/\/admin\/cms\/pageuser\/add\/\?.*_popup=1/); await expect( popup.locator('form#pageuser_form, form[action*="pageuser/add"]').first() ).toBeVisible(); await popup.close(); }); });