mirror of
https://github.com/supabase/supabase.git
synced 2026-12-03 23:18:57 +00:00
## Problem - docs layout v2 (variant `option-1` of the `docs-ia-mvp-v1` flag) is about to roll out and a reader in the rollout has no way back to the classic layout ## Solution - adds a toggle button bottom of the guide right column so user in the rollout can switch back - adds a dialog that asks for an optional reason when a reader switches back to classic, and stores the reason in `feedback_comments` with `metadata.source = 'docs_layout_switch'` so we relying on existing - adds the `docs_layout_updated` event and stops new experiment exposures for readers who picked a layout | toggle | survey | | -------|------| | <img width="654" height="312" alt="image" src="https://github.com/user-attachments/assets/69bcd4b8-7dd2-4fd3-9045-e3246625ccf9" /> | <img width="654" height="312" alt="image" src="https://github.com/user-attachments/assets/4b18eb76-b9f1-4d96-be25-14a960b11475" /> | <!-- ## Preview links If relevant, include links to changed pages for easy review access. Copy the preview base URL from the Vercel bot comment on this PR. Use the following table as an example template. | Site | Live | Preview | Search for | | -------------- | ------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------ | ----------------------------- | | WWW | [/blog/your-post](https://supabase.com/blog/your-post) | [/blog/your-post](https://zone-www-dot-com-git-branch-name-supabase.vercel.app/blog/your-post) | unique phrase from the change | | Docs | [/docs/guides/your-page](https://supabase.com/docs/guides/your-page) | [/docs/guides/your-page](https://docs-git-branch-name-supabase.vercel.app/docs/guides/your-page) | unique phrase from the change | | Studio | [/dashboard](https://supabase.com/dashboard) | [/dashboard](https://studio-git-branch-name-supabase.vercel.app/dashboard) | unique phrase from the change | | Design system | [/design-system](https://supabase.com/design-system) | [/design-system](https://design-system-git-branch-name-supabase.vercel.app/design-system) | unique phrase from the change | | UI library | [/library](https://supabase.com/library) | [/library](https://ui-library-git-branch-name-supabase.vercel.app/library) | unique phrase from the change | | Knowledge base | [/kb/guides/your-page](https://supabase.com/kb/guides/your-page) | [/kb/guides/your-page](https://kb-git-branch-name-supabase.vercel.app/kb/guides/your-page) | unique phrase from the change | --> <!-- ## Additional context Optionally add any other context or screenshots. --> ## Review instructions 1. open [/docs/guides/database/prisma?docs-ia-mvp-v1=option-1](https://docs-git-antlio-feat-docs-layout-switch-v2-supabase.vercel.app/docs/guides/database/prisma?docs-ia-mvp-v1=option-1) 2. click "Switch to classic layout" in the notice and make sure that a dialog asks "What made you switch back?" ## Checklist Check all before review: - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
275 lines
9.1 KiB
TypeScript
275 lines
9.1 KiB
TypeScript
// @vitest-environment jsdom
|
|
import { act, renderHook } from '@testing-library/react'
|
|
import { posthogClient, useFeatureFlags, useSearchParamsShallow } from 'common'
|
|
import { LOCAL_STORAGE_KEYS } from 'common/constants/local-storage'
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { IA_NAV_FLAG, IA_NAV_FLAG_OFF, IA_NAV_OVERRIDE_KEY } from './constants'
|
|
import { IaNavRouteProvider } from './IaNavRouteProvider'
|
|
import { setLayoutChoice } from './layoutChoice'
|
|
import { useIaNavVariant } from './useIaNavVariant'
|
|
|
|
vi.mock('common', () => ({
|
|
useConsentState: vi.fn(() => ({ hasAccepted: true })),
|
|
posthogClient: { captureExperimentExposure: vi.fn() },
|
|
useFeatureFlags: vi.fn(),
|
|
useSearchParamsShallow: vi.fn(),
|
|
}))
|
|
|
|
vi.mock('next/navigation', () => ({
|
|
useSelectedLayoutSegments: vi.fn(() => ['ia', 'option-1', 'guides', 'auth']),
|
|
}))
|
|
|
|
const captureExposure = vi.mocked(posthogClient.captureExperimentExposure)
|
|
|
|
function renderVariant({
|
|
flagValue,
|
|
override,
|
|
isOverrideLoaded = true,
|
|
hasLoaded = true,
|
|
otherFlags = {},
|
|
}: {
|
|
flagValue?: string | boolean
|
|
override?: string
|
|
isOverrideLoaded?: boolean
|
|
hasLoaded?: boolean
|
|
otherFlags?: Record<string, boolean>
|
|
}) {
|
|
const posthog = flagValue === undefined ? otherFlags : { ...otherFlags, [IA_NAV_FLAG]: flagValue }
|
|
const query = new URLSearchParams(override ? { [IA_NAV_FLAG]: override } : {})
|
|
// useSearchParamsShallow starts empty and reads the URL in an effect after mount.
|
|
const searchParams = isOverrideLoaded ? query : new URLSearchParams()
|
|
|
|
window.history.replaceState(null, '', `/?${query}`)
|
|
vi.mocked(useFeatureFlags).mockReturnValue({ posthog, hasLoaded } as unknown as ReturnType<
|
|
typeof useFeatureFlags
|
|
>)
|
|
vi.mocked(useSearchParamsShallow).mockReturnValue(
|
|
searchParams as unknown as ReturnType<typeof useSearchParamsShallow>
|
|
)
|
|
|
|
return renderHook(() => useIaNavVariant())
|
|
}
|
|
|
|
describe('useIaNavVariant', () => {
|
|
beforeEach(() => {
|
|
captureExposure.mockClear()
|
|
})
|
|
|
|
afterEach(() => {
|
|
localStorage.clear()
|
|
sessionStorage.clear()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
it('keeps a forced option-1 on later pages without the query param, with no exposure', () => {
|
|
renderVariant({ flagValue: 'control', override: 'option-1' })
|
|
|
|
const { result } = renderVariant({ flagValue: 'control' })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('lets a forced control replace a forced option-1 on later pages', () => {
|
|
renderVariant({ flagValue: 'option-1', override: 'option-1' })
|
|
renderVariant({ flagValue: 'option-1', override: 'control' })
|
|
|
|
const { result } = renderVariant({ flagValue: 'option-1' })
|
|
|
|
expect(result.current).toBe('control')
|
|
})
|
|
|
|
it('returns option-1 and records its exposure', () => {
|
|
const { result } = renderVariant({ flagValue: 'option-1' })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).toHaveBeenCalledWith(IA_NAV_FLAG, { variant: 'option-1' }, true)
|
|
})
|
|
|
|
it('records control exposure for visitors in the control group', () => {
|
|
const { result } = renderVariant({ flagValue: 'control' })
|
|
|
|
expect(result.current).toBe('control')
|
|
expect(captureExposure).toHaveBeenCalledWith(IA_NAV_FLAG, { variant: 'control' }, true)
|
|
})
|
|
|
|
it('falls back to control without exposure for visitors outside the experiment', () => {
|
|
const { result } = renderVariant({ flagValue: false })
|
|
|
|
expect(result.current).toBe('control')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('records no exposure when the query param forces option-1', () => {
|
|
const { result } = renderVariant({ flagValue: 'control', override: 'option-1' })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('records no exposure before the forcing query param loads', () => {
|
|
renderVariant({ flagValue: 'control', override: 'option-1', isOverrideLoaded: false })
|
|
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('ignores an unknown query param value', () => {
|
|
const { result } = renderVariant({ flagValue: 'option-1', override: 'nope' })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).toHaveBeenCalledWith(IA_NAV_FLAG, { variant: 'option-1' }, true)
|
|
})
|
|
|
|
it.each([
|
|
['option-1', 'control'],
|
|
['control', 'option-1'],
|
|
])('keeps the session override %s when the URL value is invalid', (sessionVariant, flagValue) => {
|
|
sessionStorage.setItem(IA_NAV_OVERRIDE_KEY, sessionVariant)
|
|
|
|
const { result } = renderVariant({ flagValue, override: 'nope' })
|
|
|
|
expect(result.current).toBe(sessionVariant)
|
|
expect(sessionStorage.getItem(IA_NAV_OVERRIDE_KEY)).toBe(sessionVariant)
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('uses the cached flag while the flags load, without recording exposure', () => {
|
|
localStorage.setItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG, 'option-1')
|
|
|
|
const { result } = renderVariant({ hasLoaded: false })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('caches the flag once the flags load', () => {
|
|
renderVariant({ flagValue: false })
|
|
|
|
expect(localStorage.getItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG)).toBe('false')
|
|
})
|
|
|
|
it('keeps the cached flag without exposure when the posthog request fails', () => {
|
|
localStorage.setItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG, 'option-1')
|
|
|
|
const { result } = renderVariant({})
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(localStorage.getItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG)).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('drops the cached flag once posthog no longer has it', () => {
|
|
localStorage.setItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG, 'option-1')
|
|
|
|
const { result } = renderVariant({ otherFlags: { 'another-flag': true } })
|
|
|
|
expect(result.current).toBe('control')
|
|
expect(localStorage.getItem(LOCAL_STORAGE_KEYS.IA_NAV_FLAG)).toBe(IA_NAV_FLAG_OFF)
|
|
})
|
|
|
|
it('ignores the picked layout once the flag is off', () => {
|
|
setLayoutChoice('option-1')
|
|
|
|
const { result } = renderVariant({ otherFlags: { 'another-flag': true } })
|
|
|
|
expect(result.current).toBe('control')
|
|
})
|
|
|
|
it('keeps the picked layout when the posthog request fails', () => {
|
|
setLayoutChoice('option-1')
|
|
|
|
const { result } = renderVariant({})
|
|
|
|
expect(result.current).toBe('option-1')
|
|
})
|
|
|
|
it('keeps the layout the user picked over the flag, with no exposure', () => {
|
|
setLayoutChoice('control')
|
|
|
|
const { result } = renderVariant({ flagValue: 'option-1' })
|
|
|
|
expect(result.current).toBe('control')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('switches every reader when the user picks a layout', () => {
|
|
const { result } = renderVariant({ flagValue: 'control' })
|
|
|
|
act(() => setLayoutChoice('option-1'))
|
|
|
|
expect(result.current).toBe('option-1')
|
|
})
|
|
|
|
it('lets a picked layout replace a forced session variant', () => {
|
|
sessionStorage.setItem(IA_NAV_OVERRIDE_KEY, 'option-1')
|
|
setLayoutChoice('control')
|
|
|
|
const { result } = renderVariant({ flagValue: 'option-1' })
|
|
|
|
expect(result.current).toBe('control')
|
|
})
|
|
|
|
it('drops a session override when the user picks the layout already saved', () => {
|
|
setLayoutChoice('control')
|
|
sessionStorage.setItem(IA_NAV_OVERRIDE_KEY, 'option-1')
|
|
const { result } = renderVariant({ flagValue: 'control' })
|
|
|
|
act(() => setLayoutChoice('control'))
|
|
|
|
expect(result.current).toBe('control')
|
|
})
|
|
|
|
it('applies the picked layout when storage rejects the write', () => {
|
|
vi.spyOn(Storage.prototype, 'setItem').mockImplementation(() => {
|
|
throw new Error('QuotaExceededError')
|
|
})
|
|
const { result } = renderVariant({ flagValue: 'option-1' })
|
|
|
|
act(() => setLayoutChoice('control'))
|
|
|
|
expect(result.current).toBe('control')
|
|
})
|
|
|
|
it('lets the url override win over the picked layout', () => {
|
|
setLayoutChoice('control')
|
|
|
|
const { result } = renderVariant({ flagValue: 'control', override: 'option-1' })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
})
|
|
})
|
|
|
|
describe('useIaNavVariant on the option-1 route', () => {
|
|
beforeEach(() => {
|
|
captureExposure.mockClear()
|
|
})
|
|
|
|
it('prioritizes the route over conflicting URL and session overrides', () => {
|
|
sessionStorage.setItem(IA_NAV_OVERRIDE_KEY, 'control')
|
|
renderVariant({ flagValue: 'control', override: 'control' })
|
|
|
|
const { result } = renderHook(() => useIaNavVariant(), { wrapper: IaNavRouteProvider })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
sessionStorage.clear()
|
|
})
|
|
|
|
it('returns option-1 before the URL is read and records no exposure', () => {
|
|
vi.mocked(useFeatureFlags).mockReturnValue({
|
|
posthog: { [IA_NAV_FLAG]: 'control' },
|
|
hasLoaded: true,
|
|
} as unknown as ReturnType<typeof useFeatureFlags>)
|
|
vi.mocked(useSearchParamsShallow).mockReturnValue(
|
|
new URLSearchParams() as unknown as ReturnType<typeof useSearchParamsShallow>
|
|
)
|
|
window.history.replaceState(null, '', '/')
|
|
|
|
const { result } = renderHook(() => useIaNavVariant(), { wrapper: IaNavRouteProvider })
|
|
|
|
expect(result.current).toBe('option-1')
|
|
expect(captureExposure).not.toHaveBeenCalled()
|
|
})
|
|
})
|