Files
gestion-materiaux-destock/src/app/api/creation/metafields/route.test.ts
T

38 lines
1.7 KiB
TypeScript

import { GET } from './route'
import * as shopifyMetafields from '@/lib/shopifyMetafields'
import { getServerSession } from 'next-auth'
jest.mock('next-auth')
jest.mock('@/lib/shopifyMetafields')
const mockSession = getServerSession as jest.MockedFunction<typeof getServerSession>
const mockFetchDefs = shopifyMetafields.fetchMetafieldDefinitions as jest.MockedFunction<typeof shopifyMetafields.fetchMetafieldDefinitions>
const mockMatch = shopifyMetafields.matchMetafields as jest.MockedFunction<typeof shopifyMetafields.matchMetafields>
describe('GET /api/creation/metafields', () => {
beforeEach(() => {
mockSession.mockResolvedValue({ user: { id: 'u1', role: 'admin' } } as never)
mockFetchDefs.mockResolvedValue([
{ id: '1', namespace: 'custom', key: 'puissance', name: 'Puissance', type: 'single_line_text_field' },
])
mockMatch.mockReturnValue({
matched: { 'Puissance (AN)': { columnHeader: 'Puissance (AN)', action: 'map', namespace: 'custom', key: 'puissance', existingDefinitionId: '1' } },
unmatched: ['Batterie (AP)'],
})
})
it('retourne 401 sans session', async () => {
mockSession.mockResolvedValue(null)
const res = await GET(new Request('http://localhost/api/creation/metafields?columns=Puissance') as never)
expect(res.status).toBe(401)
})
it('retourne définitions + matching', async () => {
const res = await GET(new Request('http://localhost/api/creation/metafields?columns=Puissance+%28AN%29%2CBatterie+%28AP%29') as never)
const body = await res.json()
expect(body.definitions).toHaveLength(1)
expect(body.matched['Puissance (AN)'].key).toBe('puissance')
expect(body.unmatched).toContain('Batterie (AP)')
})
})