|
| 1 | +import { readFileSync } from 'fs'; |
| 2 | +import { join } from 'path'; |
| 3 | +import { createSSRApp } from 'vue'; |
| 4 | +import { renderToString } from '@vue/server-renderer'; |
| 5 | + |
| 6 | +const templateOf = (file: string): string => { |
| 7 | + const src = readFileSync(join(__dirname, file), 'utf8'); |
| 8 | + const m = src.match(/<template>([\s\S]*)<\/template>\s*<script/); |
| 9 | + return m ? m[1] : ''; |
| 10 | +}; |
| 11 | + |
| 12 | +const badge = { |
| 13 | + template: templateOf('../../CommentCountBadge.vue'), |
| 14 | + props: ['count'], |
| 15 | + components: { MessageCircle: { template: '<i />' } }, |
| 16 | +}; |
| 17 | + |
| 18 | +const entry = (over: Record<string, unknown>) => ({ |
| 19 | + task_id: 1, |
| 20 | + date: '2026-10-12', |
| 21 | + title: 'Stretch', |
| 22 | + time: '08:00', |
| 23 | + completed: false, |
| 24 | + routine_category: { color: '#ff0000' }, |
| 25 | + ...over, |
| 26 | +}); |
| 27 | + |
| 28 | +const render = async (entries: unknown[]) => { |
| 29 | + const app = createSSRApp({ |
| 30 | + template: templateOf('../MonthView.vue'), |
| 31 | + components: { CommentCountBadge: badge }, |
| 32 | + setup: () => ({ |
| 33 | + dowLabels: ['M'], |
| 34 | + days: [new Date(2026, 9, 12)], |
| 35 | + byDay: [entries], |
| 36 | + maxChips: 3, |
| 37 | + hoverKey: '', |
| 38 | + today: new Date(2026, 9, 12), |
| 39 | + fmtIso: () => '2026-10-12', |
| 40 | + isSameDay: () => false, |
| 41 | + hexAlpha: () => '#fff', |
| 42 | + cellStyle: () => ({}), |
| 43 | + dayLabelStyle: () => ({}), |
| 44 | + onCellClick: () => undefined, |
| 45 | + }), |
| 46 | + }); |
| 47 | + app.config.globalProperties.$t = (_k: string, p?: { n: number }) => |
| 48 | + `${p?.n} comments`; |
| 49 | + return renderToString(app); |
| 50 | +}; |
| 51 | + |
| 52 | +describe('MonthView comment badge', () => { |
| 53 | + it('shows the count when comments_count > 0', async () => { |
| 54 | + const html = await render([entry({ comments_count: 3 })]); |
| 55 | + expect(html).toContain('data-testid="routine-comment-count"'); |
| 56 | + expect(html).toContain('>3<'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders nothing when comments_count is 0 or missing', async () => { |
| 60 | + expect(await render([entry({ comments_count: 0 })])).not.toContain( |
| 61 | + 'routine-comment-count', |
| 62 | + ); |
| 63 | + expect(await render([entry({})])).not.toContain('routine-comment-count'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments