|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * objectstack#36955 — every result type the full search page can emit has a |
| 11 | + * group heading (`search.type<Type>s`) and a badge (`search.badge<Type>`) in |
| 12 | + * every shipped locale pack. |
| 13 | + * |
| 14 | + * The page derives both keys from the nav item's `type` at render time, so a |
| 15 | + * type the packs do not name prints its raw key. Component and doc items did |
| 16 | + * exactly that (`search.typeComponents`, `search.badgeDoc`, …) while objects, |
| 17 | + * dashboards, pages and reports were translated. |
| 18 | + * |
| 19 | + * ⭐ Neither the type list nor the key list is written down here. The types come |
| 20 | + * from the spec's nav-item discriminator (`NavigationItemTypeSchema`, read off |
| 21 | + * `@objectstack/spec`); the page is rendered with one item of EVERY type, each |
| 22 | + * carrying every target field, and the page itself decides which survive (its |
| 23 | + * `resolveHref` drops the ones that link nowhere). The keys are the ones the |
| 24 | + * page then asks `t` for. A new nav type that the page lists, or a new key |
| 25 | + * derivation, is measured without an edit to this file. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi } from 'vitest'; |
| 29 | +import { render } from '@testing-library/react'; |
| 30 | +import React from 'react'; |
| 31 | +import { NavigationItemTypeSchema } from '@object-ui/types/zod'; |
| 32 | +import { builtInLocales } from '@object-ui/i18n/locales'; |
| 33 | + |
| 34 | +/** Every key the component asked `t` for during the render. */ |
| 35 | +const asked = vi.hoisted(() => ({ keys: [] as string[] })); |
| 36 | +const app = vi.hoisted(() => ({ navigation: [] as unknown[] })); |
| 37 | + |
| 38 | +vi.mock('react-router-dom', () => ({ |
| 39 | + useParams: () => ({ appName: 'crm' }), |
| 40 | + useSearchParams: () => [new URLSearchParams(''), vi.fn()], |
| 41 | + Link: ({ to, children, ...rest }: any) => ( |
| 42 | + <a href={typeof to === 'string' ? to : ''} {...rest}> |
| 43 | + {children} |
| 44 | + </a> |
| 45 | + ), |
| 46 | +})); |
| 47 | + |
| 48 | +vi.mock('@object-ui/i18n', async (importOriginal) => { |
| 49 | + const actual = await importOriginal<any>(); |
| 50 | + return { |
| 51 | + ...actual, |
| 52 | + useObjectTranslation: () => ({ |
| 53 | + t: (key: string) => { |
| 54 | + asked.keys.push(key); |
| 55 | + return key; |
| 56 | + }, |
| 57 | + language: 'en', |
| 58 | + }), |
| 59 | + }; |
| 60 | +}); |
| 61 | + |
| 62 | +vi.mock('@object-ui/react', async (importOriginal) => ({ |
| 63 | + ...(await importOriginal<Record<string, unknown>>()), |
| 64 | + useRecordSearch: () => ({ results: [], isSearching: false, error: undefined }), |
| 65 | +})); |
| 66 | + |
| 67 | +vi.mock('../../providers/MetadataProvider', () => ({ |
| 68 | + useMetadata: () => ({ |
| 69 | + apps: [{ name: 'crm', label: 'CRM', navigation: app.navigation }], |
| 70 | + objects: [], |
| 71 | + }), |
| 72 | +})); |
| 73 | + |
| 74 | +vi.mock('../../providers/AdapterProvider', () => ({ |
| 75 | + useAdapter: () => ({ find: vi.fn(), searchAll: vi.fn() }), |
| 76 | +})); |
| 77 | + |
| 78 | +vi.mock('@object-ui/auth', async (importOriginal) => ({ |
| 79 | + ...(await importOriginal<Record<string, unknown>>()), |
| 80 | + useAuth: () => ({ user: { id: 'u1' }, activeOrganization: null }), |
| 81 | +})); |
| 82 | + |
| 83 | +import { SearchResultsPage } from '../SearchResultsPage'; |
| 84 | + |
| 85 | +const SPEC_NAV_TYPES = NavigationItemTypeSchema.options as readonly string[]; |
| 86 | + |
| 87 | +/** |
| 88 | + * One item per spec nav type, each carrying every target field any type reads, |
| 89 | + * so whether it is listed is the page's own decision, not this fixture's. |
| 90 | + */ |
| 91 | +const everyTypeNavigation = () => |
| 92 | + SPEC_NAV_TYPES.map((type) => ({ |
| 93 | + id: `nav_${type}`, |
| 94 | + type, |
| 95 | + label: `Item ${type}`, |
| 96 | + objectName: 'crm_account', |
| 97 | + dashboardName: 'crm_overview', |
| 98 | + pageName: 'crm_home', |
| 99 | + reportName: 'crm_pipeline', |
| 100 | + url: 'https://example.com/help', |
| 101 | + componentRef: 'approvals:inbox', |
| 102 | + book: 'service_manual', |
| 103 | + doc: 'getting_started', |
| 104 | + actionDef: { actionName: 'crm_refresh' }, |
| 105 | + children: [], |
| 106 | + })); |
| 107 | + |
| 108 | +const lookup = (pack: unknown, dotted: string): unknown => |
| 109 | + dotted.split('.').reduce<any>((node, part) => node?.[part], pack); |
| 110 | + |
| 111 | +/** The packs that carry the page's `search` block — every shipped one today. */ |
| 112 | +const searchPacks = Object.entries(builtInLocales).filter( |
| 113 | + ([, pack]) => typeof lookup(pack, 'search') === 'object', |
| 114 | +); |
| 115 | + |
| 116 | +describe('SearchResultsPage result-type keys exist in every locale pack (objectstack#36955)', () => { |
| 117 | + asked.keys = []; |
| 118 | + app.navigation = everyTypeNavigation(); |
| 119 | + render(<SearchResultsPage />); |
| 120 | + const headingKeys = [...new Set(asked.keys.filter((k) => /^search\.type\p{Lu}/u.test(k)))]; |
| 121 | + const badgeKeys = [...new Set(asked.keys.filter((k) => /^search\.badge\p{Lu}/u.test(k)))]; |
| 122 | + |
| 123 | + it('measures something: the page lists component and doc items among its groups', () => { |
| 124 | + // Non-vacuity: the render really produced groups, including the two types |
| 125 | + // whose keys were missing. |
| 126 | + expect(searchPacks.length).toBe(Object.keys(builtInLocales).length); |
| 127 | + expect(headingKeys).toEqual(expect.arrayContaining(['search.typeComponents', 'search.typeDocs'])); |
| 128 | + expect(badgeKeys).toEqual(expect.arrayContaining(['search.badgeComponent', 'search.badgeDoc'])); |
| 129 | + expect(headingKeys.length).toBe(badgeKeys.length); |
| 130 | + }); |
| 131 | + |
| 132 | + it.each(searchPacks.map(([tag]) => tag))('%s names every heading and badge the page asks for', (tag) => { |
| 133 | + const pack = builtInLocales[tag as keyof typeof builtInLocales]; |
| 134 | + const missing = [...headingKeys, ...badgeKeys].filter((key) => { |
| 135 | + const value = lookup(pack, key); |
| 136 | + return typeof value !== 'string' || value.trim() === ''; |
| 137 | + }); |
| 138 | + expect(missing).toEqual([]); |
| 139 | + }); |
| 140 | +}); |
0 commit comments