Skip to content

Commit 83eeb56

Browse files
authored
chore(e2e): Tests for validating billing hooks behaviour (#7161)
1 parent a474c59 commit 83eeb56

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use client';
2+
import { usePlans, useSubscription, useStatements } from '@clerk/nextjs/experimental';
3+
4+
export default function Home() {
5+
const { data: plans, count: planCount } = usePlans();
6+
const { data: subscription } = useSubscription();
7+
const { data: statements, count: statementCount } = useStatements();
8+
return (
9+
<main>
10+
{plans?.map(plan => (
11+
<div key={plan.id}>
12+
<h2>Plan: {plan.name}</h2>
13+
<p>{plan.description}</p>
14+
</div>
15+
))}
16+
17+
{planCount > 0 ? <p>Plans found</p> : <p>No plans found</p>}
18+
19+
{statements?.map(statement => (
20+
<div key={statement.id}>
21+
<p>Statement total: {statement.totals.grandTotal.amountFormatted}</p>
22+
</div>
23+
))}
24+
25+
{statementCount > 0 ? <p>Statements found</p> : <p>No statements found</p>}
26+
27+
{subscription ? (
28+
<div>
29+
<h2>Subscribed to {subscription.subscriptionItems[0].plan.name}</h2>
30+
</div>
31+
) : (
32+
<p>No subscription found</p>
33+
)}
34+
</main>
35+
);
36+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { expect, test } from '@playwright/test';
2+
3+
import { appConfigs } from '../presets';
4+
import type { FakeUser } from '../testUtils';
5+
import { createTestUtils, testAgainstRunningApps } from '../testUtils';
6+
7+
testAgainstRunningApps({ withEnv: [appConfigs.envs.withBilling] })('billing hooks @billing', ({ app }) => {
8+
test.describe.configure({ mode: 'parallel' });
9+
test.skip(!app.name.includes('next'), 'Skipping: Only runs on next');
10+
11+
let fakeUser: FakeUser;
12+
13+
test.beforeAll(async () => {
14+
const u = createTestUtils({ app });
15+
fakeUser = u.services.users.createFakeUser();
16+
await u.services.users.createBapiUser(fakeUser);
17+
});
18+
19+
test.afterAll(async () => {
20+
await fakeUser.deleteIfExists();
21+
await app.teardown();
22+
});
23+
24+
test.describe('when signed out', () => {
25+
test('renders billing hooks with plans, but no statements and no subscription', async ({ page, context }) => {
26+
const u = createTestUtils({ app, page, context });
27+
await u.po.page.goToRelative('/billing/hooks');
28+
29+
await u.po.page.waitForClerkJsLoaded();
30+
31+
await expect(u.po.page.getByText('Plans found')).toBeVisible();
32+
await expect(u.po.page.getByRole('heading', { name: 'Plan: Pro' })).toBeVisible();
33+
await expect(u.po.page.getByText('No statements found')).toBeVisible();
34+
await expect(u.po.page.getByText('No subscription found')).toBeVisible();
35+
});
36+
});
37+
38+
test.describe('when signed in', () => {
39+
test.describe.configure({ mode: 'serial' });
40+
test('subscribes to a plan', async ({ page, context }) => {
41+
const u = createTestUtils({ app, page, context });
42+
await u.po.signIn.goTo();
43+
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });
44+
await u.po.page.goToRelative('/pricing-table?newSubscriptionRedirectUrl=/pricing-table');
45+
await u.po.pricingTable.waitForMounted();
46+
await u.po.pricingTable.startCheckout({ planSlug: 'plus' });
47+
await u.po.checkout.waitForMounted();
48+
await u.po.checkout.fillTestCard();
49+
await u.po.checkout.clickPayOrSubscribe();
50+
await expect(u.po.checkout.root.getByText('Payment was successful!')).toBeVisible();
51+
await u.po.checkout.confirmAndContinue();
52+
});
53+
54+
test('renders billing hooks with plans, statements and subscription', async ({ page, context }) => {
55+
const u = createTestUtils({ app, page, context });
56+
await u.po.signIn.goTo();
57+
await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeUser.email, password: fakeUser.password });
58+
await u.po.page.goToRelative('/billing/hooks');
59+
60+
await u.po.page.waitForClerkJsLoaded();
61+
62+
await expect(u.po.page.getByText('Plans found')).toBeVisible();
63+
await expect(u.po.page.getByRole('heading', { name: 'Plan: Pro' })).toBeVisible();
64+
65+
await expect(u.po.page.getByText('Statements found')).toBeVisible();
66+
await expect(u.po.page.getByText('Statement total: 99.96')).toBeVisible();
67+
68+
await expect(u.po.page.getByRole('heading', { name: 'Subscribed to Plus' })).toBeVisible();
69+
70+
await u.page.evaluate(async () => {
71+
await window.Clerk.signOut({
72+
redirectUrl: '/billing/hooks',
73+
});
74+
});
75+
76+
await expect(u.po.page.getByText('Plans found')).toBeVisible();
77+
await expect(u.po.page.getByRole('heading', { name: 'Plan: Pro' })).toBeVisible();
78+
await expect(u.po.page.getByText('No statements found')).toBeVisible();
79+
await expect(u.po.page.getByText('No subscription found')).toBeVisible();
80+
81+
await expect(u.po.page.getByRole('heading', { name: 'Subscribed to Plus' })).toBeHidden();
82+
await expect(u.po.page.getByText('Statements found', { exact: true })).toBeHidden();
83+
await expect(u.po.page.getByText('Statement total: 99.96', { exact: true })).toBeHidden();
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)