Fix the shell: useState was used and never imported, so the app never mounted
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s

The launch-screen work added a useState to App.jsx without adding it to the React import
line, and the only symptom in production was a background: React threw while rendering the
shell, so neither the app nor the splash ever mounted. "Uncaught ReferenceError: useState
is not defined" at the top of the bundle is as fatal as a build error and much quieter —
the page loads, the CSS applies, the aura paints, and nothing else happens.

It shipped because no test rendered App. The whole point of a shell is that it can fail
everything by failing once, so there is now a smoke test that mounts it against a mocked
network: it checks the app renders at all, that the launch screen comes first with the
photo and the mark on it, that the shell gets past it to a real screen, and that a
signed-in device whose boot never finishes still gets its own data rather than being held
on the splash. Checked against the bug itself — with useState removed from the import all
three fail with that exact ReferenceError.

Also swept every file in src/ for the same class of mistake (a hook called, its name not in
the React import): App.jsx was the only one.
This commit is contained in:
dsantillan committed 2026-09-23 11:40:56 -04:00
1 parent a0f1f388bc
commit 3908dc524a
2 files changed
+66 -1

No files matched your search

+1 -1
View File
@@ -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'
+65
View File
@@ -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(<App />) })
// 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(<App />) })
// 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(<App />) })
await act(async () => { await new Promise(r => setTimeout(r, 600)) })
expect(host.querySelector('.boot-splash')).toBeNull()
})
})