Files
advanced-camera-card/tests/conditions/state-manager.test.ts
T
Dermot Duffy 8d3cf07b43 refactor: Significantly refactor how conditions work internally (#1886)
- Much improved API cleanliness and testability to allow further
extensibility in future
 - Simplified code in `live` view

This technically contains a small change in how overrides work in the
`live` view. Since that change is _closer_ to the documentation, and
since this is likely to be rarely used, this is not considered a
breaking change. Previously, overrides for a given live camera would
always render _as if_ that camera was selected, vs was actually
selected. Now, overrides will only apply in the live view when the
camera is _actually_ selected. If this is an issue for you in practice,
lets discuss.
2025-02-11 20:07:55 -08:00

110 lines
2.7 KiB
TypeScript

import { afterEach, describe, expect, it, vi } from 'vitest';
import { ConditionStateManager } from '../../src/conditions/state-manager';
import { createStateEntity } from '../test-utils';
describe('ConditionStateManager', () => {
afterEach(() => {
vi.restoreAllMocks();
});
it('should get state', () => {
const state = { fullscreen: true };
const manager = new ConditionStateManager();
manager.setState(state);
expect(manager.getState()).toEqual(state);
});
describe('should set state', () => {
it('should set and be able to get it again', () => {
const state = {
fullscreen: true,
};
const manager = new ConditionStateManager();
manager.setState(state);
expect(manager.getState()).toEqual(state);
});
it('should set but only trigger when necessary', () => {
const listener = vi.fn();
const manager = new ConditionStateManager();
manager.addListener(listener);
const state = {
fullscreen: true,
};
manager.setState(state);
expect(listener).toBeCalledTimes(1);
manager.setState(state);
expect(listener).toBeCalledTimes(1);
manager.setState({ ...state });
expect(listener).toBeCalledTimes(1);
manager.setState({
state: {
'binary_sensor.foo': createStateEntity(),
},
});
expect(listener).toBeCalledTimes(2);
manager.setState({ fullscreen: true });
expect(listener).toBeCalledTimes(2);
manager.setState({
state: {
'binary_sensor.foo': createStateEntity(),
},
});
expect(listener).toBeCalledTimes(2);
manager.setState({ fullscreen: false });
expect(listener).toBeCalledTimes(3);
manager.setState({ fullscreen: false });
expect(listener).toBeCalledTimes(3);
manager.setState({
state: {
'binary_sensor.foo': createStateEntity({ state: 'off' }),
},
});
expect(listener).toBeCalledTimes(4);
});
});
it('should add listener', () => {
const listener = vi.fn();
const manager = new ConditionStateManager();
manager.setState({ fullscreen: true });
manager.addListener(listener);
manager.setState({ expand: true });
expect(listener).toBeCalledWith({
old: { fullscreen: true },
change: { expand: true },
new: { fullscreen: true, expand: true },
});
});
it('should remove listener', () => {
const listener = vi.fn();
const manager = new ConditionStateManager();
manager.addListener(listener);
manager.removeListener(listener);
const state = { fullscreen: true };
manager.setState(state);
expect(listener).not.toBeCalled();
});
});