Files
SubTracker/apps/web/tests/unit/utils/subscription-table.test.ts
SmileQWQ 34b59ebe4b feat: refine statistics, subscriptions, and deployment docs
- add monthly subscription spend TOP10 to statistics overview and UI

- improve subscriptions pagination with per-page persistence and note-row handling

- make login validation messages friendlier on both client and server

- clarify API-only upgrade flow for static web assets

- move web unit tests into apps/web/tests/unit
2026-04-19 21:52:20 +08:00

64 lines
1.7 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import type { Subscription } from '../../../src/types/api'
import { buildSubscriptionTableRows, paginateSubscriptions } from '../../../src/utils/subscription-table'
function createSubscription(id: string, overrides: Partial<Subscription> = {}): Subscription {
return {
id,
name: `sub-${id}`,
description: '',
websiteUrl: '',
logoUrl: '',
logoSource: '',
logoFetchedAt: '',
status: 'active',
amount: 10,
currency: 'CNY',
billingIntervalCount: 1,
billingIntervalUnit: 'month',
autoRenew: true,
startDate: '2026-01-01',
nextRenewalDate: '2026-02-01',
notifyDaysBefore: 3,
webhookEnabled: true,
notes: '',
tags: [],
createdAt: '2026-01-01T00:00:00.000Z',
updatedAt: '2026-01-01T00:00:00.000Z',
...overrides
}
}
describe('subscription-table utils', () => {
it('builds a note row immediately after the main row', () => {
const rows = buildSubscriptionTableRows([
createSubscription('a', { notes: ' 备注 A ' }),
createSubscription('b')
])
expect(rows.map((item) => item.id)).toEqual(['a', 'a__note', 'b'])
expect(rows[1]).toMatchObject({
__rowType: 'note',
note: '备注 A',
subscriptionId: 'a'
})
})
it('paginates subscriptions before note rows are expanded', () => {
const pageItems = paginateSubscriptions(
[
createSubscription('a', { notes: '备注 A' }),
createSubscription('b'),
createSubscription('c')
],
1,
2
)
const rows = buildSubscriptionTableRows(pageItems)
expect(pageItems.map((item) => item.id)).toEqual(['a', 'b'])
expect(rows.map((item) => item.id)).toEqual(['a', 'a__note', 'b'])
})
})