|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act, type ChangeEventHandler, type ReactNode } from 'react' |
| 5 | +import { emcnMock } from '@sim/testing/mocks/emcn.mock' |
| 6 | +import { NuqsTestingAdapter } from 'nuqs/adapters/testing' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { mockGenerate } = vi.hoisted(() => ({ mockGenerate: vi.fn() })) |
| 11 | + |
| 12 | +vi.mock('@sim/emcn', () => ({ |
| 13 | + ...emcnMock, |
| 14 | + Badge: ({ children }: { children?: ReactNode }) => <span>{children}</span>, |
| 15 | + Button: ({ children, ...props }: { children?: ReactNode }) => ( |
| 16 | + <button type='button' {...props}> |
| 17 | + {children} |
| 18 | + </button> |
| 19 | + ), |
| 20 | + ChipCopyInput: ({ value }: { value?: string }) => ( |
| 21 | + <input data-testid='license-key' readOnly value={value ?? ''} /> |
| 22 | + ), |
| 23 | + ChipInput: ({ |
| 24 | + value, |
| 25 | + onChange, |
| 26 | + placeholder, |
| 27 | + }: { |
| 28 | + value?: string |
| 29 | + onChange?: ChangeEventHandler<HTMLInputElement> |
| 30 | + placeholder?: string |
| 31 | + }) => <input placeholder={placeholder} value={value ?? ''} onChange={onChange} />, |
| 32 | + ChipModalTabs: () => <div />, |
| 33 | + ChipSelect: () => <div />, |
| 34 | + Label: ({ children }: { children?: ReactNode }) => <span>{children}</span>, |
| 35 | + Skeleton: () => <div />, |
| 36 | +})) |
| 37 | + |
| 38 | +vi.mock('@/app/workspace/[workspaceId]/settings/components/settings-panel', () => ({ |
| 39 | + SettingsPanel: ({ children }: { children?: ReactNode }) => <div>{children}</div>, |
| 40 | +})) |
| 41 | + |
| 42 | +vi.mock('@/app/workspace/[workspaceId]/settings/components/settings-empty-state', () => ({ |
| 43 | + SettingsEmptyState: () => null, |
| 44 | +})) |
| 45 | + |
| 46 | +vi.mock('@/hooks/queries/mothership-admin', () => ({ |
| 47 | + useGenerateLicense: () => ({ mutate: mockGenerate, isPending: false, error: null }), |
| 48 | + useMothershipLicenses: () => ({ data: undefined, isLoading: false }), |
| 49 | + useMothershipRequests: () => ({ data: undefined, isLoading: false }), |
| 50 | + useMothershipUserBreakdown: () => ({ data: undefined, isLoading: false }), |
| 51 | +})) |
| 52 | + |
| 53 | +import { Mothership } from '@/app/workspace/[workspaceId]/settings/components/mothership/mothership' |
| 54 | +import { useSettingsDirtyStore } from '@/stores/settings/dirty/store' |
| 55 | + |
| 56 | +let container: HTMLDivElement |
| 57 | +let root: Root |
| 58 | + |
| 59 | +beforeEach(() => { |
| 60 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 61 | + useSettingsDirtyStore.getState().reset() |
| 62 | + mockGenerate.mockImplementation( |
| 63 | + (_input: unknown, options: { onSuccess: (result: { license_key: string }) => void }) => |
| 64 | + options.onSuccess({ license_key: 'sim_license_once' }) |
| 65 | + ) |
| 66 | + container = document.createElement('div') |
| 67 | + document.body.appendChild(container) |
| 68 | + root = createRoot(container) |
| 69 | + act(() => |
| 70 | + root.render( |
| 71 | + <NuqsTestingAdapter searchParams='?tab=licenses'> |
| 72 | + <Mothership /> |
| 73 | + </NuqsTestingAdapter> |
| 74 | + ) |
| 75 | + ) |
| 76 | +}) |
| 77 | + |
| 78 | +afterEach(() => { |
| 79 | + act(() => root.unmount()) |
| 80 | + container.remove() |
| 81 | +}) |
| 82 | + |
| 83 | +function type(placeholder: string, value: string) { |
| 84 | + const input = container.querySelector<HTMLInputElement>(`input[placeholder="${placeholder}"]`) |
| 85 | + expect(input).not.toBeNull() |
| 86 | + act(() => { |
| 87 | + Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value')?.set?.call( |
| 88 | + input, |
| 89 | + value |
| 90 | + ) |
| 91 | + input?.dispatchEvent(new Event('input', { bubbles: true })) |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +function generateKey() { |
| 96 | + type('e.g. Acme Corp', 'Acme') |
| 97 | + type('Signed order form or written approval', 'order-1') |
| 98 | + const generate = Array.from(container.querySelectorAll('button')).find( |
| 99 | + (button) => !button.disabled |
| 100 | + ) |
| 101 | + act(() => generate?.click()) |
| 102 | +} |
| 103 | + |
| 104 | +function licenseKey() { |
| 105 | + return container.querySelector<HTMLInputElement>('[data-testid="license-key"]')?.value |
| 106 | +} |
| 107 | + |
| 108 | +describe('Mothership license generation', () => { |
| 109 | + it('asks before leaving while the shown-once license key is on screen', () => { |
| 110 | + generateKey() |
| 111 | + const leave = vi.fn() |
| 112 | + |
| 113 | + const left = useSettingsDirtyStore.getState().requestLeave(leave) |
| 114 | + |
| 115 | + expect(licenseKey()).toBe('sim_license_once') |
| 116 | + expect(left).toBe(false) |
| 117 | + expect(leave).not.toHaveBeenCalled() |
| 118 | + }) |
| 119 | + |
| 120 | + it('drops the license key when the admin confirms leaving', () => { |
| 121 | + generateKey() |
| 122 | + const leave = vi.fn() |
| 123 | + useSettingsDirtyStore.getState().requestLeave(leave) |
| 124 | + |
| 125 | + act(() => useSettingsDirtyStore.getState().confirmLeave()) |
| 126 | + |
| 127 | + expect(leave).toHaveBeenCalledOnce() |
| 128 | + expect(licenseKey()).toBeUndefined() |
| 129 | + expect(useSettingsDirtyStore.getState().isDirty).toBe(false) |
| 130 | + }) |
| 131 | +}) |
0 commit comments