wrolpi/app/src/hooks/useSubsystemToggle.test.js
Roland 29f35fcc7d
build: uninstall Semantic UI
`semantic-ui-react`, `react-semantic-toasts-2` and `semantic-ui-offline` are
gone from package.json, the lockfile and node_modules -- 19 MB of dependencies
that nothing had imported since the migration finished.

Four things still referenced them and would have broken on uninstall.  None was
caught by a build or by jest:

  Four test files still ran `jest.mock('react-semantic-toasts-2', ...)` for
  components that no longer use it.  A jest.mock of a module that does not exist
  throws, so the packages could not have come out without this.

  `src/components/Download.cy.js` and `cypress/support/component.js` both
  imported semantic.min.css.  The support file now loads what the app loads --
  index.css, the font and token stylesheets, and Mantine's.

The ratchet changes job rather than being deleted.  An import of a missing module
fails loudly now, but three quieter mistakes would not: a `className="ui button"`
that styles nothing, a file reaching back through Theme.tsx, and someone
reinstalling a package to "just use one Semantic component".  It asserts all
three, plus that package.json names nothing matching /semantic/i.

Two pre-existing problems in the cypress component harness surfaced while
checking the uninstall had not broken it, both fixed because that file had to be
edited anyway:

  It imported `react-router-dom`, which is not installed -- this app is on
  react-router v7.  Dates to 5621d473, so the component suite has been
  un-runnable since well before this migration.

  Its mount helper supplied no MantineProvider, so every library component threw
  -- the same gap test-utils.js had for jest.

The component specs now load and four pass; eleven assertions are still written
against markup from before they stopped running.  Repairing those is its own
piece of work, deliberately not bundled in here.  The e2e suite is unaffected:
43/43.
2026-07-30 14:50:47 -06:00

128 lines
5.7 KiB
JavaScript

import React from 'react';
import {act, renderHook, waitFor} from '@testing-library/react';
import {useBluetooth, useDesktop, useHotspot, useThrottle, useVnc} from './customHooks';
import {StatusContext} from '../contexts/contexts';
jest.mock('../api/controller', () => ({
unblockBluetooth: jest.fn().mockResolvedValue({}),
blockBluetooth: jest.fn().mockResolvedValue({}),
startHotspot: jest.fn().mockResolvedValue({}),
stopHotspot: jest.fn().mockResolvedValue({}),
startDesktop: jest.fn().mockResolvedValue({}),
stopDesktop: jest.fn().mockResolvedValue({}),
startVnc: jest.fn().mockResolvedValue({}),
stopVnc: jest.fn().mockResolvedValue({}),
enableThrottle: jest.fn().mockResolvedValue({}),
disableThrottle: jest.fn().mockResolvedValue({}),
getControllerStats: jest.fn().mockResolvedValue({}),
}));
const controllerApi = require('../api/controller');
const fetchStatus = jest.fn();
const makeWrapper = (status) => ({children}) => (
<StatusContext.Provider value={{status, fetchStatus}}>{children}</StatusContext.Provider>
);
beforeEach(() => {
jest.clearAllMocks();
Object.values(controllerApi).forEach(fn => fn.mockResolvedValue && fn.mockResolvedValue({}));
});
describe('subsystem toggle hooks', () => {
// Each hook reads a different status field with different values for on/off.
const cases = [
{
name: 'bluetooth', hook: useBluetooth, setter: 'setBluetooth',
status: {bluetooth_status: 'on'}, offStatus: {bluetooth_status: 'off'},
start: 'unblockBluetooth', stop: 'blockBluetooth',
},
{
name: 'desktop', hook: useDesktop, setter: 'setDesktop',
status: {desktop_status: 'on'}, offStatus: {desktop_status: 'off'},
start: 'startDesktop', stop: 'stopDesktop',
},
{
name: 'vnc', hook: useVnc, setter: 'setVnc',
status: {vnc_status: 'on'}, offStatus: {vnc_status: 'off'},
start: 'startVnc', stop: 'stopVnc',
},
{
name: 'throttle', hook: useThrottle, setter: 'setThrottle',
status: {throttle_status: 'powersave'}, offStatus: {throttle_status: 'ondemand'},
start: 'enableThrottle', stop: 'disableThrottle',
},
{
name: 'hotspot', hook: useHotspot, setter: 'setHotspot',
status: {hotspot_status: 'connected'}, offStatus: {hotspot_status: 'disconnected'},
start: 'startHotspot', stop: 'stopHotspot',
},
];
cases.forEach(({name, hook, setter, status, offStatus, start, stop}) => {
describe(name, () => {
it('reports on and off from the status field', async () => {
const {result} = renderHook(hook, {wrapper: makeWrapper(status)});
await waitFor(() => expect(result.current.on).toBe(true));
const {result: offResult} = renderHook(hook, {wrapper: makeWrapper(offStatus)});
await waitFor(() => expect(offResult.current.on).toBe(false));
});
it('reports unknown status as null so the UI can disable the toggle', async () => {
const {result} = renderHook(hook, {wrapper: makeWrapper({})});
await waitFor(() => expect(result.current.on).toBe(null));
});
it('starts and stops through the Controller', async () => {
const {result} = renderHook(hook, {wrapper: makeWrapper(offStatus)});
await waitFor(() => expect(result.current.on).toBe(false));
await act(async () => await result.current[setter](true));
expect(controllerApi[start]).toHaveBeenCalled();
await act(async () => await result.current[setter](false));
expect(controllerApi[stop]).toHaveBeenCalled();
});
it('restores the previous state when the request fails', async () => {
// Otherwise `on` stays null, and because the status poll sees no change
// the toggle is left disabled and reading as unsupported until remount.
controllerApi[stop].mockRejectedValue(new Error('boom'));
const {result} = renderHook(hook, {wrapper: makeWrapper(status)});
await waitFor(() => expect(result.current.on).toBe(true));
await act(async () => await result.current[setter](false));
expect(result.current.on).toBe(true);
});
});
});
it('throttle refetches status because the governor is read from sysfs', async () => {
const {result} = renderHook(useThrottle, {wrapper: makeWrapper({throttle_status: 'ondemand'})});
await waitFor(() => expect(result.current.on).toBe(false));
await act(async () => await result.current.setThrottle(true));
expect(fetchStatus).toHaveBeenCalled();
});
it('hotspot reports a WiFi device that is in use for a network', async () => {
const {result} = renderHook(useHotspot, {wrapper: makeWrapper({hotspot_status: 'in_use'})});
await waitFor(() => expect(result.current.inUse).toBe(true));
// In use means the hotspot is not running, but starting it leaves that network.
expect(result.current.on).toBe(false);
});
it('vnc exposes whether the desktop it serves is running', async () => {
const {result} = renderHook(useVnc, {wrapper: makeWrapper({vnc_status: 'off', desktop_status: 'on'})});
await waitFor(() => expect(result.current.desktopRunning).toBe(true));
const {result: stopped} = renderHook(useVnc, {wrapper: makeWrapper({vnc_status: 'off', desktop_status: 'off'})});
await waitFor(() => expect(stopped.current.desktopRunning).toBe(false));
});
});