test: Split test utilities to improve test times (#2622)
This commit is contained in:
@@ -45,7 +45,7 @@ describe('ConditionsManager', () => {
|
||||
hass: createHASS({ 'sensor.foo': createStateEntity({ state: '11' }) }),
|
||||
});
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should forward the triggering state change to listeners', () => {
|
||||
@@ -94,11 +94,11 @@ describe('ConditionsManager', () => {
|
||||
|
||||
// Fire the media-query change; the manager re-evaluates and notifies.
|
||||
addEventListener.mock.calls[0][1]();
|
||||
expect(listener).toBeCalledWith({ result: true }, undefined);
|
||||
expect(listener).toHaveBeenCalledWith({ result: true }, undefined);
|
||||
|
||||
// Destroy tears the subscription down.
|
||||
manager.destroy();
|
||||
expect(removeEventListener).toBeCalled();
|
||||
expect(removeEventListener).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
describe('should handle listeners correctly', () => {
|
||||
@@ -115,20 +115,20 @@ describe('ConditionsManager', () => {
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).toBeCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
stateManager.setState({ fullscreen: false });
|
||||
expect(listener).toBeCalledWith({ result: false }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
expect(listener).toHaveBeenCalledWith({ result: false }, expect.anything());
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
// Re-add the same listener (will still only be called once).
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).toBeCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
expect(listener).toHaveBeenCalledWith({ result: true }, expect.anything());
|
||||
expect(listener).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
|
||||
it('should remove listener', () => {
|
||||
@@ -145,7 +145,7 @@ describe('ConditionsManager', () => {
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should remove listener on destroy', () => {
|
||||
@@ -162,7 +162,7 @@ describe('ConditionsManager', () => {
|
||||
|
||||
stateManager.setState({ fullscreen: true });
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should not call listeners when the condition result does not change', () => {
|
||||
@@ -177,19 +177,19 @@ describe('ConditionsManager', () => {
|
||||
manager.addListener(listener);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
stateManager.setState({ view: 'clip' });
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
stateManager.setState({ view: 'clip' });
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
expect(listener).toHaveBeenCalledTimes(3);
|
||||
|
||||
stateManager.setState({ view: 'live' });
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
expect(listener).toHaveBeenCalledTimes(3);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -42,13 +42,13 @@ describe('ConditionStateManager', () => {
|
||||
};
|
||||
|
||||
expect(manager.setState(state)).toBe(true);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(manager.setState(state)).toBe(false);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(manager.setState({ ...state })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(1);
|
||||
expect(listener).toHaveBeenCalledTimes(1);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
@@ -57,10 +57,10 @@ describe('ConditionStateManager', () => {
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(manager.setState({ fullscreen: true })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(2);
|
||||
expect(listener).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
@@ -69,13 +69,13 @@ describe('ConditionStateManager', () => {
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(3);
|
||||
expect(listener).toHaveBeenCalledTimes(3);
|
||||
|
||||
expect(manager.setState({ fullscreen: false })).toBe(true);
|
||||
expect(listener).toBeCalledTimes(4);
|
||||
expect(listener).toHaveBeenCalledTimes(4);
|
||||
|
||||
expect(manager.setState({ fullscreen: false })).toBe(false);
|
||||
expect(listener).toBeCalledTimes(4);
|
||||
expect(listener).toHaveBeenCalledTimes(4);
|
||||
|
||||
expect(
|
||||
manager.setState({
|
||||
@@ -84,7 +84,7 @@ describe('ConditionStateManager', () => {
|
||||
}),
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(listener).toBeCalledTimes(5);
|
||||
expect(listener).toHaveBeenCalledTimes(5);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -138,7 +138,7 @@ describe('ConditionStateManager', () => {
|
||||
|
||||
manager.setState({ expand: true });
|
||||
|
||||
expect(listener).toBeCalledWith({
|
||||
expect(listener).toHaveBeenCalledWith({
|
||||
old: { fullscreen: true },
|
||||
change: { expand: true },
|
||||
new: { fullscreen: true, expand: true },
|
||||
@@ -155,6 +155,6 @@ describe('ConditionStateManager', () => {
|
||||
const state = { fullscreen: true };
|
||||
manager.setState(state);
|
||||
|
||||
expect(listener).not.toBeCalled();
|
||||
expect(listener).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,7 @@ import { describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import { ConditionStateManager } from '../../../../src/condition-trigger/conditions/state-manager';
|
||||
import { CameraTrigger } from '../../../../src/condition-trigger/triggers/triggers/camera';
|
||||
import type { TriggerOfType } from '../../../../src/condition-trigger/triggers/triggers/types';
|
||||
import { createConfig } from '../../../test-utils';
|
||||
import { createConfig } from '../../../config/test-utils';
|
||||
import { createTriggerEvaluatorContext } from './test-utils';
|
||||
|
||||
describe('CameraTrigger', () => {
|
||||
|
||||
@@ -3,7 +3,7 @@ import { describe, expect, it, vi, type Mock } from 'vitest';
|
||||
import { ConditionStateManager } from '../../../../src/condition-trigger/conditions/state-manager';
|
||||
import { ConfigTrigger } from '../../../../src/condition-trigger/triggers/triggers/config';
|
||||
import type { TriggerOfType } from '../../../../src/condition-trigger/triggers/triggers/types';
|
||||
import { createConfig } from '../../../test-utils';
|
||||
import { createConfig } from '../../../config/test-utils';
|
||||
import { createTriggerEvaluatorContext } from './test-utils';
|
||||
|
||||
describe('ConfigTrigger', () => {
|
||||
|
||||
@@ -49,7 +49,7 @@ describe('EventTrigger', () => {
|
||||
event_type: 'zha_event',
|
||||
});
|
||||
trigger.subscribe(callback);
|
||||
expect(eventWatcher.subscribe).toBeCalledTimes(1);
|
||||
expect(eventWatcher.subscribe).toHaveBeenCalledTimes(1);
|
||||
expect(vi.mocked(eventWatcher.subscribe).mock.calls[0][0].event_type).toBe(
|
||||
'zha_event',
|
||||
);
|
||||
@@ -63,7 +63,7 @@ describe('EventTrigger', () => {
|
||||
event_type: ['zha_event', 'zha_event'],
|
||||
});
|
||||
trigger.subscribe(callback);
|
||||
expect(eventWatcher.subscribe).toBeCalledTimes(1);
|
||||
expect(eventWatcher.subscribe).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should expand list-form event_type into one request per type', () => {
|
||||
@@ -72,7 +72,7 @@ describe('EventTrigger', () => {
|
||||
event_type: ['zha_event', 'deconz_event'],
|
||||
});
|
||||
trigger.subscribe(callback);
|
||||
expect(eventWatcher.subscribe).toBeCalledTimes(2);
|
||||
expect(eventWatcher.subscribe).toHaveBeenCalledTimes(2);
|
||||
expect(vi.mocked(eventWatcher.subscribe).mock.calls[0][0].event_type).toBe(
|
||||
'zha_event',
|
||||
);
|
||||
@@ -89,7 +89,7 @@ describe('EventTrigger', () => {
|
||||
trigger.subscribe(callback);
|
||||
const event = createHASSEvent('zha_event', { command: 'press' });
|
||||
callEventCallback(eventWatcher, event);
|
||||
expect(callback).toBeCalledWith({ platform: 'event', event });
|
||||
expect(callback).toHaveBeenCalledWith({ platform: 'event', event });
|
||||
});
|
||||
|
||||
it('should omit the matcher when neither event_data nor context is set', () => {
|
||||
@@ -164,7 +164,7 @@ describe('EventTrigger', () => {
|
||||
});
|
||||
trigger.subscribe(callback);
|
||||
trigger.destroy();
|
||||
expect(eventWatcher.unsubscribe).toBeCalledTimes(2);
|
||||
expect(eventWatcher.unsubscribe).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should be a no-op when destroyed without subscribing', () => {
|
||||
@@ -173,6 +173,6 @@ describe('EventTrigger', () => {
|
||||
event_type: 'zha_event',
|
||||
});
|
||||
trigger.destroy();
|
||||
expect(eventWatcher.unsubscribe).not.toBeCalled();
|
||||
expect(eventWatcher.unsubscribe).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user