import { afterEach, expect, it, vi } from 'vitest' import { assetsApi } from '../src/api/assets' afterEach(() => vi.unstubAllGlobals()) it('uses the company search API and submits only company_id when following a company', async () => { const fetchMock = vi.fn() .mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({ data: [{ company_id: 'isin:CNE100001526', name: 'BYD Company Limited', exchange: 'Shenzhen Stock Exchange', country: 'China', mic: 'XSHE' }] }) }) .mockResolvedValueOnce({ ok: true, status: 201, json: async () => ({ data: { id: 'asset-1', company_id: 'isin:CNE100001526' } }) }) vi.stubGlobal('fetch', fetchMock) expect(await assetsApi.search('BYD', 20)).toEqual([{ company_id: 'isin:CNE100001526', name: 'BYD Company Limited', exchange: 'Shenzhen Stock Exchange', country: 'China', mic: 'XSHE' }]) await assetsApi.add({ company_id: 'isin:CNE100001526' }) expect.soft(fetchMock.mock.calls[0][0]).toBe('/api/companies/search?q=BYD&limit=20') expect(fetchMock).toHaveBeenNthCalledWith(2, '/api/me/assets', expect.objectContaining({ method: 'POST', body: JSON.stringify({ company_id: 'isin:CNE100001526' }), })) })