Full tests coverage
This commit is contained in:
parent
99134d8556
commit
02d782c3da
157 changed files with 1074 additions and 34670 deletions
|
|
@ -104,4 +104,92 @@ describe('useShoppingListSync', () => {
|
|||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('handles invalid onmessage payload gracefully', () => {
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onopen) ws.onopen(); });
|
||||
|
||||
// Should swallow invalid JSON parse error
|
||||
act(() => {
|
||||
if (ws.onmessage) ws.onmessage({ data: 'invalid-json' });
|
||||
});
|
||||
expect(onSync).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('handles ws error states', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onerror) ws.onerror(); });
|
||||
expect(result.current.error).toBe('Connection interrupt');
|
||||
});
|
||||
|
||||
it('handles websocket initialization errors gracefully', () => {
|
||||
global.WebSocket = function() {
|
||||
throw new Error('WS Blocked');
|
||||
} as any;
|
||||
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
expect(result.current.error).toBe('Sync failed to initialize');
|
||||
});
|
||||
|
||||
it('does not connect if householdId or listId is missing', () => {
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('', 'list1', onSync));
|
||||
expect(createdSockets).toHaveLength(0);
|
||||
|
||||
renderHook(() => useShoppingListSync('hh1', '', onSync));
|
||||
expect(createdSockets).toHaveLength(0);
|
||||
});
|
||||
|
||||
it('handles disconnect and uses default severed message when reason is missing', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
const ws = createdSockets[0];
|
||||
act(() => { if (ws.onopen) ws.onopen(); });
|
||||
act(() => { if (ws.onclose) ws.onclose({} as any); });
|
||||
expect(result.current.isConnected).toBe(false);
|
||||
});
|
||||
|
||||
it('stops attempting to reconnect after 5 consecutive failures', () => {
|
||||
vi.useFakeTimers();
|
||||
const onSync = vi.fn();
|
||||
renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
|
||||
expect(createdSockets).toHaveLength(1);
|
||||
|
||||
for (let attempt = 1; attempt <= 5; attempt++) {
|
||||
const currentSocket = createdSockets[createdSockets.length - 1];
|
||||
act(() => { if (currentSocket.onclose) currentSocket.onclose({ reason: 'fail' }); });
|
||||
act(() => { vi.advanceTimersByTime(attempt * 1000); });
|
||||
}
|
||||
|
||||
const totalSocketsCreated = createdSockets.length;
|
||||
expect(totalSocketsCreated).toBe(6);
|
||||
|
||||
const finalSocket = createdSockets[totalSocketsCreated - 1];
|
||||
act(() => { if (finalSocket.onclose) finalSocket.onclose({ reason: 'fail' }); });
|
||||
act(() => { vi.advanceTimersByTime(6000); });
|
||||
|
||||
expect(createdSockets.length).toBe(totalSocketsCreated);
|
||||
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it('does not send toggle item message if socket is not open', () => {
|
||||
const onSync = vi.fn();
|
||||
const { result } = renderHook(() => useShoppingListSync('hh1', 'list1', onSync));
|
||||
|
||||
const ws = createdSockets[0];
|
||||
ws.readyState = 0; // CONNECTING
|
||||
|
||||
act(() => {
|
||||
result.current.toggleItemCheck('item1', true);
|
||||
});
|
||||
|
||||
expect(ws.send).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue