このコミットが含まれているのは:
2026-10-25 14:14:55 +09:00
コミット 304090f717
16個のファイルの変更、745行の追加、27行の削除
+83
ファイルの表示
@@ -0,0 +1,83 @@
import { fireEvent, screen, waitFor } from '@testing-library/react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import CountsPage from '@/pages/CountsPage'
import CountsResourcePage from '@/pages/CountsResourcePage'
import { renderWithProviders } from '@/test/render'
import type { CountsResult } from '@/lib/counts'
const { fetchCounts } = vi.hoisted (() => ({ fetchCounts: vi.fn () }))
vi.mock ('@/lib/counts', async importOriginal => ({
...await importOriginal<typeof import ('@/lib/counts')> (), fetchCounts }))
beforeEach (() => {
fetchCounts.mockReset ()
fetchCounts.mockResolvedValue ({ count: 0, groupCount: 0, groups: [], page: 1, limit: 20 })
})
describe ('counts pages', () => {
it ('links to the two locale-aware resource screens', () => {
renderWithProviders (<CountsPage/>, { route: '/ja/counts' })
expect (screen.getByRole ('link', { name: '投稿' }))
.toHaveAttribute ('href', '/ja/counts/posts')
expect (screen.getByRole ('link', { name: 'タグ' }))
.toHaveAttribute ('href', '/ja/counts/tags')
})
it ('restores filters and selected columns and preserves them in page links', async () => {
fetchCounts.mockResolvedValue ({
count: 21, page: 1, limit: 20,
versions: [{ postId: 7, versionNo: 2,
url: { current: 'https://example.com' },
title: { current: 'Snapshot' } }] as CountsResult['versions'] })
renderWithProviders (<CountsResourcePage resource="posts"/>, {
route: '/ja/counts/posts?mode=details&columns=title&user_id=3' })
expect (await screen.findByRole ('cell', { name: 'Snapshot' })).toBeInTheDocument ()
expect (screen.getByRole ('checkbox', { name: '投稿タイトル' })).toBeChecked ()
expect (screen.getByRole ('checkbox', { name: '投稿 URL' })).not.toBeChecked ()
expect (screen.getByLabelText ('次のページ')).toHaveAttribute (
'href', '/ja/counts/posts?mode=details&columns=title&user_id=3&page=2')
expect (fetchCounts.mock.calls[0][1].get ('user_id')).toBe ('3')
fireEvent.click (screen.getByRole ('checkbox', { name: '投稿 URL' }))
await waitFor (() => {
expect (fetchCounts.mock.lastCall?.[1].get ('columns')).toBe ('title,url')
})
})
it ('executes combined filters and resets the page', async () => {
renderWithProviders (<CountsResourcePage resource="posts"/>, {
route: '/ja/counts/posts?page=3&mode=details' })
fireEvent.change (screen.getByRole ('textbox', { name: '投稿 ID' }), {
target: { value: '7' } })
fireEvent.change (screen.getByRole ('textbox', { name: '操作者 ID' }), {
target: { value: '9' } })
fireEvent.submit (screen.getByRole ('button', { name: '実行' }).closest ('form')!)
await waitFor (() => {
const params = fetchCounts.mock.lastCall?.[1] as URLSearchParams
expect (params.get ('resource_id')).toBe ('7')
expect (params.get ('user_id')).toBe ('9')
expect (params.has ('page')).toBe (false)
})
const calls = fetchCounts.mock.calls.length
fireEvent.submit (screen.getByRole ('button', { name: '実行' }).closest ('form')!)
await waitFor (() => expect (fetchCounts.mock.calls.length).toBeGreaterThan (calls))
})
it ('keeps a partial-month date range when drilling down into that month', async () => {
fetchCounts.mockResolvedValue ({ count: 4, groupCount: 1, page: 1, limit: 20,
groups: [{ key: '2024-01', name: null, count: 4 }] })
renderWithProviders (<CountsResourcePage resource="tags"/>, {
route: '/ja/counts/tags?axis=month&from=2024-01-15T00:00Z&event_type=update' })
const link = await screen.findByRole ('link', { name: '2024-01' })
const url = new URL (link.getAttribute ('href')!, 'http://localhost')
expect (url.searchParams.get ('from')).toBe ('2024-01-15T00:00Z')
expect (url.searchParams.get ('before')).toBe ('2024-02-01T00:00Z')
expect (url.searchParams.get ('event_type')).toBe ('update')
expect (url.searchParams.get ('mode')).toBe ('details')
})
})