Extract admin dashboard from ghabilee-frontend2 into a dedicated Next.js app for backoffice.ghabilee.ir (no SEO indexing / Clarity).
76 lines
2.2 KiB
TypeScript
76 lines
2.2 KiB
TypeScript
import { describe, expect, it } from 'vitest'
|
|
|
|
import { extractExportFileData, getItemsKeyFromUrl, parseRemittanceList, unwrapApiPayload } from '@/helpers/listResponse'
|
|
|
|
describe('list response normalization', () => {
|
|
it('unwraps API envelopes and preserves flat arrays', () => {
|
|
expect(unwrapApiPayload({ success: true, data: { items: [1] } })).toEqual({ items: [1] })
|
|
expect(unwrapApiPayload({ success: true, data: [1, 2] })).toEqual([1, 2])
|
|
expect(unwrapApiPayload(null)).toEqual({})
|
|
})
|
|
|
|
it('normalizes current and legacy pagination fields', () => {
|
|
const result = parseRemittanceList<{ id: number }>(
|
|
{
|
|
success: true,
|
|
data: {
|
|
users: [{ id: 1 }],
|
|
meta: { page: '2', limit: '10', total: '21', total_page: '3' },
|
|
},
|
|
},
|
|
'users'
|
|
)
|
|
|
|
expect(result.items).toEqual([{ id: 1 }])
|
|
expect(result.pagination).toMatchObject({
|
|
page: 2,
|
|
pageSize: 10,
|
|
totalItemsCount: 21,
|
|
totalPages: 3,
|
|
})
|
|
})
|
|
|
|
it('normalizes response metadata, fallbacks and optional export fields', () => {
|
|
const result = parseRemittanceList(
|
|
{
|
|
items: ['fallback-item'],
|
|
response: {
|
|
page: 'invalid',
|
|
pageSize: 5,
|
|
totalItemsCount: 11,
|
|
filters: { status: 'active', empty: null },
|
|
sort: '-createdAt',
|
|
resultType: '2',
|
|
fileData: 'encoded',
|
|
},
|
|
},
|
|
'missing',
|
|
3,
|
|
25
|
|
)
|
|
|
|
expect(result).toEqual({
|
|
items: ['fallback-item'],
|
|
pagination: {
|
|
page: 3,
|
|
pageSize: 5,
|
|
totalItemsCount: 11,
|
|
totalPages: 3,
|
|
filters: { status: 'active', empty: 'null' },
|
|
sort: '-createdAt',
|
|
resultType: 2,
|
|
fileData: 'encoded',
|
|
},
|
|
})
|
|
expect(parseRemittanceList('invalid').items).toEqual([])
|
|
})
|
|
|
|
it('derives list keys and extracts non-empty exports', () => {
|
|
expect(getItemsKeyFromUrl('/api/v1/users/?page=2')).toBe('users')
|
|
expect(getItemsKeyFromUrl('/')).toBe('items')
|
|
expect(extractExportFileData({ response: { fileData: 'base64' } })).toBe('base64')
|
|
expect(extractExportFileData({ response: { fileData: '' } })).toBeUndefined()
|
|
expect(extractExportFileData({ response: null })).toBeUndefined()
|
|
})
|
|
})
|