Files
textbee/web/lib/api/hooks.test.tsx
isra el 497a14074a refactor: centralize data fetching into typed react-query hooks
Introduce a typed data layer under lib/api (query-key factory, response
types, feature hooks) plus shared lib/format and lib/status helpers, and
migrate the canonical dashboard consumers (subscription-info, overview,
device-list, api-keys) onto it, deleting their duplicated inline queries,
mutations and formatters.

List hooks keep the raw { data: [] } envelope in the cache and unwrap
per-observer with react-query `select`, so shared keys like ['devices']
stay compatible with the not-yet-migrated components that still read the
raw shape (avoids a cache-shape collision surfaced by the dashboard e2e).

Adds unit tests for the formatters and the hooks (against MSW). Build,
19 unit tests, and 2 e2e all green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-11 01:36:16 +03:00

31 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'
import { TestProviders } from '@/test/render'
import { mockDevices, mockSubscription, mockUser } from '@/test/fixtures'
import { useCurrentUser, useDevices, useSubscription } from './hooks'
// Verifies the typed hooks talk to the mocked API and unwrap the various
// response envelopes correctly. No real backend is contacted (MSW).
const wrapper = TestProviders
describe('data hooks', () => {
it('useCurrentUser unwraps res.data.data', async () => {
const { result } = renderHook(() => useCurrentUser(), { wrapper })
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data?.email).toBe(mockUser.email)
})
it('useSubscription returns the raw body', async () => {
const { result } = renderHook(() => useSubscription(), { wrapper })
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data?.plan?.name).toBe(mockSubscription.plan.name)
})
it('useDevices unwraps to the device array', async () => {
const { result } = renderHook(() => useDevices(), { wrapper })
await waitFor(() => expect(result.current.isSuccess).toBe(true))
expect(result.current.data).toHaveLength(mockDevices.length)
expect(result.current.data?.[0]._id).toBe(mockDevices[0]._id)
})
})