Files
advanced-camera-card/tests/card-controller/interaction-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

91 lines
2.7 KiB
TypeScript

import { add } from 'date-fns';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { InteractionManager } from '../../src/card-controller/interaction-manager';
import { createCardAPI, createConfig, createLitElement } from '../test-utils';
vi.mock('lodash-es/throttle', () => ({
default: vi.fn((fn) => fn),
}));
// @vitest-environment jsdom
describe('InteractionManager', () => {
const start = new Date('2023-09-24T20:20:00');
afterEach(() => {
vi.useRealTimers();
});
it('should initialize', () => {
const api = createCardAPI();
const element = createLitElement();
vi.mocked(api.getCardElementManager().getElement).mockReturnValue(element);
const manager = new InteractionManager(api);
manager.initialize();
expect(api.getConditionStateManager().setState).toBeCalledWith({
interaction: false,
});
expect(element.getAttribute('interaction')).toBeNull();
});
it('should still report interaction without an interaction timeout', () => {
const api = createCardAPI();
const element = createLitElement();
vi.mocked(api.getCardElementManager().getElement).mockReturnValue(element);
vi.mocked(api.getConfigManager().getConfig).mockReturnValue(
createConfig({
view: {
interaction_seconds: 0,
},
}),
);
const manager = new InteractionManager(api);
manager.reportInteraction();
expect(element.getAttribute('interaction')).not.toBeNull();
expect(manager.hasInteraction()).toBeTruthy();
});
it('should set condition state', () => {
const api = createCardAPI();
const element = createLitElement();
vi.mocked(api.getCardElementManager().getElement).mockReturnValue(element);
vi.mocked(api.getConfigManager().getConfig).mockReturnValue(
createConfig({
view: {
interaction_seconds: 10,
},
}),
);
const manager = new InteractionManager(api);
vi.useFakeTimers();
vi.setSystemTime(start);
expect(api.getConditionStateManager().setState).not.toBeCalled();
manager.reportInteraction();
expect(api.getConditionStateManager().setState).toHaveBeenLastCalledWith(
expect.objectContaining({
interaction: true,
}),
);
expect(manager.hasInteraction()).toBeTruthy();
expect(element.getAttribute('interaction')).not.toBeNull();
vi.setSystemTime(add(start, { seconds: 10 }));
vi.runOnlyPendingTimers();
expect(api.getConditionStateManager().setState).toHaveBeenLastCalledWith(
expect.objectContaining({
interaction: false,
}),
);
expect(manager.hasInteraction()).toBeFalsy();
expect(element.getAttribute('interaction')).toBeNull();
});
});