spaxel/dashboard/js/onboard.test.setup.js
jedarden 1b0e7ea2b4 fix(dashboard): fix onboarding wizard test failures
- Fix WebSocket mock to use factory function so resetAllMocks doesn't
  break it (state.ws.close is not a function errors)
- Fix TextEncoderStream mock to provide functional readable/writable
  for pipeTo (needed by provisioning serial send tests)
- Fix flash_firmware test to check wizard-nav for "Skip Flashing"
  button instead of wizard-content
- Fix provisionAndSend "no port" test to use mockResolvedValue
  instead of mockResolvedValueOnce so both primary and fallback
  paths fail consistently

All 60 tests now pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-28 04:01:20 -04:00

101 lines
2.8 KiB
JavaScript

/**
* Jest setup for onboard tests.
* Mocks Web Serial API, fetch, WebSocket, and sessionStorage.
*/
// Mock TextEncoderStream (not available in jsdom)
// Must provide functional readable/writable for pipeTo to work
var _lastEncodedData = '';
global.TextEncoderStream = class TextEncoderStream {
constructor() {
this.readable = {
pipeTo: jest.fn().mockResolvedValue(undefined),
};
this.writable = {
getWriter: jest.fn().mockReturnValue({
write: jest.fn(function (data) { _lastEncodedData = data; }),
close: jest.fn().mockResolvedValue(undefined),
releaseLock: jest.fn(),
}),
};
}
};
global.__getLastEncodedData = function () { return _lastEncodedData; };
global.__clearLastEncodedData = function () { _lastEncodedData = ''; };
// Mock ReadableStream/WritableStream (not available in jsdom)
global.ReadableStream = class ReadableStream {};
global.WritableStream = class WritableStream {};
// Mock navigator.serial
const mockPort = {
open: jest.fn().mockResolvedValue(undefined),
close: jest.fn().mockResolvedValue(undefined),
readable: {
pipeTo: jest.fn().mockResolvedValue(undefined),
},
writable: {
getWriter: jest.fn().mockReturnValue({
write: jest.fn().mockResolvedValue(undefined),
close: jest.fn().mockResolvedValue(undefined),
releaseLock: jest.fn(),
}),
},
};
Object.defineProperty(navigator, 'serial', {
value: {
requestPort: jest.fn().mockResolvedValue(mockPort),
getPorts: jest.fn().mockResolvedValue([mockPort]),
},
writable: true,
configurable: true,
});
// Mock fetch
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue([]),
});
// Mock WebSocket — use a factory so resetAllMocks doesn't break it
function _makeWSMock() {
return {
binaryType: 'arraybuffer',
close: jest.fn(),
send: jest.fn(),
readyState: 1,
onopen: null,
onclose: null,
onerror: null,
onmessage: null,
};
}
global.WebSocket = jest.fn().mockImplementation(function () {
return _makeWSMock();
});
// Mock crypto.randomUUID
Object.defineProperty(global, 'crypto', {
value: {
randomUUID: jest.fn().mockReturnValue('test-uuid-1234'),
},
});
// Mock customElements
global.customElements = {
get: jest.fn().mockReturnValue(null),
define: jest.fn(),
};
// Mock sessionStorage
var storage = {};
global.sessionStorage = {
getItem: jest.fn(function (key) { return storage[key] || null; }),
setItem: jest.fn(function (key, val) { storage[key] = val; }),
removeItem: jest.fn(function (key) { delete storage[key]; }),
clear: jest.fn(function () { storage = {}; }),
};
// Export mock port for tests
global.__mockPort = mockPort;