Skip to content

Commit

Permalink
test(e2e): Group duplicated code on AP tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anagstef committed Mar 19, 2024
1 parent eaba239 commit 11d5f5c
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 627 deletions.
163 changes: 6 additions & 157 deletions integration/tests/next-account-portal/clerk-v4-ap-core-1.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, test } from '@playwright/test';
import { test } from '@playwright/test';

import type { Application } from '../../models/application';
import { appConfigs } from '../../presets';
import type { FakeUser } from '../../testUtils';
import { createTestUtils } from '../../testUtils';
import { CLERK_DB_JWT_COOKIE_NAME, CLERK_SESSION_COOKIE_NAME } from './common';
import { testSignIn, testSignUp, testSSR } from './common';

test.describe('Next with ClerkJS V4 <-> Account Portal Core 1 @ap-flows', () => {
test.describe.configure({ mode: 'serial' });
Expand All @@ -17,7 +17,7 @@ test.describe('Next with ClerkJS V4 <-> Account Portal Core 1 @ap-flows', () =>
await app.withEnv(appConfigs.envs.withAPCore1ClerkV4);
await app.dev();
const u = createTestUtils({ app });
fakeUser = u.services.users.createFakeUser({ fictionalEmail: true });
fakeUser = u.services.users.createFakeUser();
await u.services.users.createBapiUser(fakeUser);
});

Expand All @@ -27,165 +27,14 @@ test.describe('Next with ClerkJS V4 <-> Account Portal Core 1 @ap-flows', () =>
});

test('sign in', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Get the Initial DevBrowser JWT
const initialDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign in/i }).click();
await u.po.signIn.waitForMounted();

// Check that the DevBrowser JWT between localhost and AP is the same
const accountPortalURL = page.url();
// Check that we are in Account Portal
expect(accountPortalURL).toContain('.accounts.dev');
const accountPortalDbJwt = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);
expect(accountPortalDbJwt).toEqual(initialDbJwt);

// Sign in with email and password
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });

// Navigate back to localhost
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

// Get the new DevBrowser JWT that was set after signing in the Account Portal
const appDbJwtAfterSignIn = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the new DevBrowser JWT
const newLocalhostDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the __session cookie
const __session = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_SESSION_COOKIE_NAME)?.value);

// Check that the new localhost DevBrowser JWT is the same as the one set after signing in the Account Portal
// and not the same as the initial DevBrowser JWT
expect(newLocalhostDbJwt).toEqual(appDbJwtAfterSignIn);
expect(newLocalhostDbJwt).not.toEqual(initialDbJwt);

// Check that the __session cookie is set
expect(!!__session).toBeTruthy();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();

// cleanup the search params after consuming the dev browser jwt
const finalURL = new URL(u.page.url());
expect(finalURL.searchParams.size).toEqual(0);
await testSignIn({ app, page, context, fakeUser });
});

test('sign up', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const fakeUser = u.services.users.createFakeUser({ fictionalEmail: true });

// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Get the Initial DevBrowser JWT
const initialDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign up/i }).click();
await u.po.signUp.waitForMounted();

// Check that the DevBrowser JWT between localhost and AP is the same
const accountPortalURL = page.url();
// Check that we are in Account Portal
expect(accountPortalURL).toContain('.accounts.dev');
const accountPortalDbJwt = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);
expect(accountPortalDbJwt).toEqual(initialDbJwt);

// Sign up with email and password
await u.po.signUp.signUpWithEmailAndPassword({ email: fakeUser.email, password: fakeUser.password });
await u.po.signUp.enterOtpCode('424242');

// Navigate back to localhost
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

// Get the new DevBrowser JWT that was set after signing in the Account Portal
const appDbJwtAfterSignIn = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the new DevBrowser JWT
const newLocalhostDbJwt = await context
.cookies(u.page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the __session cookie
const __session = await context
.cookies(u.page.url())
.then(cookies => cookies.find(c => c.name === CLERK_SESSION_COOKIE_NAME)?.value);

// Check that the new localhost DevBrowser JWT is the same as the one set after signing in the Account Portal
// and not the same as the initial DevBrowser JWT
expect(newLocalhostDbJwt).toEqual(appDbJwtAfterSignIn);
expect(newLocalhostDbJwt).not.toEqual(initialDbJwt);

// Check that the __session cookie is set
expect(!!__session).toBeTruthy();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();

// cleanup the search params after consuming the dev browser jwt
const finalURL = new URL(u.page.url());
expect(finalURL.searchParams.size).toEqual(0);

// cleanup
await fakeUser.deleteIfExists();
await testSignUp({ app, page, context, fakeUser });
});

test('ssr', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });

// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign in/i }).click();
await u.po.signIn.waitForMounted();

// Sign in with email and password
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });

// Navigate back to localhost
const response = await page.waitForResponse(
response =>
response.url().includes('localhost') &&
response.status() === 200 &&
response.request().resourceType() === 'document',
);

// This text is included in the SSR response because it's wrapped inside the SignedIn component
expect(await response.text()).toContain('signed-in-state');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();
await testSSR({ app, page, context, fakeUser });
});
});
161 changes: 5 additions & 156 deletions integration/tests/next-account-portal/clerk-v4-ap-core-2.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { expect, test } from '@playwright/test';
import { test } from '@playwright/test';

