fix: Substantially reduce cases where errors block whole card (#1764)
This commit is contained in:
@@ -1,22 +1,17 @@
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { getEndpointAddressOrDispatchError } from '../../src/utils/endpoint';
|
||||
import { convertEndpointAddressToSignedWebsocket } from '../../src/utils/endpoint';
|
||||
import { homeAssistantSignPath } from '../../src/utils/ha';
|
||||
import { createHASS } from '../test-utils';
|
||||
|
||||
vi.mock('../../src/utils/ha');
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('getEndpointAddressOrDispatchError', () => {
|
||||
describe('convertEndpointAddressToSignedWebsocket', () => {
|
||||
it('without signing', async () => {
|
||||
expect(
|
||||
await getEndpointAddressOrDispatchError(
|
||||
document.createElement('div'),
|
||||
createHASS(),
|
||||
{
|
||||
endpoint: 'http://example.com',
|
||||
sign: false,
|
||||
},
|
||||
),
|
||||
await convertEndpointAddressToSignedWebsocket(createHASS(), {
|
||||
endpoint: 'http://example.com',
|
||||
sign: false,
|
||||
}),
|
||||
).toBe('http://example.com');
|
||||
});
|
||||
|
||||
@@ -29,51 +24,31 @@ describe('getEndpointAddressOrDispatchError', () => {
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue('http://signed.com');
|
||||
|
||||
expect(
|
||||
await getEndpointAddressOrDispatchError(
|
||||
document.createElement('div'),
|
||||
createHASS(),
|
||||
{
|
||||
endpoint: 'http://example.com',
|
||||
sign: true,
|
||||
},
|
||||
),
|
||||
await convertEndpointAddressToSignedWebsocket(createHASS(), {
|
||||
endpoint: 'http://example.com',
|
||||
sign: true,
|
||||
}),
|
||||
).toBe('ws://signed.com');
|
||||
});
|
||||
|
||||
it('with null response', async () => {
|
||||
const element = document.createElement('div');
|
||||
const listener = vi.fn();
|
||||
element.addEventListener('frigate-card:message', listener);
|
||||
|
||||
vi.mocked(homeAssistantSignPath).mockResolvedValue(null);
|
||||
|
||||
expect(
|
||||
await getEndpointAddressOrDispatchError(element, createHASS(), {
|
||||
await convertEndpointAddressToSignedWebsocket(createHASS(), {
|
||||
endpoint: 'http://example.com',
|
||||
sign: true,
|
||||
}),
|
||||
).toBeNull();
|
||||
|
||||
expect(listener).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
detail: expect.objectContaining({
|
||||
message: 'Could not sign Home Assistant URL',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('with exception on signing', async () => {
|
||||
const consoleSpy = vi.spyOn(global.console, 'warn').mockReturnValue(undefined);
|
||||
|
||||
const element = document.createElement('div');
|
||||
const listener = vi.fn();
|
||||
element.addEventListener('frigate-card:message', listener);
|
||||
|
||||
vi.mocked(homeAssistantSignPath).mockRejectedValue(new Error());
|
||||
|
||||
expect(
|
||||
await getEndpointAddressOrDispatchError(element, createHASS(), {
|
||||
await convertEndpointAddressToSignedWebsocket(createHASS(), {
|
||||
endpoint: 'http://example.com',
|
||||
sign: true,
|
||||
}),
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { createCameraConfig, createHASS, createStateEntity } from '../test-utils';
|
||||
import { getStateObjOrDispatchError } from '../../src/utils/get-state-obj';
|
||||
|
||||
// @vitest-environment jsdom
|
||||
describe('getStateObjOrDispatchError', () => {
|
||||
it('should retrieve valid state object', () => {
|
||||
const messageHandler = vi.fn();
|
||||
const element = document.createElement('div');
|
||||
element.addEventListener('frigate-card:message', messageHandler);
|
||||
const state = createStateEntity();
|
||||
|
||||
expect(
|
||||
getStateObjOrDispatchError(
|
||||
element,
|
||||
createHASS({
|
||||
'camera.test': state,
|
||||
}),
|
||||
createCameraConfig({
|
||||
camera_entity: 'camera.test',
|
||||
}),
|
||||
),
|
||||
).toBe(state);
|
||||
|
||||
expect(messageHandler).not.toBeCalled();
|
||||
});
|
||||
|
||||
it('should dispatch unspecified entity', () => {
|
||||
const messageHandler = vi.fn();
|
||||
const element = document.createElement('div');
|
||||
element.addEventListener('frigate-card:message', messageHandler);
|
||||
|
||||
expect(
|
||||
getStateObjOrDispatchError(element, createHASS(), createCameraConfig()),
|
||||
).toBeNull();
|
||||
|
||||
expect(messageHandler).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
detail: expect.objectContaining({
|
||||
message:
|
||||
'The camera_entity parameter must be set and valid for this live provider',
|
||||
type: 'error',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('should dispatch not found state', () => {
|
||||
const messageHandler = vi.fn();
|
||||
const element = document.createElement('div');
|
||||
element.addEventListener('frigate-card:message', messageHandler);
|
||||
|
||||
expect(
|
||||
getStateObjOrDispatchError(
|
||||
element,
|
||||
createHASS(),
|
||||
createCameraConfig({
|
||||
camera_entity: 'camera.will-not-be-found',
|
||||
}),
|
||||
),
|
||||
).toBeNull();
|
||||
|
||||
expect(messageHandler).toBeCalledWith(
|
||||
expect.objectContaining({
|
||||
detail: expect.objectContaining({
|
||||
message: 'The configured camera_entity was not found',
|
||||
type: 'error',
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user