test: the SPA's chat tests on the port, client, store and link embeds
- vitest runs app/**/*.test.ts and tests/**/*.test.ts; the SPA's chatEvents, chatFormat, chatText, chatUnread and realtime tests sit next to their ports with only import paths changed - linkEmbeds: escaping, http(s) only, rel and target, registry order - client: every endpoint, refresh-and-retry, unauthorized, 422, upload - store: bootstrap, anonymous and non-member starts, lazy joins, DMs, live messages and unread, threads, pins, reactions, slash commands, polling without realtime, onUserEvent, DM-only actions
This commit is contained in:
81
app/lib/chatUnread.test.ts
Normal file
81
app/lib/chatUnread.test.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import type { ChatMessage } from './types'
|
||||
import { badgeText, dividerMessageId, mentionLabel, shouldMarkRead, typingText, unreadLabel } from './chatUnread'
|
||||
|
||||
function message(id: number, sender: number): ChatMessage {
|
||||
return {
|
||||
id,
|
||||
conversation_id: 10,
|
||||
sender_id: sender,
|
||||
sender_name: '',
|
||||
sender_avatar: null,
|
||||
content: `m${id}`,
|
||||
type: 'text',
|
||||
attachments: [],
|
||||
created_at: '2026-10-26T10:00:00Z',
|
||||
created_at_human: '',
|
||||
edited_at: null,
|
||||
deleted_at: null,
|
||||
is_mine: sender === 1,
|
||||
root_id: null,
|
||||
}
|
||||
}
|
||||
|
||||
describe('shouldMarkRead (D-18)', () => {
|
||||
const all = { active: true, visible: true, focused: true, atBottom: true }
|
||||
it('needs the conversation active and visible, the window focused and the list at the bottom', () => {
|
||||
expect(shouldMarkRead(all)).toBe(true)
|
||||
expect(shouldMarkRead({ ...all, active: false })).toBe(false)
|
||||
expect(shouldMarkRead({ ...all, visible: false })).toBe(false)
|
||||
expect(shouldMarkRead({ ...all, focused: false })).toBe(false)
|
||||
expect(shouldMarkRead({ ...all, atBottom: false })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('badgeText', () => {
|
||||
it('hides 0, shows the number, caps at 99+', () => {
|
||||
expect(badgeText(0)).toBe('')
|
||||
expect(badgeText(-1)).toBe('')
|
||||
expect(badgeText(5)).toBe('5')
|
||||
expect(badgeText(99)).toBe('99')
|
||||
expect(badgeText(120)).toBe('99+')
|
||||
})
|
||||
})
|
||||
|
||||
describe('sr-only plurals', () => {
|
||||
it('reads unread counts in the three Polish forms', () => {
|
||||
expect(unreadLabel(1)).toBe('1 nieprzeczytana wiadomość')
|
||||
expect(unreadLabel(3)).toBe('3 nieprzeczytane wiadomości')
|
||||
expect(unreadLabel(5)).toBe('5 nieprzeczytanych wiadomości')
|
||||
expect(unreadLabel(22)).toBe('22 nieprzeczytane wiadomości')
|
||||
})
|
||||
|
||||
it('reads mention counts in the three Polish forms', () => {
|
||||
expect(mentionLabel(1)).toBe('wzmianka o Tobie')
|
||||
expect(mentionLabel(3)).toBe('3 wzmianki o Tobie')
|
||||
expect(mentionLabel(5)).toBe('5 wzmianek o Tobie')
|
||||
})
|
||||
})
|
||||
|
||||
describe('dividerMessageId', () => {
|
||||
const list = [message(1, 2), message(2, 1), message(3, 2), message(4, 1), message(5, 3)]
|
||||
it('marks the first of the last N messages from others', () => {
|
||||
expect(dividerMessageId(list, 2, 1)).toBe(3)
|
||||
expect(dividerMessageId(list, 1, 1)).toBe(5)
|
||||
})
|
||||
it('has no divider without unread messages, and stops at the oldest loaded one', () => {
|
||||
expect(dividerMessageId(list, 0, 1)).toBeNull()
|
||||
expect(dividerMessageId([], 3, 1)).toBeNull()
|
||||
expect(dividerMessageId(list, 9, 1)).toBe(1)
|
||||
expect(dividerMessageId([message(1, 1)], 2, 1)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('typingText (D-15)', () => {
|
||||
it('reads zero, one, two and many', () => {
|
||||
expect(typingText([])).toBe('')
|
||||
expect(typingText(['Ola K.'])).toBe('Ola K. pisze…')
|
||||
expect(typingText(['Ola K.', 'Jan N.'])).toBe('Ola K. i Jan N. piszą…')
|
||||
expect(typingText(['Ola K.', 'Jan N.', 'Ewa M.'])).toBe('Kilka osób pisze…')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user