import type { Application } from '../../models/application';
import { appConfigs } from '../../presets';
import type { FakeUser } from '../../testUtils';
import { createTestUtils } from '../../testUtils';
import { CLERK_DB_JWT_COOKIE_NAME, CLERK_SESSION_COOKIE_NAME } from './common';
import { testSignIn, testSignUp, testSSR } from './common';

test.describe('Next with ClerkJS V4 <-> Account Portal Core 2 @ap-flows', () => {
test.describe.configure({ mode: 'serial' });
Expand All @@ -27,165 +27,14 @@ test.describe('Next with ClerkJS V4 <-> Account Portal Core 2 @ap-flows', () =>
});

test('sign in', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Get the Initial DevBrowser JWT
const initialDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign in/i }).click();
await u.po.signIn.waitForMounted();

// Check that the DevBrowser JWT between localhost and AP is the same
const accountPortalURL = page.url();
// Check that we are in Account Portal
expect(accountPortalURL).toContain('.accounts.dev');
const accountPortalDbJwt = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);
expect(accountPortalDbJwt).toEqual(initialDbJwt);

// Sign in with email and password
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });

// Navigate back to localhost
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

// Get the new DevBrowser JWT that was set after signing in the Account Portal
const appDbJwtAfterSignIn = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the new DevBrowser JWT
const newLocalhostDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the __session cookie
const __session = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_SESSION_COOKIE_NAME)?.value);

// Check that the new localhost DevBrowser JWT is the same as the one set after signing in the Account Portal
// and not the same as the initial DevBrowser JWT
expect(newLocalhostDbJwt).toEqual(appDbJwtAfterSignIn);
expect(newLocalhostDbJwt).not.toEqual(initialDbJwt);

// Check that the __session cookie is set
expect(!!__session).toBeTruthy();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();

// cleanup the search params after consuming the dev browser jwt
const finalURL = new URL(u.page.url());
expect(finalURL.searchParams.size).toEqual(0);
await testSignIn({ app, page, context, fakeUser });
});

test('sign up', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });
const fakeUser = u.services.users.createFakeUser({ fictionalEmail: true });

// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Get the Initial DevBrowser JWT
const initialDbJwt = await context
.cookies(page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign up/i }).click();
await u.po.signUp.waitForMounted();

// Check that the DevBrowser JWT between localhost and AP is the same
const accountPortalURL = page.url();
// Check that we are in Account Portal
expect(accountPortalURL).toContain('.accounts.dev');
const accountPortalDbJwt = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);
expect(accountPortalDbJwt).toEqual(initialDbJwt);

// Sign up with email and password
await u.po.signUp.signUpWithEmailAndPassword({ email: fakeUser.email, password: fakeUser.password });
await u.po.signUp.enterOtpCode('424242');

// Navigate back to localhost
await u.page.waitForAppUrl('/');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

// Get the new DevBrowser JWT that was set after signing in the Account Portal
const appDbJwtAfterSignIn = await context
.cookies(accountPortalURL)
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the new DevBrowser JWT
const newLocalhostDbJwt = await context
.cookies(u.page.url())
.then(cookies => cookies.find(c => c.name === CLERK_DB_JWT_COOKIE_NAME)?.value);

// Get the __session cookie
const __session = await context
.cookies(u.page.url())
.then(cookies => cookies.find(c => c.name === CLERK_SESSION_COOKIE_NAME)?.value);

// Check that the new localhost DevBrowser JWT is the same as the one set after signing in the Account Portal
// and not the same as the initial DevBrowser JWT
expect(newLocalhostDbJwt).toEqual(appDbJwtAfterSignIn);
expect(newLocalhostDbJwt).not.toEqual(initialDbJwt);

// Check that the __session cookie is set
expect(!!__session).toBeTruthy();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();

// cleanup the search params after consuming the dev browser jwt
const finalURL = new URL(u.page.url());
expect(finalURL.searchParams.size).toEqual(0);

// cleanup
await fakeUser.deleteIfExists();
await testSignUp({ app, page, context, fakeUser });
});

test('ssr', async ({ page, context }) => {
const u = createTestUtils({ app, page, context });

// Begin in localhost
await u.page.goToStart();
await u.page.waitForClerkJsLoaded();
await u.po.expect.toBeSignedOut();

// Navigate to the Account Portal
await u.page.getByRole('button', { name: /Sign in/i }).click();
await u.po.signIn.waitForMounted();

// Sign in with email and password
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });

// Navigate back to localhost
const response = await page.waitForResponse(
response =>
response.url().includes('localhost') &&
response.status() === 200 &&
response.request().resourceType() === 'document',
);

// This text is included in the SSR response because it's wrapped inside the SignedIn component
expect(await response.text()).toContain('signed-in-state');
await u.po.expect.toBeSignedIn();
await u.po.userButton.waitForMounted();

await expect(u.page.getByRole('button', { name: /Open user button/i })).toBeVisible();
await testSSR({ app, page, context, fakeUser });
});
});
Loading

0 comments on commit 11d5f5c

Please sign in to comment.