78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
/** @jest-environment node */
|
|
|
|
import { NextRequest } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
|
|
import { GET } from '@/app/api/search/suggestions/route'
|
|
import { prisma } from '@/lib/prisma'
|
|
import {
|
|
getUserViewableMaterialTypes,
|
|
isSuperAdmin,
|
|
isSystemAdmin,
|
|
} from '@/lib/department-permission'
|
|
|
|
jest.mock('next-auth', () => ({
|
|
getServerSession: jest.fn(),
|
|
}))
|
|
|
|
jest.mock('@/lib/auth', () => ({
|
|
authOptions: {},
|
|
}))
|
|
|
|
jest.mock('@/lib/prisma', () => ({
|
|
prisma: {
|
|
material: {
|
|
findMany: jest.fn(),
|
|
},
|
|
materialSku: {
|
|
findMany: jest.fn(),
|
|
},
|
|
user: {
|
|
findMany: jest.fn(),
|
|
},
|
|
},
|
|
}))
|
|
|
|
jest.mock('@/lib/department-permission', () => ({
|
|
getUserViewableMaterialTypes: jest.fn(),
|
|
isSuperAdmin: jest.fn(),
|
|
isSystemAdmin: jest.fn(),
|
|
}))
|
|
|
|
describe('search suggestions special init visibility', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
;(getServerSession as jest.Mock).mockResolvedValue({
|
|
user: {
|
|
id: 'user-1',
|
|
role: 'USER',
|
|
department: '设计部',
|
|
},
|
|
})
|
|
;(getUserViewableMaterialTypes as jest.Mock).mockResolvedValue([
|
|
'任务-页面',
|
|
'任务-详情',
|
|
])
|
|
;(isSuperAdmin as jest.Mock).mockResolvedValue(false)
|
|
;(isSystemAdmin as unknown as jest.Mock).mockReturnValue(false)
|
|
;(prisma.material.findMany as jest.Mock).mockResolvedValue([])
|
|
;(prisma.materialSku.findMany as jest.Mock).mockResolvedValue([])
|
|
;(prisma.user.findMany as jest.Mock).mockResolvedValue([])
|
|
})
|
|
|
|
test('SPU、作者和场景建议都复用普通列表的空壳隐藏条件', async () => {
|
|
const response = await GET(
|
|
new NextRequest('http://localhost/api/search/suggestions?q=SPU'),
|
|
)
|
|
|
|
expect(response.status).toBe(200)
|
|
expect(prisma.material.findMany).toHaveBeenCalledTimes(3)
|
|
for (const [query] of (prisma.material.findMany as jest.Mock).mock.calls) {
|
|
expect(query.where.AND[0]).toEqual(expect.objectContaining({
|
|
deletedAt: null,
|
|
isIncompleteShell: false,
|
|
}))
|
|
}
|
|
})
|
|
})
|