diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index f896f7b..98f10bd 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,4 +1,4 @@ -import { useEffect, useLayoutEffect, useRef } from 'react' +import { useEffect, useLayoutEffect, useRef, useState } from 'react' import { HashRouter, Routes, Route, Navigate, useNavigate, useLocation, useNavigationType } from 'react-router-dom' import { useStore } from './store/useStore.js' import { useUI } from './store/useUI.js' diff --git a/frontend/src/App.smoke.test.jsx b/frontend/src/App.smoke.test.jsx new file mode 100644 index 0000000..45ffc15 --- /dev/null +++ b/frontend/src/App.smoke.test.jsx @@ -0,0 +1,65 @@ +// @vitest-environment happy-dom +/* The app shell, rendered once, end to end. + * + * This exists because a missing hook import in App.jsx — `useState` used, never imported — shipped + * to production and the only symptom was a background: React threw while rendering the shell, so + * neither the app nor the launch screen ever mounted, and every test in the suite passed because + * none of them rendered App. A page that only ever needs to render is still a page worth rendering + * in a test: the whole class of "the bundle parses but the tree throws" lives here. */ +import React, { act } from 'react' +import { createRoot } from 'react-dom/client' +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +// No network in a smoke test: the shell's boot() pulls config and state, and every one of those +// goes through lib/api.js. Everything else in that module stays real (Login reads IS_APPLE, BIO, +// passkeyLogin… from it), which is what `importOriginal` is for — a hand-listed mock would leave +// the screens importing exports that do not exist, and the error boundary would hide it. +vi.mock('./lib/api.js', async importOriginal => ({ + ...(await importOriginal()), + api: vi.fn(async () => ({})) +})) +vi.mock('./lib/back.js', () => ({ initBackButton: async () => () => {} })) +vi.mock('./lib/push.js', () => ({ syncPushSubscription: async () => {} })) + +const { default: App } = await import('./App.jsx') + +let host, root +beforeEach(() => { + globalThis.IS_REACT_ACT_ENVIRONMENT = true + localStorage.clear() + host = document.createElement('div') + document.body.appendChild(host) + root = createRoot(host) +}) +afterEach(() => { act(() => root.unmount()); host.remove() }) + +describe('the app shell', () => { + it('mounts without throwing, and shows the launch screen first', async () => { + await act(async () => { root.render() }) + // The splash: the shell renders it before anything else, which is what a launch screen is. + expect(host.querySelector('.boot-splash')).toBeTruthy() + expect(host.querySelector('img.boot-photo')?.getAttribute('src')).toBe('splash.webp') + expect(host.querySelector('img.boot-logo')?.getAttribute('src')).toBe('logo.png') + }) + + it('gets past the launch screen and paints a screen', async () => { + await act(async () => { root.render() }) + // Wait out the 500ms floor, then let boot() settle. + await act(async () => { await new Promise(r => setTimeout(r, 600)) }) + expect(host.querySelector('.boot-splash')).toBeNull() + // Something real is on screen — the login door for a device with no profile. The assertion is + // deliberately loose: the point is that the tree rendered, not which screen it chose. + expect(host.textContent.trim().length).toBeGreaterThan(0) + }) + + it('keeps local data visible when boot is slow, instead of holding the splash', async () => { + // A signed-in device whose boot never finishes (bad network): the splash covers the first half + // second and then gets out of the way, because the local copy is the whole point of offline. + localStorage.setItem('gym_user', JSON.stringify({ id: 'u1', name: 'Test' })) + const api = (await import('./lib/api.js')).api + api.mockImplementation(() => new Promise(() => {})) // never resolves + await act(async () => { root.render() }) + await act(async () => { await new Promise(r => setTimeout(r, 600)) }) + expect(host.querySelector('.boot-splash')).toBeNull() + }) +})