fix: Confirmations should apply to all actions (#1959)

This also refactors how generic HA actions are handled, and improves
typing of actions.


[skip ci]
This commit is contained in:
Dermot Duffy
2025-03-15 14:32:48 -07:00
committed by GitHub
parent a79ffa6edc
commit 75ae7720d3
90 changed files with 1332 additions and 854 deletions
+19 -40
View File
@@ -1,9 +1,7 @@
import { afterEach, describe, expect, it, vi } from 'vitest';
import { mock } from 'vitest-mock-extended';
import { actionSchema, INTERNAL_CALLBACK_ACTION } from '../../src/config/types.js';
import { hasAction as customCardHasAction } from '../../src/ha/has-action.js';
import { ActionConfig, INTERNAL_CALLBACK_ACTION } from '../../src/config/types.js';
import {
convertActionToCardCustomAction,
createCameraAction,
createDisplayModeAction,
createGeneralAction,
@@ -21,30 +19,6 @@ import {
stopEventFromActivatingCardWideActions,
} from '../../src/utils/action.js';
vi.mock('../../src/ha/has-action.js');
describe('convertActionToAdvancedCameraCardCustomAction', () => {
it('should skip null action', () => {
expect(convertActionToCardCustomAction(null)).toBeFalsy();
});
it('should parse valid', () => {
expect(
convertActionToCardCustomAction({
action: 'custom:advanced-camera-card-action',
advanced_camera_card_action: 'download',
}),
).toEqual({
action: 'fire-dom-event',
advanced_camera_card_action: 'download',
});
});
it('should not parse invalid', () => {
expect(convertActionToCardCustomAction('this is garbage')).toBeNull();
});
});
describe('createGeneralAction', () => {
it('should create general action', () => {
expect(
@@ -293,10 +267,7 @@ describe('createPerformAction', () => {
});
describe('getActionConfigGivenAction', () => {
const action = actionSchema.parse({
action: 'fire-dom-event',
advanced_camera_card_action: 'clips',
});
const action = createViewAction('clips');
it('should not handle undefined arguments', () => {
expect(getActionConfigGivenAction()).toBeNull();
@@ -348,21 +319,29 @@ describe('getActionConfigGivenAction', () => {
});
describe('hasAction', () => {
const action = actionSchema.parse({
action: 'toggle',
});
const realAction = createViewAction('clips');
const noneAction: ActionConfig = {
action: 'none',
};
afterEach(() => {
vi.clearAllMocks();
});
it('should handle non-array case', () => {
expect(hasAction(action)).toBeFalsy();
expect(customCardHasAction).toBeCalledTimes(1);
it('should return true for real action', () => {
expect(hasAction(realAction)).toBeTruthy();
});
it('should handle array case', () => {
expect(hasAction([action, action, action])).toBeFalsy();
expect(customCardHasAction).toBeCalledTimes(3);
it('should return false for none action', () => {
expect(hasAction(noneAction)).toBeFalsy();
});
it('should return true with an array of actions some real', () => {
expect(hasAction([noneAction, noneAction, realAction])).toBeTruthy();
});
it('should return false with an array of actions none real', () => {
expect(hasAction([noneAction, noneAction, noneAction])).toBeFalsy();
});
});