-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(e2e): Tests for Keyless mode (#5046)
- Loading branch information
1 parent
e6df88a
commit a75a1f7
Showing
7 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { Browser, BrowserContext } from '@playwright/test'; | ||
|
||
import type { createAppPageObject } from './appPageObject'; | ||
|
||
export type EnchancedPage = ReturnType<typeof createAppPageObject>; | ||
export type TestArgs = { page: EnchancedPage; context: BrowserContext; browser: Browser }; | ||
|
||
export const createKeylessPopoverPageObject = (testArgs: TestArgs) => { | ||
const { page } = testArgs; | ||
// TODO: Is this the ID we really want ? | ||
const elementId = '#--clerk-keyless-prompt-button'; | ||
const self = { | ||
waitForMounted: () => page.waitForSelector(elementId, { state: 'attached' }), | ||
waitForUnmounted: () => page.waitForSelector(elementId, { state: 'detached' }), | ||
isExpanded: () => | ||
page | ||
.locator(elementId) | ||
.getAttribute('aria-expanded') | ||
.then(val => val === 'true'), | ||
toggle: () => page.locator(elementId).click(), | ||
|
||
promptsToClaim: () => { | ||
return page.getByRole('link', { name: /^claim application$/i }); | ||
}, | ||
promptToUseClaimedKeys: () => { | ||
return page.getByRole('link', { name: /^get api keys$/i }); | ||
}, | ||
promptToDismiss: () => { | ||
return page.getByRole('button', { name: /^dismiss$/i }); | ||
}, | ||
}; | ||
return self; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import type { Page } from '@playwright/test'; | ||
import { expect, test } from '@playwright/test'; | ||
|
||
import type { Application } from '../models/application'; | ||
import { appConfigs } from '../presets'; | ||
import { createTestUtils } from '../testUtils'; | ||
|
||
const commonSetup = appConfigs.next.appRouterQuickstart.clone(); | ||
|
||
const mockClaimedInstanceEnvironmentCall = async (page: Page) => { | ||
await page.route('*/**/v1/environment*', async route => { | ||
const response = await route.fetch(); | ||
const json = await response.json(); | ||
const newJson = { | ||
...json, | ||
auth_config: { | ||
...json.auth_config, | ||
claimed_at: Date.now(), | ||
}, | ||
}; | ||
await route.fulfill({ response, json: newJson }); | ||
}); | ||
}; | ||
|
||
test.describe('Keyless mode @quickstart', () => { | ||
test.describe.configure({ mode: 'serial' }); | ||
let app: Application; | ||
|
||
test.beforeAll(async () => { | ||
app = await commonSetup.commit(); | ||
await app.setup(); | ||
await app.withEnv(appConfigs.envs.withKeyless); | ||
await app.dev(); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await app.teardown(); | ||
}); | ||
|
||
test('Toggle collapse popover and claim.', async ({ page, context }) => { | ||
const u = createTestUtils({ app, page, context }); | ||
await u.page.goToAppHome(); | ||
await u.page.waitForClerkJsLoaded(); | ||
await u.po.expect.toBeSignedOut(); | ||
|
||
await u.po.keylessPopover.waitForMounted(); | ||
|
||
expect(await u.po.keylessPopover.isExpanded()).toBe(false); | ||
await u.po.keylessPopover.toggle(); | ||
expect(await u.po.keylessPopover.isExpanded()).toBe(true); | ||
|
||
const claim = await u.po.keylessPopover.promptsToClaim(); | ||
|
||
const [newPage] = await Promise.all([context.waitForEvent('page'), claim.click()]); | ||
|
||
await newPage.waitForLoadState(); | ||
await newPage.waitForURL(url => { | ||
const urlToReturnTo = 'https://dashboard.clerk.com/apps/claim?token='; | ||
return ( | ||
url.pathname === '/apps/claim/sign-in' && | ||
url.searchParams.get('sign_in_force_redirect_url')?.startsWith(urlToReturnTo) && | ||
url.searchParams.get('sign_up_force_redirect_url')?.startsWith(urlToReturnTo) | ||
); | ||
}); | ||
}); | ||
|
||
test('Lands on claimed application with missing explicit keys, expanded by default, click to get keys from dashboard.', async ({ | ||
page, | ||
context, | ||
}) => { | ||
await mockClaimedInstanceEnvironmentCall(page); | ||
const u = createTestUtils({ app, page, context }); | ||
await u.page.goToAppHome(); | ||
await u.page.waitForClerkJsLoaded(); | ||
|
||
await u.po.keylessPopover.waitForMounted(); | ||
expect(await u.po.keylessPopover.isExpanded()).toBe(true); | ||
await expect(u.po.keylessPopover.promptToUseClaimedKeys()).toBeVisible(); | ||
|
||
const [newPage] = await Promise.all([ | ||
context.waitForEvent('page'), | ||
u.po.keylessPopover.promptToUseClaimedKeys().click(), | ||
]); | ||
|
||
await newPage.waitForLoadState(); | ||
await newPage.waitForURL(url => | ||
url.href.startsWith( | ||
'https://dashboard.clerk.com/sign-in?redirect_url=https%3A%2F%2Fdashboard.clerk.com%2Fapps%2Fapp_', | ||
), | ||
); | ||
}); | ||
|
||
test('Claimed application with keys inside .env, on dismiss, keyless prompt is removed.', async ({ | ||
page, | ||
context, | ||
}) => { | ||
await mockClaimedInstanceEnvironmentCall(page); | ||
const u = createTestUtils({ app, page, context }); | ||
await u.page.goToAppHome(); | ||
|
||
await u.po.keylessPopover.waitForMounted(); | ||
await expect(await u.po.keylessPopover.promptToUseClaimedKeys()).toBeVisible(); | ||
|
||
/** | ||
* Copy keys from `.clerk/.tmp/keyless.json to `.env` | ||
*/ | ||
await app.keylessToEnv(); | ||
/** | ||
* wait a bit for the server to load the new env file | ||
*/ | ||
await page.waitForTimeout(5_000); | ||
|
||
await page.reload(); | ||
await u.po.keylessPopover.waitForMounted(); | ||
await u.po.keylessPopover.promptToDismiss().click(); | ||
|
||
await u.po.keylessPopover.waitForUnmounted(); | ||
}); | ||
}); |