Skip to content

Commit 210ff35

Browse files
committed
test(mothership): assert the license key guard through tab and environment navigation
1 parent 7aa719f commit 210ff35

1 file changed

Lines changed: 61 additions & 14 deletions

File tree

‎apps/sim/app/workspace/[workspaceId]/settings/components/mothership/mothership.test.tsx‎

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,46 @@ vi.mock('@sim/emcn', () => ({
2929
onChange?: ChangeEventHandler<HTMLInputElement>
3030
placeholder?: string
3131
}) => <input placeholder={placeholder} value={value ?? ''} onChange={onChange} />,
32-
ChipModalTabs: () => <div />,
33-
ChipSelect: () => <div />,
32+
ChipModalTabs: ({
33+
tabs,
34+
onChange,
35+
}: {
36+
tabs: Array<{ label: string; value: string }>
37+
onChange: (value: string) => void
38+
}) => (
39+
<div>
40+
{tabs.map((tab) => (
41+
<button
42+
key={tab.value}
43+
type='button'
44+
data-tab={tab.value}
45+
onClick={() => onChange(tab.value)}
46+
>
47+
{tab.label}
48+
</button>
49+
))}
50+
</div>
51+
),
52+
ChipSelect: ({
53+
options,
54+
onChange,
55+
}: {
56+
options: Array<{ label: string; value: string }>
57+
onChange: (value: string) => void
58+
}) => (
59+
<div>
60+
{options.map((option) => (
61+
<button
62+
key={option.value}
63+
type='button'
64+
data-env={option.value}
65+
onClick={() => onChange(option.value)}
66+
>
67+
{option.label}
68+
</button>
69+
))}
70+
</div>
71+
),
3472
Label: ({ children }: { children?: ReactNode }) => <span>{children}</span>,
3573
Skeleton: () => <div />,
3674
}))
@@ -55,6 +93,7 @@ import { useSettingsDirtyStore } from '@/stores/settings/dirty/store'
5593

5694
let container: HTMLDivElement
5795
let root: Root
96+
let urlSearch: string
5897

5998
beforeEach(() => {
6099
;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true
@@ -66,9 +105,15 @@ beforeEach(() => {
66105
container = document.createElement('div')
67106
document.body.appendChild(container)
68107
root = createRoot(container)
108+
urlSearch = '?tab=licenses'
69109
act(() =>
70110
root.render(
71-
<NuqsTestingAdapter searchParams='?tab=licenses'>
111+
<NuqsTestingAdapter
112+
searchParams={urlSearch}
113+
onUrlUpdate={(event) => {
114+
urlSearch = event.queryString
115+
}}
116+
>
72117
<Mothership />
73118
</NuqsTestingAdapter>
74119
)
@@ -96,7 +141,7 @@ function generateKey() {
96141
type('e.g. Acme Corp', 'Acme')
97142
type('Signed order form or written approval', 'order-1')
98143
const generate = Array.from(container.querySelectorAll('button')).find(
99-
(button) => !button.disabled
144+
(button) => !button.dataset.tab && !button.dataset.env && !button.disabled
100145
)
101146
act(() => generate?.click())
102147
}
@@ -105,26 +150,28 @@ function licenseKey() {
105150
return container.querySelector<HTMLInputElement>('[data-testid="license-key"]')?.value
106151
}
107152

153+
function switchToByok() {
154+
act(() => container.querySelector<HTMLButtonElement>('[data-tab="byok"]')?.click())
155+
}
156+
108157
describe('Mothership license generation', () => {
109-
it('asks before leaving while the shown-once license key is on screen', () => {
158+
it('stays on the tab while the shown-once license key is on screen', () => {
110159
generateKey()
111-
const leave = vi.fn()
112160

113-
const left = useSettingsDirtyStore.getState().requestLeave(leave)
161+
switchToByok()
114162

115163
expect(licenseKey()).toBe('sim_license_once')
116-
expect(left).toBe(false)
117-
expect(leave).not.toHaveBeenCalled()
164+
expect(urlSearch).not.toContain('byok')
165+
expect(useSettingsDirtyStore.getState().pendingLeave).not.toBeNull()
118166
})
119167

120-
it('drops the license key when the admin confirms leaving', () => {
168+
it('drops the license key when the admin confirms switching environments', async () => {
121169
generateKey()
122-
const leave = vi.fn()
123-
useSettingsDirtyStore.getState().requestLeave(leave)
170+
act(() => container.querySelector<HTMLButtonElement>('[data-env="prod"]')?.click())
124171

125-
act(() => useSettingsDirtyStore.getState().confirmLeave())
172+
await act(async () => useSettingsDirtyStore.getState().confirmLeave())
126173

127-
expect(leave).toHaveBeenCalledOnce()
174+
await vi.waitFor(() => expect(urlSearch).toContain('env=prod'))
128175
expect(licenseKey()).toBeUndefined()
129176
expect(useSettingsDirtyStore.getState().isDirty).toBe(false)
130177
})

0 commit comments

Comments
 (0)