|
| 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 | + * The scan button on text and lookup fields, offered by the ObjectStack |
| 11 | + * mobile shell's `scan()` bridge method (objectstack-ai/mobile |
| 12 | + * `docs/HYBRID-SHELL.md`, "Scanning codes"). |
| 13 | + * |
| 14 | + * The stub below is `window.ObjectStackNative` with the members the fields |
| 15 | + * read. The control cases are the ones a desktop browser and an older shell |
| 16 | + * take: no global at all, and a global whose `has('scan')` says no. Both must |
| 17 | + * render no button. |
| 18 | + */ |
| 19 | + |
| 20 | +import * as React from 'react'; |
| 21 | +import { describe, it, expect, vi, afterEach } from 'vitest'; |
| 22 | +import { render, screen, cleanup, waitFor, act, fireEvent } from '@testing-library/react'; |
| 23 | +import '@testing-library/jest-dom'; |
| 24 | +import { toast } from '@object-ui/components'; |
| 25 | +import { TextField } from './TextField'; |
| 26 | +import { LookupField } from './LookupField'; |
| 27 | + |
| 28 | +const GLOBAL = 'ObjectStackNative'; |
| 29 | + |
| 30 | +type ScanAnswer = string | null | Error; |
| 31 | + |
| 32 | +function installShell(opts: { capabilities?: string[]; answer?: ScanAnswer } = {}) { |
| 33 | + const capabilities = opts.capabilities ?? ['haptic', 'scan']; |
| 34 | + const scan = vi.fn(async () => { |
| 35 | + const answer = opts.answer === undefined ? 'ASSET-0042' : opts.answer; |
| 36 | + if (answer instanceof Error) throw answer; |
| 37 | + return answer; |
| 38 | + }); |
| 39 | + (window as unknown as Record<string, unknown>)[GLOBAL] = { |
| 40 | + version: 1, |
| 41 | + platform: 'ios', |
| 42 | + appVersion: '2.1.0', |
| 43 | + capabilities, |
| 44 | + safeArea: { top: 0, bottom: 0, left: 0, right: 0 }, |
| 45 | + has: (c: string) => capabilities.includes(c), |
| 46 | + haptic: vi.fn(async () => null), |
| 47 | + scan, |
| 48 | + ready: vi.fn(), |
| 49 | + routeChanged: vi.fn(), |
| 50 | + }; |
| 51 | + return scan; |
| 52 | +} |
| 53 | + |
| 54 | +afterEach(() => { |
| 55 | + cleanup(); |
| 56 | + delete (window as unknown as Record<string, unknown>)[GLOBAL]; |
| 57 | + vi.restoreAllMocks(); |
| 58 | + try { |
| 59 | + localStorage.clear(); |
| 60 | + } catch { |
| 61 | + /* no storage in this environment */ |
| 62 | + } |
| 63 | +}); |
| 64 | + |
| 65 | +const scanButton = () => screen.queryByRole('button', { name: 'Scan code' }); |
| 66 | + |
| 67 | +async function settle(ms = 0): Promise<void> { |
| 68 | + for (let i = 0; i < 6; i++) { |
| 69 | + await act(async () => { |
| 70 | + await new Promise((r) => setTimeout(r, ms)); |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +async function tapScan(): Promise<void> { |
| 76 | + await act(async () => { |
| 77 | + fireEvent.click(scanButton()!); |
| 78 | + }); |
| 79 | + await settle(); |
| 80 | +} |
| 81 | + |
| 82 | +function renderText(props: Record<string, unknown> = {}, field: Record<string, unknown> = {}) { |
| 83 | + const onChange = vi.fn(); |
| 84 | + render( |
| 85 | + <TextField |
| 86 | + value="" |
| 87 | + onChange={onChange} |
| 88 | + field={{ name: 'asset_tag', type: 'text', ...field } as never} |
| 89 | + {...props} |
| 90 | + />, |
| 91 | + ); |
| 92 | + return onChange; |
| 93 | +} |
| 94 | + |
| 95 | +describe('text field scan button', () => { |
| 96 | + it('control: a plain browser (no shell global) renders no scan button', () => { |
| 97 | + renderText(); |
| 98 | + expect(screen.getByRole('textbox')).toBeInTheDocument(); |
| 99 | + expect(scanButton()).toBeNull(); |
| 100 | + }); |
| 101 | + |
| 102 | + it("control: an older shell whose has('scan') is false renders no scan button", () => { |
| 103 | + const scan = installShell({ capabilities: ['haptic'] }); |
| 104 | + renderText(); |
| 105 | + expect(scanButton()).toBeNull(); |
| 106 | + expect(scan).not.toHaveBeenCalled(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("a shell with has('scan') renders the button on an editable text field", () => { |
| 110 | + installShell(); |
| 111 | + renderText(); |
| 112 | + expect(scanButton()).toBeInTheDocument(); |
| 113 | + }); |
| 114 | + |
| 115 | + it('a read-only, a disabled and a password field render no scan button', () => { |
| 116 | + installShell(); |
| 117 | + renderText({ readonly: true }); |
| 118 | + expect(scanButton()).toBeNull(); |
| 119 | + cleanup(); |
| 120 | + renderText({ disabled: true }); |
| 121 | + expect(scanButton()).toBeNull(); |
| 122 | + cleanup(); |
| 123 | + renderText({}, { type: 'password' }); |
| 124 | + expect(scanButton()).toBeNull(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('the decoded value lands through onChange', async () => { |
| 128 | + const scan = installShell({ answer: 'ASSET-0042' }); |
| 129 | + const onChange = renderText(); |
| 130 | + await tapScan(); |
| 131 | + expect(scan).toHaveBeenCalledTimes(1); |
| 132 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 133 | + expect(onChange).toHaveBeenCalledWith('ASSET-0042'); |
| 134 | + }); |
| 135 | + |
| 136 | + it('null (the user closed the scanner) changes nothing and shows no toast', async () => { |
| 137 | + const error = vi.spyOn(toast, 'error'); |
| 138 | + const scan = installShell({ answer: null }); |
| 139 | + const onChange = renderText(); |
| 140 | + await tapScan(); |
| 141 | + expect(scan).toHaveBeenCalledTimes(1); |
| 142 | + expect(onChange).not.toHaveBeenCalled(); |
| 143 | + expect(error).not.toHaveBeenCalled(); |
| 144 | + }); |
| 145 | + |
| 146 | + it("a failed scan raises the error toast with the shell's message and changes nothing", async () => { |
| 147 | + const error = vi.spyOn(toast, 'error'); |
| 148 | + installShell({ answer: Object.assign(new Error('Camera permission denied'), { code: 'failed' }) }); |
| 149 | + const onChange = renderText(); |
| 150 | + await tapScan(); |
| 151 | + expect(onChange).not.toHaveBeenCalled(); |
| 152 | + expect(error).toHaveBeenCalledTimes(1); |
| 153 | + expect(error).toHaveBeenCalledWith('Could not scan', { description: 'Camera permission denied' }); |
| 154 | + }); |
| 155 | +}); |
| 156 | + |
| 157 | +const ROWS: Record<string, unknown>[] = [ |
| 158 | + { id: 'a1', name: 'ASSET-0042 Forklift' }, |
| 159 | + { id: 'a2', name: 'ASSET-0077 Pallet jack' }, |
| 160 | + { id: 'a3', name: 'ASSET-0099 Scissor lift' }, |
| 161 | +]; |
| 162 | + |
| 163 | +function makeBackend() { |
| 164 | + const find = vi.fn(async (_objectName: string, params?: { $search?: string; $filter?: { id?: { $in?: unknown[] } } }) => { |
| 165 | + const wanted = params?.$filter?.id?.$in; |
| 166 | + if (wanted) { |
| 167 | + const rows = ROWS.filter((r) => wanted.includes(r.id)); |
| 168 | + return { data: rows.map((r) => ({ ...r })), total: rows.length }; |
| 169 | + } |
| 170 | + const term = params?.$search; |
| 171 | + const rows = term ? ROWS.filter((r) => String(r.name).includes(term)) : ROWS; |
| 172 | + return { data: rows.map((r) => ({ ...r })), total: rows.length }; |
| 173 | + }); |
| 174 | + const findOne = vi.fn(async (_o: string, id: string) => ROWS.find((r) => r.id === id) ?? null); |
| 175 | + return { find, findOne }; |
| 176 | +} |
| 177 | + |
| 178 | +function renderLookup(ds: ReturnType<typeof makeBackend>, field: Record<string, unknown> = {}, props: Record<string, unknown> = {}) { |
| 179 | + render( |
| 180 | + <LookupField |
| 181 | + value={null} |
| 182 | + onChange={() => {}} |
| 183 | + dataSource={ds as never} |
| 184 | + field={{ name: 'asset', reference: 'asset', label: 'Asset', ...field } as never} |
| 185 | + {...props} |
| 186 | + />, |
| 187 | + ); |
| 188 | +} |
| 189 | + |
| 190 | +const searchedTerms = (ds: ReturnType<typeof makeBackend>) => |
| 191 | + ds.find.mock.calls.map((c) => c[1]?.$search).filter(Boolean); |
| 192 | + |
| 193 | +describe('lookup field scan button', () => { |
| 194 | + it('control: a plain browser renders no scan button on a lookup', async () => { |
| 195 | + const ds = makeBackend(); |
| 196 | + renderLookup(ds); |
| 197 | + await settle(); |
| 198 | + expect(screen.getByTestId('lookup-trigger-asset')).toBeInTheDocument(); |
| 199 | + expect(scanButton()).toBeNull(); |
| 200 | + }); |
| 201 | + |
| 202 | + it("control: an older shell whose has('scan') is false renders no scan button on a lookup", async () => { |
| 203 | + installShell({ capabilities: ['haptic'] }); |
| 204 | + const ds = makeBackend(); |
| 205 | + renderLookup(ds); |
| 206 | + await settle(); |
| 207 | + expect(scanButton()).toBeNull(); |
| 208 | + }); |
| 209 | + |
| 210 | + it('a disabled lookup renders no scan button', async () => { |
| 211 | + installShell(); |
| 212 | + const ds = makeBackend(); |
| 213 | + renderLookup(ds, {}, { disabled: true }); |
| 214 | + await settle(); |
| 215 | + expect(scanButton()).toBeNull(); |
| 216 | + }); |
| 217 | + |
| 218 | + it('the decoded value opens the dropdown and drives its search', async () => { |
| 219 | + installShell({ answer: 'ASSET-0042' }); |
| 220 | + const ds = makeBackend(); |
| 221 | + renderLookup(ds); |
| 222 | + await settle(); |
| 223 | + await tapScan(); |
| 224 | + expect(screen.getByRole('combobox')).toHaveValue('ASSET-0042'); |
| 225 | + await settle(70); |
| 226 | + await waitFor(() => expect(searchedTerms(ds)).toContain('ASSET-0042')); |
| 227 | + await waitFor(() => expect(screen.getAllByRole('option').map((o) => o.getAttribute('title'))).toEqual(['ASSET-0042 Forklift'])); |
| 228 | + }); |
| 229 | + |
| 230 | + it('null leaves a lookup closed and unsearched', async () => { |
| 231 | + installShell({ answer: null }); |
| 232 | + const ds = makeBackend(); |
| 233 | + renderLookup(ds); |
| 234 | + await settle(); |
| 235 | + await tapScan(); |
| 236 | + expect(screen.queryByRole('combobox')).toBeNull(); |
| 237 | + expect(searchedTerms(ds)).toEqual([]); |
| 238 | + }); |
| 239 | + |
| 240 | + it("the search-first variant opens its picker seeded with the decoded value", async () => { |
| 241 | + installShell({ answer: 'ASSET-0077' }); |
| 242 | + const ds = makeBackend(); |
| 243 | + renderLookup(ds, { picker: 'search' }); |
| 244 | + await settle(); |
| 245 | + await tapScan(); |
| 246 | + const box = await screen.findByTestId('people-picker-search'); |
| 247 | + expect(box).toHaveValue('ASSET-0077'); |
| 248 | + await settle(70); |
| 249 | + await waitFor(() => expect(searchedTerms(ds)).toContain('ASSET-0077')); |
| 250 | + }); |
| 251 | +}); |
0 commit comments