import { describe, expect, it } from 'vitest' import { QueryClient } from '@tanstack/react-query' import { postsKeys, tagsKeys, wikiKeys } from '@/lib/queryKeys' import type { FetchNicoTagsParams } from '@/types' describe ('query keys', () => { it ('uses stable namespaces for posts, tags, and wiki', () => { expect (postsKeys.show ('3')).toEqual (['posts', 'ja', '3']) expect (postsKeys.related ('3')).toEqual (['related', 'ja', '3']) expect (tagsKeys.deerjikists ('7')).toEqual (['tags', 'ja', 'deerjikists', '7']) expect (wikiKeys.show ('Title', { version: '2' })).toEqual ( ['wiki', 'ja', 'Title', { version: '2' }]) }) it ('isolates the same resource by viewing locale', () => { const client = new QueryClient () client.setQueryData (postsKeys.show ('3', 'ja'), { title: '日本語' }) client.setQueryData (postsKeys.show ('3', 'ain-Kana'), { title: 'アイヌ語' }) expect (client.getQueryData (postsKeys.show ('3', 'ja'))).toEqual ({ title: '日本語' }) expect (client.getQueryData (postsKeys.show ('3', 'ain-Kana'))) .toEqual ({ title: 'アイヌ語' }) client.clear () }) it ('invalidates both nico locales without invalidating ordinary tags', async () => { const client = new QueryClient () const params: FetchNicoTagsParams = { name: '', linkedTag: '', linkStatus: 'all', page: 1, limit: 20, order: 'name:asc' } const ja = tagsKeys.nicoIndex (params, 'ja') const ain = tagsKeys.nicoIndex (params, 'ain-Kana') const ordinary = tagsKeys.show ('3', 'ja') for (const key of [ja, ain, ordinary]) client.setQueryData (key, []) await client.invalidateQueries ({ queryKey: tagsKeys.nicoRoot }) expect (client.getQueryState (ja)?.isInvalidated).toBe (true) expect (client.getQueryState (ain)?.isInvalidated).toBe (true) expect (client.getQueryState (ordinary)?.isInvalidated).toBe (false) client.clear () }) })