|
| 1 | +/** |
| 2 | + * The billing page's list of token packs: what Payment Terms 5.4 promises the customer can read. The date printed is |
| 3 | + * the API's `validUntil`, never one worked out from `expiresAt`, which is a day later (CU-17tkuw61pae). |
| 4 | + */ |
| 5 | +import { act } from 'react'; |
| 6 | +import { createRoot, type Root } from 'react-dom/client'; |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 8 | +import type { purchasedTokenPacks_subscription$key } from '@/__generated__/purchasedTokenPacks_subscription.graphql'; |
| 9 | +import { EMPTY_VALUE } from '@/lib/empty-value'; |
| 10 | +import { PurchasedTokenPacks } from './purchased-token-packs'; |
| 11 | + |
| 12 | +// The `graphql` tag needs the Relay transform, which vitest has no equivalent of; |
| 13 | +// a fragment read hands back the record it was given. |
| 14 | +vi.mock('react-relay', () => ({ |
| 15 | + graphql: () => ({}), |
| 16 | + useFragment: (_fragment: unknown, ref: unknown) => ref, |
| 17 | +})); |
| 18 | + |
| 19 | +const OLDER = { |
| 20 | + purchasedAt: '2026-10-28T14:35:00Z', |
| 21 | + tokensPurchased: 5_000_000, |
| 22 | + tokensRemaining: 1_200_000, |
| 23 | + validUntil: '2027-10-28', |
| 24 | + // Selected by nothing: a printed date taken from here would be a day late. |
| 25 | + expiresAt: '2027-10-29T00:00:00Z', |
| 26 | +}; |
| 27 | +const NEWER = { |
| 28 | + purchasedAt: '2026-10-30T09:00:00Z', |
| 29 | + tokensPurchased: 1_000_000, |
| 30 | + tokensRemaining: 1_000_000, |
| 31 | + validUntil: '2027-10-30', |
| 32 | + expiresAt: '2027-10-31T00:00:00Z', |
| 33 | +}; |
| 34 | + |
| 35 | +const subscription = (purchasedTokenPacks: unknown[]) => |
| 36 | + ({ usage: { purchasedTokenPacks } }) as unknown as purchasedTokenPacks_subscription$key; |
| 37 | + |
| 38 | +let container: HTMLDivElement; |
| 39 | +let root: Root; |
| 40 | + |
| 41 | +function render(ref: purchasedTokenPacks_subscription$key) { |
| 42 | + act(() => { |
| 43 | + root.render(<PurchasedTokenPacks subscription={ref} />); |
| 44 | + }); |
| 45 | + return [...container.querySelectorAll('li')].map(row => row.textContent ?? ''); |
| 46 | +} |
| 47 | + |
| 48 | +beforeEach(() => { |
| 49 | + (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true; |
| 50 | + container = document.createElement('div'); |
| 51 | + root = createRoot(container); |
| 52 | +}); |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + act(() => root.unmount()); |
| 56 | +}); |
| 57 | + |
| 58 | +describe('PurchasedTokenPacks', () => { |
| 59 | + it("states each pack's tokens, purchase date and the API's last valid day", () => { |
| 60 | + const [row] = render(subscription([OLDER])); |
| 61 | + |
| 62 | + expect(row).toContain('1.2M / 5M'); |
| 63 | + expect(row).toContain('Purchased 2026-10-28'); |
| 64 | + expect(row).toContain('Valid until 2027-10-28'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('keeps the order the API sent, the order tokens are spent in', () => { |
| 68 | + const rows = render(subscription([OLDER, NEWER])); |
| 69 | + |
| 70 | + expect(rows.map(row => /Valid until (\S+?)Purchased/.exec(row)?.[1])).toEqual(['2027-10-28', '2027-10-30']); |
| 71 | + }); |
| 72 | + |
| 73 | + it('prints no date for a pack that never expires', () => { |
| 74 | + const [row] = render(subscription([{ ...OLDER, validUntil: null, expiresAt: null }])); |
| 75 | + |
| 76 | + expect(row).not.toContain('Valid until'); |
| 77 | + expect(row).toContain(EMPTY_VALUE); |
| 78 | + }); |
| 79 | + |
| 80 | + it('draws nothing when there are no packs', () => { |
| 81 | + render(subscription([])); |
| 82 | + |
| 83 | + expect(container.innerHTML).toBe(''); |
| 84 | + }); |
| 85 | +}); |
0 commit comments