// The count is invisible until it is known, and visible only for the // publication it was asked about. `load` never touches the DOM — it redraws // through the host — so this exercises the real card with a stand-in element. import { beforeEach, describe, expect, it, vi } from 'vitest' import type { PageState } from '../../lib/types' import type { CardHost } from './card' const documentCount = vi.fn() /** The one element the card owns. */ const { countEl } = vi.hoisted(() => ({ countEl: { hidden: true, textContent: '' } })) vi.mock('../../lib/subscribers', () => ({ documentCount: (...a: unknown[]) => documentCount(...a), })) vi.mock('../dom', () => ({ $: () => countEl })) const { postsCard } = await import('./posts') const PUB = 'at://did:plc:pub00000000000000000000/site.standard.publication/self' const OTHER = 'at://did:plc:other0000000000000000/site.standard.publication/self' function hostFor(pub?: { uri: string; handle?: string }): CardHost { return { state: () => pub ? ({ url: 'https://pub.example', fetchedAt: 0, pub } as unknown as PageState) : undefined, siteAccount: () => undefined, session: () => undefined, rerender: () => {}, loading: () => false, } } beforeEach(() => { postsCard.reset?.() documentCount.mockReset() countEl.hidden = true countEl.textContent = '' }) describe('the posts card', () => { it('asks for nothing when there is no publication', async () => { await postsCard.load?.(hostFor(), false) expect(documentCount).not.toHaveBeenCalled() }) it('asks once per publication, passing the handle form along', async () => { documentCount.mockResolvedValue(12) const host = hostFor({ uri: PUB, handle: 'pub.example' }) await postsCard.load?.(host, false) await postsCard.load?.(host, false) expect(documentCount).toHaveBeenCalledOnce() expect(documentCount).toHaveBeenCalledWith(PUB, 'pub.example') }) it('draws a plural count after the name', async () => { documentCount.mockResolvedValue(12) const host = hostFor({ uri: PUB }) await postsCard.load?.(host, false) postsCard.render(host) expect(countEl.textContent).toBe('12 posts') expect(countEl.hidden).toBe(false) }) it('says "post" in the singular', async () => { documentCount.mockResolvedValue(1) const host = hostFor({ uri: PUB }) await postsCard.load?.(host, false) postsCard.render(host) expect(countEl.textContent).toBe('1 post') expect(countEl.hidden).toBe(false) }) it('stays hidden for zero and for an unknown count', async () => { const host = hostFor({ uri: PUB }) for (const answer of [0, null]) { documentCount.mockResolvedValue(answer) postsCard.reset?.() await postsCard.load?.(host, false) postsCard.render(host) expect(countEl.hidden, String(answer)).toBe(true) expect(countEl.textContent, String(answer)).toBe('') } }) it('never draws a count that belongs to another publication', async () => { documentCount.mockResolvedValue(9) await postsCard.load?.(hostFor({ uri: PUB }), false) postsCard.render(hostFor({ uri: OTHER })) expect(countEl.hidden).toBe(true) expect(countEl.textContent).toBe('') }) it('forgets the count on reset', async () => { documentCount.mockResolvedValue(9) const host = hostFor({ uri: PUB }) await postsCard.load?.(host, false) postsCard.reset?.() postsCard.render(host) expect(countEl.hidden).toBe(true) }) })