|
| 1 | +import { test, expect, Page } from '@playwright/test' |
| 2 | +import { setupSite } from '../fixtures' |
| 3 | +import { |
| 4 | + closeFlashMessage, |
| 5 | + expectLiveViewConnected, |
| 6 | + hasFlashMessage, |
| 7 | + hasNoFlashMessage, |
| 8 | + randomID |
| 9 | +} from '../test-utils' |
| 10 | + |
| 11 | +async function createTeam(page: Page) { |
| 12 | + await page.goto('/team/setup', { waitUntil: 'commit' }) |
| 13 | + await expectLiveViewConnected(page) |
| 14 | + await page.getByRole('button', { name: 'Create team' }).click() |
| 15 | + await expect(page).toHaveURL(/\/settings\/team\/general/) |
| 16 | + await expectLiveViewConnected(page) |
| 17 | + // so that tests start without a flash |
| 18 | + await closeFlashMessage(page) |
| 19 | +} |
| 20 | + |
| 21 | +async function submitInviteForm( |
| 22 | + page: Page, |
| 23 | + { email, roleOptionName }: { email: string; roleOptionName: string | RegExp } |
| 24 | +) { |
| 25 | + await page.getByPlaceholder('Enter e-mail').fill(email) |
| 26 | + await page.locator('#input-role-picker-trigger').click() |
| 27 | + await page |
| 28 | + .locator('#input-role-picker') |
| 29 | + .getByRole('option', { name: roleOptionName }) |
| 30 | + .click() |
| 31 | + await page.getByRole('button', { name: 'Invite' }).click() |
| 32 | +} |
| 33 | + |
| 34 | +test('lowering your own role asks for confirmation', async ({ |
| 35 | + page, |
| 36 | + request |
| 37 | +}) => { |
| 38 | + const { user } = await setupSite({ page, request }) |
| 39 | + await createTeam(page) |
| 40 | + |
| 41 | + // The only owner can't change their own role, so a second (invited) owner |
| 42 | + // is needed for the self-demotion options to become selectable. |
| 43 | + const ownerEmail = `owner-${randomID()}@example.com` |
| 44 | + await submitInviteForm(page, { email: ownerEmail, roleOptionName: /^Owner/ }) |
| 45 | + await expect( |
| 46 | + page.getByRole('button', { name: `Role for ${ownerEmail}:` }) |
| 47 | + ).toContainText('Owner') |
| 48 | + |
| 49 | + const trigger = page.getByRole('button', { name: `Role for ${user.email}:` }) |
| 50 | + const options = page.getByRole('listbox', { |
| 51 | + name: `Role for ${user.email}:` |
| 52 | + }) |
| 53 | + |
| 54 | + const dialogs: string[] = [] |
| 55 | + const answerNextDialog = (accept: boolean) => |
| 56 | + page.once('dialog', async (dialog) => { |
| 57 | + dialogs.push(dialog.message()) |
| 58 | + await (accept ? dialog.accept() : dialog.dismiss()) |
| 59 | + }) |
| 60 | + |
| 61 | + await expect(trigger).toBeEnabled() |
| 62 | + await expect(trigger).toContainText('Owner') |
| 63 | + |
| 64 | + await test.step('lower your own role, but cancel the confirmation dialog (mouse)', async () => { |
| 65 | + answerNextDialog(false) |
| 66 | + |
| 67 | + await trigger.click() |
| 68 | + await options.getByRole('option', { name: /^Viewer/ }).click() |
| 69 | + |
| 70 | + expect(dialogs).toHaveLength(1) |
| 71 | + expect(dialogs[0]).toContain("You're about to lower your own role") |
| 72 | + await expect(trigger).toContainText('Owner') |
| 73 | + await hasNoFlashMessage(page) |
| 74 | + |
| 75 | + // Close the listbox that remained open because the option selection click didn't |
| 76 | + // go through -- our confirmation dialog prevented it from bubbling up to Prima. |
| 77 | + await page.keyboard.press('Escape') |
| 78 | + }) |
| 79 | + |
| 80 | + await test.step('lower your own role, but cancel the confirmation dialog (keyboard)', async () => { |
| 81 | + answerNextDialog(false) |
| 82 | + |
| 83 | + await trigger.focus() |
| 84 | + await page.keyboard.press('ArrowUp') |
| 85 | + await expect( |
| 86 | + options.getByRole('option', { name: /^Viewer/ }) |
| 87 | + ).toHaveAttribute('data-focus') |
| 88 | + await page.keyboard.press('Enter') |
| 89 | + |
| 90 | + expect(dialogs).toHaveLength(2) |
| 91 | + await expect(trigger).toContainText('Owner') |
| 92 | + await hasNoFlashMessage(page) |
| 93 | + |
| 94 | + // Close the listbox that remained open because the option selection click didn't |
| 95 | + // go through -- our confirmation dialog prevented it from bubbling up to Prima. |
| 96 | + await page.keyboard.press('Escape') |
| 97 | + }) |
| 98 | + |
| 99 | + await test.step('lower your own role and confirm (mouse)', async () => { |
| 100 | + answerNextDialog(true) |
| 101 | + |
| 102 | + await trigger.click() |
| 103 | + await options.getByRole('option', { name: /^Viewer/ }).click() |
| 104 | + |
| 105 | + // The change reaches the server, which rejects it: the other owner has only |
| 106 | + // been invited so far. |
| 107 | + await hasFlashMessage(page, 'The team has to have at least one owner') |
| 108 | + |
| 109 | + // asked once: phoenix_html must not ask again as the confirmed click bubbles up |
| 110 | + expect(dialogs).toHaveLength(3) |
| 111 | + |
| 112 | + // Known bug, to be fixed soon: the trigger keeps showing the rejected role |
| 113 | + // until the page is reloaded. Once fixed, the reload won't be necessary. |
| 114 | + await page.reload() |
| 115 | + await expectLiveViewConnected(page) |
| 116 | + |
| 117 | + await expect(trigger).toContainText('Owner') |
| 118 | + }) |
| 119 | + |
| 120 | + await test.step('lower your own role and confirm (keyboard)', async () => { |
| 121 | + answerNextDialog(true) |
| 122 | + |
| 123 | + await trigger.focus() |
| 124 | + await page.keyboard.press('ArrowUp') |
| 125 | + await expect( |
| 126 | + options.getByRole('option', { name: /^Viewer/ }) |
| 127 | + ).toHaveAttribute('data-focus') |
| 128 | + await page.keyboard.press('Enter') |
| 129 | + |
| 130 | + // The change reaches the server, which rejects it: the other owner has only |
| 131 | + // been invited so far. |
| 132 | + await hasFlashMessage(page, 'The team has to have at least one owner') |
| 133 | + |
| 134 | + // asked once: phoenix_html must not ask again as the confirmed click bubbles up |
| 135 | + expect(dialogs).toHaveLength(4) |
| 136 | + |
| 137 | + // Known bug, to be fixed soon: the trigger keeps showing the rejected role |
| 138 | + // until the page is reloaded. Once fixed, the reload won't be necessary. |
| 139 | + await page.reload() |
| 140 | + await expectLiveViewConnected(page) |
| 141 | + |
| 142 | + await expect(trigger).toContainText('Owner') |
| 143 | + }) |
| 144 | +}) |
| 145 | + |
| 146 | +test('choosing the current role again just closes the listbox', async ({ |
| 147 | + page, |
| 148 | + request |
| 149 | +}) => { |
| 150 | + await setupSite({ page, request }) |
| 151 | + await createTeam(page) |
| 152 | + |
| 153 | + const viewerEmail = `viewer-${randomID()}@example.com` |
| 154 | + await submitInviteForm(page, { |
| 155 | + email: viewerEmail, |
| 156 | + roleOptionName: /^Viewer/ |
| 157 | + }) |
| 158 | + |
| 159 | + const trigger = page.getByRole('button', { name: `Role for ${viewerEmail}:` }) |
| 160 | + const options = page.getByRole('listbox', { |
| 161 | + name: `Role for ${viewerEmail}:` |
| 162 | + }) |
| 163 | + |
| 164 | + await expect(trigger).toContainText('Viewer') |
| 165 | + |
| 166 | + const dialogs: string[] = [] |
| 167 | + page.on('dialog', async (dialog) => { |
| 168 | + dialogs.push(dialog.message()) |
| 169 | + await dialog.dismiss() |
| 170 | + }) |
| 171 | + |
| 172 | + await trigger.click() |
| 173 | + await options.getByRole('option', { name: /^Viewer/ }).click() |
| 174 | + |
| 175 | + await expect(trigger).toHaveAttribute('aria-expanded', 'false') |
| 176 | + await expect(trigger).toContainText('Viewer') |
| 177 | + expect(dialogs).toHaveLength(0) |
| 178 | + await hasNoFlashMessage(page) |
| 179 | +}) |
0 commit comments