|
| 1 | +import { fireEvent, render, screen } from '@testing-library/svelte'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | + |
| 4 | +import type { ViewOrganization } from '#features/organizations/models.ts'; |
| 5 | +import type { BillingPlan } from '#generated/api.ts'; |
| 6 | + |
| 7 | +const plans: BillingPlan[] = [ |
| 8 | + { |
| 9 | + description: 'Free', |
| 10 | + has_premium_features: false, |
| 11 | + id: 'EX_FREE', |
| 12 | + is_hidden: false, |
| 13 | + max_events_per_month: 3000, |
| 14 | + max_projects: -1, |
| 15 | + max_users: -1, |
| 16 | + name: 'Free', |
| 17 | + price: 0, |
| 18 | + retention_days: 3 |
| 19 | + }, |
| 20 | + { |
| 21 | + description: 'Small', |
| 22 | + has_premium_features: true, |
| 23 | + id: 'EX_SMALL', |
| 24 | + is_hidden: false, |
| 25 | + max_events_per_month: 15000, |
| 26 | + max_projects: -1, |
| 27 | + max_users: -1, |
| 28 | + name: 'Small', |
| 29 | + price: 15, |
| 30 | + retention_days: 30 |
| 31 | + }, |
| 32 | + { |
| 33 | + description: 'Small yearly', |
| 34 | + has_premium_features: true, |
| 35 | + id: 'EX_SMALL_YEARLY', |
| 36 | + is_hidden: false, |
| 37 | + max_events_per_month: 15000, |
| 38 | + max_projects: -1, |
| 39 | + max_users: -1, |
| 40 | + name: 'Small (Yearly)', |
| 41 | + price: 165, |
| 42 | + retention_days: 30 |
| 43 | + }, |
| 44 | + { |
| 45 | + description: 'Medium', |
| 46 | + has_premium_features: true, |
| 47 | + id: 'EX_MEDIUM', |
| 48 | + is_hidden: false, |
| 49 | + max_events_per_month: 75000, |
| 50 | + max_projects: -1, |
| 51 | + max_users: -1, |
| 52 | + name: 'Medium', |
| 53 | + price: 49, |
| 54 | + retention_days: 90 |
| 55 | + } |
| 56 | +]; |
| 57 | + |
| 58 | +vi.mock('#features/organizations/api.svelte.ts', () => ({ |
| 59 | + changePlanMutation: () => ({ mutateAsync: vi.fn() }), |
| 60 | + getPlansQuery: () => ({ data: plans, error: null, isLoading: false }) |
| 61 | +})); |
| 62 | +vi.mock('#features/billing/stripe.svelte.ts', async (importOriginal) => ({ |
| 63 | + ...(await importOriginal<typeof import('#features/billing/stripe.svelte.ts')>()), |
| 64 | + isStripeEnabled: () => true, |
| 65 | + loadStripeOnce: async () => null |
| 66 | +})); |
| 67 | + |
| 68 | +import ChangePlanDialog from './change-plan-dialog.svelte'; |
| 69 | + |
| 70 | +describe('ChangePlanDialog loss warning', () => { |
| 71 | + it('warns for trial to Small in both intervals without blocking confirmation, then clears for Medium', async () => { |
| 72 | + // Arrange |
| 73 | + const organization = { |
| 74 | + billing_price: 0, |
| 75 | + card_last4: '4242', |
| 76 | + has_premium_features: true, |
| 77 | + id: 'trial-organization', |
| 78 | + max_events_per_month: 75000, |
| 79 | + name: 'Trial organization', |
| 80 | + plan_id: 'EX_TRIAL', |
| 81 | + plan_name: 'Trial', |
| 82 | + retention_days: 90, |
| 83 | + trial_ends_utc: '2099-01-01T00:00:00Z' |
| 84 | + } as ViewOrganization; |
| 85 | + |
| 86 | + // Act |
| 87 | + render(ChangePlanDialog, { initialPlanId: 'EX_SMALL', onclose: vi.fn(), organization }); |
| 88 | + |
| 89 | + // Assert |
| 90 | + const losses = await screen.findByTestId('downgrade-losses'); |
| 91 | + expect(losses).toHaveTextContent('event history drops from 90 days to 30 days'); |
| 92 | + expect(losses).toHaveTextContent('limit drops to 15,000 events a month'); |
| 93 | + expect(screen.getByRole('button', { name: 'Start Small yearly' })).toBeEnabled(); |
| 94 | + |
| 95 | + // Act |
| 96 | + await fireEvent.click(screen.getByRole('tab', { name: 'Monthly' })); |
| 97 | + |
| 98 | + // Assert |
| 99 | + expect(losses).toHaveTextContent('event history drops from 90 days to 30 days'); |
| 100 | + expect(screen.getByRole('button', { name: 'Start Small monthly' })).toBeEnabled(); |
| 101 | + |
| 102 | + // Act |
| 103 | + await fireEvent.click(screen.getByRole('button', { name: /^Medium/ })); |
| 104 | + |
| 105 | + // Assert |
| 106 | + expect(screen.queryByTestId('downgrade-losses')).toBeNull(); |
| 107 | + expect(screen.getByRole('button', { name: 'Start Medium monthly' })).toBeEnabled(); |
| 108 | + }); |
| 109 | +}); |
0 commit comments