|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectstack#29147 — below `sm`, every Home launcher link and button meets the |
| 5 | + * 44 px touch target; at `sm` and up each keeps its desktop size. |
| 6 | + * |
| 7 | + * What these cases pin, on the real `HomePage` and the real strip and rail it |
| 8 | + * renders: each target carries its phone class (`max-sm:h-11`, or |
| 9 | + * `max-sm:min-h-11` where the target has no fixed height) beside its unchanged |
| 10 | + * desktop class. A narrow text link also carries `max-sm:min-w-11`, so a short |
| 11 | + * label ("View all") is never a target narrower than 44 px. |
| 12 | + * |
| 13 | + * happy-dom computes no Tailwind media query, so the pin is the class; that the |
| 14 | + * classes reach 44 px in a browser is the PR's 375 / 390 px measurement. |
| 15 | + * |
| 16 | + * `t` echoes the key, so a hardcoded string fails here. |
| 17 | + */ |
| 18 | + |
| 19 | +import '@testing-library/jest-dom/vitest'; |
| 20 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 21 | +import React from 'react'; |
| 22 | +import { render, screen, cleanup, within } from '@testing-library/react'; |
| 23 | +import userEvent from '@testing-library/user-event'; |
| 24 | + |
| 25 | +const navigateMock = vi.fn(); |
| 26 | + |
| 27 | +const state = vi.hoisted(() => ({ |
| 28 | + apps: [] as Array<Record<string, unknown>>, |
| 29 | + /** The catalog's answer, once it has answered. */ |
| 30 | + agents: [] as Array<{ name: string }>, |
| 31 | + askAgent: false, |
| 32 | + buildAgent: false, |
| 33 | +})); |
| 34 | + |
| 35 | +vi.mock('react-router-dom', async () => { |
| 36 | + const { createElement } = await import('react'); |
| 37 | + return { |
| 38 | + useNavigate: () => navigateMock, |
| 39 | + Link: ({ to, ...rest }: { to: string } & Record<string, unknown>) => |
| 40 | + createElement('a', { ...rest, href: to }), |
| 41 | + }; |
| 42 | +}); |
| 43 | + |
| 44 | +vi.mock('@object-ui/i18n', async (importOriginal) => ({ |
| 45 | + ...(await importOriginal<Record<string, unknown>>()), |
| 46 | + useObjectTranslation: () => ({ t: (key: string) => `«${key}»`, language: 'en' }), |
| 47 | + useObjectLabel: () => { |
| 48 | + const label = (x: { name?: string; label?: string }) => String(x?.label || x?.name || ''); |
| 49 | + return { |
| 50 | + appLabel: label, |
| 51 | + objectLabel: label, |
| 52 | + objectPluralLabel: label, |
| 53 | + dashboardLabel: label, |
| 54 | + pageLabel: label, |
| 55 | + reportLabel: label, |
| 56 | + viewLabel: (_object: string, _view: string, fallback?: string) => fallback ?? '', |
| 57 | + }; |
| 58 | + }, |
| 59 | +})); |
| 60 | + |
| 61 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 62 | + ...(await importOriginal<Record<string, unknown>>()), |
| 63 | + useAuth: () => ({ user: { id: 'u1', name: 'Dev Admin', email: 'dev.admin@example.com' } }), |
| 64 | + useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }), |
| 65 | +})); |
| 66 | + |
| 67 | +// The catalog moves the way the real `useAgents` does: an empty first frame, |
| 68 | +// then loading, then the answer. |
| 69 | +vi.mock('@object-ui/plugin-chatbot/core', async (importOriginal) => { |
| 70 | + const { useEffect, useState } = await import('react'); |
| 71 | + return { |
| 72 | + ...(await importOriginal<typeof import('@object-ui/plugin-chatbot/core')>()), |
| 73 | + useAgents: () => { |
| 74 | + const [phase, setPhase] = useState<'idle' | 'loading' | 'answered'>('idle'); |
| 75 | + useEffect(() => { |
| 76 | + if (phase === 'idle') setPhase('loading'); |
| 77 | + else if (phase === 'loading') setPhase('answered'); |
| 78 | + }, [phase]); |
| 79 | + return { agents: phase === 'answered' ? state.agents : [], isLoading: phase === 'loading' }; |
| 80 | + }, |
| 81 | + isAskAgent: () => state.askAgent, |
| 82 | + agentHasCapability: () => state.buildAgent, |
| 83 | + }; |
| 84 | +}); |
| 85 | + |
| 86 | +vi.mock('../../../providers/MetadataProvider', () => ({ |
| 87 | + useMetadata: () => ({ apps: state.apps, objects: [], dashboards: [], pages: [], reports: [], loading: false }), |
| 88 | +})); |
| 89 | +vi.mock('../../../context/NavigationContext', () => ({ |
| 90 | + useNavigationContext: () => ({ currentAppName: 'crm' }), |
| 91 | +})); |
| 92 | +vi.mock('../../../hooks/useCanAuthorMetadata', () => ({ |
| 93 | + useCanAuthorMetadata: () => true, |
| 94 | + useCanReadPendingDrafts: () => true, |
| 95 | +})); |
| 96 | +vi.mock('../../../runtime-config', () => ({ |
| 97 | + getRuntimeConfig: () => ({ branding: { productName: 'ObjectStack' } }), |
| 98 | + isMarketplaceEnabled: () => true, |
| 99 | + isAiStudioEnabled: () => true, |
| 100 | +})); |
| 101 | + |
| 102 | +const now = new Date().toISOString(); |
| 103 | +vi.mock('../../../hooks/useHomeInbox', () => ({ |
| 104 | + useHomeInbox: () => ({ |
| 105 | + pendingApprovals: [{ id: 'apr_1', processLabel: 'Discount approval', recordTitle: 'QUO-0007', createdAt: now }], |
| 106 | + pendingApprovalsCount: 1, |
| 107 | + pendingApprovalsStatus: 'ready', |
| 108 | + pendingApprovalsReadable: true, |
| 109 | + notifications: [{ id: 'n1', title: 'Lead assigned', createdAt: now }], |
| 110 | + unreadTopicCount: 1, |
| 111 | + notificationsStatus: 'ready', |
| 112 | + notificationsReadable: true, |
| 113 | + activity: { |
| 114 | + value: [{ id: 'a1', user: 'Ada', description: 'updated a lead', timestamp: now, objectName: 'crm_lead' }], |
| 115 | + status: 'ready', |
| 116 | + readable: true, |
| 117 | + }, |
| 118 | + }), |
| 119 | +})); |
| 120 | +vi.mock('../../../hooks/useRecentItems', () => ({ |
| 121 | + useRecentItems: () => ({ |
| 122 | + recentItems: [{ id: 'object:crm_lead', type: 'object', name: 'crm_lead', href: '/apps/crm/crm_lead', visitedAt: now }], |
| 123 | + }), |
| 124 | +})); |
| 125 | +vi.mock('../../../hooks/useFavorites', () => ({ useFavorites: () => ({ favorites: [] }) })); |
| 126 | +vi.mock('../../../hooks/useAiSurface', () => ({ resolveAiApiBase: () => '' })); |
| 127 | +vi.mock('../../../preview/usePendingDrafts', () => ({ |
| 128 | + usePendingDrafts: () => ({ count: 1, entries: [{ name: 'crm_lead', label: 'Lead' }] }), |
| 129 | +})); |
| 130 | +vi.mock('../../../preview/usePublishAllDrafts', () => ({ |
| 131 | + usePublishAllDrafts: () => ({ publishAll: async () => ({ ok: true }), publishing: false }), |
| 132 | +})); |
| 133 | +vi.mock('../../../hooks/useInboxBell', () => ({ useInboxBell: () => ({ markRead: vi.fn() }) })); |
| 134 | +vi.mock('../../../layout/ActivityObjectLabel', () => ({ ActivityObjectLabel: () => null })); |
| 135 | + |
| 136 | +import { HomePage } from '../HomePage'; |
| 137 | + |
| 138 | +/** Enough apps that the strip folds the rest behind "More". */ |
| 139 | +const MANY_APPS = [ |
| 140 | + { name: 'setup', label: 'Setup', icon: 'Settings' }, |
| 141 | + ...Array.from({ length: 21 }, (_, i) => ({ name: `app_${i}`, label: `App ${i}`, icon: 'Box' })), |
| 142 | +]; |
| 143 | + |
| 144 | +/** Asserts a target keeps its desktop class and takes its phone class. */ |
| 145 | +function expectPhoneTarget(el: HTMLElement, desktop: string[], phone: string[]) { |
| 146 | + const classes = el.className.split(/\s+/); |
| 147 | + for (const c of desktop) expect(classes, `${el.dataset.testid}: desktop class ${c}`).toContain(c); |
| 148 | + for (const c of phone) expect(classes, `${el.dataset.testid}: phone class ${c}`).toContain(c); |
| 149 | +} |
| 150 | + |
| 151 | +beforeEach(() => { |
| 152 | + navigateMock.mockReset(); |
| 153 | + state.apps = MANY_APPS; |
| 154 | + state.agents = []; |
| 155 | + state.askAgent = false; |
| 156 | + state.buildAgent = false; |
| 157 | +}); |
| 158 | + |
| 159 | +afterEach(() => { |
| 160 | + cleanup(); |
| 161 | +}); |
| 162 | + |
| 163 | +describe('the greeting row (objectstack#29147)', () => { |
| 164 | + it('the builder chips are 44 px below sm and 32 px from sm', async () => { |
| 165 | + render(<HomePage />); |
| 166 | + expectPhoneTarget(await screen.findByTestId('home-build-in-studio'), ['h-8'], ['max-sm:h-11']); |
| 167 | + expectPhoneTarget(screen.getByTestId('home-start-template'), ['h-8'], ['max-sm:h-11']); |
| 168 | + }); |
| 169 | + |
| 170 | + it('"Build an app" and "Build with AI" are 44 px below sm and 32 px from sm', async () => { |
| 171 | + state.agents = [{ name: 'builder' }]; |
| 172 | + state.buildAgent = true; |
| 173 | + render(<HomePage />); |
| 174 | + expectPhoneTarget(await screen.findByTestId('home-build-app'), ['h-8'], ['max-sm:h-11']); |
| 175 | + expectPhoneTarget(screen.getByTestId('home-build-with-ai'), ['h-8'], ['max-sm:h-11']); |
| 176 | + }); |
| 177 | + |
| 178 | + it('"Ask AI" is 44 px below sm and 32 px from sm', async () => { |
| 179 | + state.agents = [{ name: 'ask' }]; |
| 180 | + state.askAgent = true; |
| 181 | + render(<HomePage />); |
| 182 | + expectPhoneTarget(await screen.findByTestId('home-ask-ai'), ['h-8'], ['max-sm:h-11']); |
| 183 | + }); |
| 184 | + |
| 185 | + it('with no app yet, the empty state\'s "Ask AI" is 44 px below sm', async () => { |
| 186 | + state.apps = []; |
| 187 | + state.agents = [{ name: 'ask' }]; |
| 188 | + state.askAgent = true; |
| 189 | + render(<HomePage />); |
| 190 | + expectPhoneTarget(await screen.findByTestId('home-ask-ai'), ['h-10'], ['max-sm:h-11']); |
| 191 | + }); |
| 192 | +}); |
| 193 | + |
| 194 | +describe('"Your apps" (objectstack#29147)', () => { |
| 195 | + it('the heading\'s links are 44 px tall below sm', () => { |
| 196 | + render(<HomePage />); |
| 197 | + expectPhoneTarget(screen.getByTestId('browse-marketplace-btn'), ['inline-flex', 'items-center'], ['max-sm:min-h-11']); |
| 198 | + expectPhoneTarget(screen.getByTestId('home-system-settings-link'), ['inline-flex', 'items-center'], ['max-sm:min-h-11']); |
| 199 | + }); |
| 200 | + |
| 201 | + it('"Show less" is a 44 px target below sm', async () => { |
| 202 | + render(<HomePage />); |
| 203 | + await userEvent.setup().click(screen.getByTestId('apps-show-all')); |
| 204 | + expectPhoneTarget(screen.getByTestId('apps-show-less'), ['text-xs'], ['max-sm:min-h-11', 'max-sm:min-w-11']); |
| 205 | + }); |
| 206 | +}); |
| 207 | + |
| 208 | +describe('"My work" (objectstack#29147)', () => { |
| 209 | + it('"Open inbox" and both "View all" links are 44 px targets below sm', () => { |
| 210 | + render(<HomePage />); |
| 211 | + for (const id of ['home-my-work-open-inbox', 'home-my-work-approvals-view-all', 'home-my-work-notifications-view-all']) { |
| 212 | + expectPhoneTarget(screen.getByTestId(id), ['text-xs'], ['max-sm:min-h-11', 'max-sm:min-w-11']); |
| 213 | + } |
| 214 | + // The column header grows with its link, so the link is not clipped. |
| 215 | + const header = screen.getByTestId('home-my-work-approvals-view-all').parentElement as HTMLElement; |
| 216 | + expectPhoneTarget(header, ['h-10'], ['max-sm:h-11']); |
| 217 | + }); |
| 218 | + |
| 219 | + it('a row is 44 px below sm, its 1 px divider outside the button', () => { |
| 220 | + render(<HomePage />); |
| 221 | + for (const id of ['home-my-work-approval-row', 'home-my-work-notification-row']) { |
| 222 | + const row = screen.getByTestId(id); |
| 223 | + expectPhoneTarget(row, ['h-full'], ['max-sm:min-h-11']); |
| 224 | + // A fixed 44 px item would leave the button 43 px under its border-top. |
| 225 | + expectPhoneTarget(row.parentElement as HTMLElement, ['h-10', 'border-t'], ['max-sm:h-auto']); |
| 226 | + } |
| 227 | + }); |
| 228 | + |
| 229 | + it('the pending-drafts row\'s "Publish" is 44 px below sm, in a row tall enough to hold it', () => { |
| 230 | + render(<HomePage />); |
| 231 | + expectPhoneTarget(screen.getByTestId('pending-drafts-publish'), ['h-7'], ['max-sm:h-11']); |
| 232 | + expectPhoneTarget(screen.getByTestId('home-pending-drafts'), ['h-10'], ['max-sm:h-14']); |
| 233 | + }); |
| 234 | +}); |
| 235 | + |
| 236 | +describe('"Recently accessed" and the activity feed (objectstack#29147)', () => { |
| 237 | + it('a recent row is 44 px below sm', () => { |
| 238 | + render(<HomePage />); |
| 239 | + const [row] = within(screen.getByTestId('home-recent')).getAllByTestId('home-recent-row'); |
| 240 | + expectPhoneTarget(row, ['h-10'], ['max-sm:h-11']); |
| 241 | + }); |
| 242 | + |
| 243 | + it('"View all activity" is 44 px tall below sm', () => { |
| 244 | + render(<HomePage />); |
| 245 | + expectPhoneTarget(screen.getByTestId('home-activity-view-all'), ['inline-flex', 'items-center'], ['max-sm:min-h-11']); |
| 246 | + }); |
| 247 | +}); |
0 commit comments