Skip to content

Commit b2a1a00

Browse files
authored
[LG-5457] feat(suggestions): restrict ConfigurationParameter types to enhance API clarity (#3093)
* refactor(suggestions): clean up ConfigurationParameter types for better state management * chore(suggestions): changeset * test(suggestions): add type specs
1 parent e245037 commit b2a1a00

File tree

5 files changed

+89
-10
lines changed

5 files changed

+89
-10
lines changed

.changeset/cute-zebras-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lg-chat/suggestions': patch
3+
---
4+
5+
[LG-5457](https://jira.mongodb.org/browse/LG-5457): restrict `ConfigurationParameter` types to enhance API clarity

chat/suggestions/src/StatusBanner/StatusBanner.tsx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import React from 'react';
22

33
import Banner, { Variant as BannerVariant } from '@leafygreen-ui/banner';
44

5-
import { ConfigurationParameter, State } from '../shared.types';
5+
import {
6+
ErrorConfigurationParameter,
7+
State,
8+
SuccessConfigurationParameter,
9+
} from '../shared.types';
610

711
import {
812
bannerWrapperStyles,
@@ -16,11 +20,13 @@ export const StatusBanner = ({
1620
failedParameters,
1721
}: {
1822
state: State;
19-
appliedParameters?: Array<ConfigurationParameter>;
20-
failedParameters?: Array<ConfigurationParameter>;
23+
appliedParameters?: Array<SuccessConfigurationParameter>;
24+
failedParameters?: Array<ErrorConfigurationParameter>;
2125
}) => {
2226
const clusterConfigurationBannerContent = (
23-
parameters?: Array<ConfigurationParameter>,
27+
parameters?: Array<
28+
SuccessConfigurationParameter | ErrorConfigurationParameter
29+
>,
2430
) => {
2531
return (
2632
<ul className={listStyles}>

chat/suggestions/src/SuggestedActions/SuggestedActions.spec.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,57 @@ describe('chat/suggestions', () => {
586586
});
587587
});
588588
});
589+
590+
/* eslint-disable jest/no-disabled-tests */
591+
describe.skip('types behave as expected', () => {
592+
test('SuggestedActions throws error when no required props are provided', () => {
593+
// @ts-expect-error - missing required props
594+
<SuggestedActions />;
595+
});
596+
597+
test('SuggestedActions throws error when missing configurationParameters', () => {
598+
// @ts-expect-error - missing configurationParameters
599+
<SuggestedActions state={State.Unset} onClickApply={() => {}} />;
600+
});
601+
602+
test('SuggestedActions throws error when missing onClickApply', () => {
603+
// @ts-expect-error - missing onClickApply
604+
<SuggestedActions
605+
state={State.Unset}
606+
configurationParameters={[{ key: 'test', value: 'test' }]}
607+
/>;
608+
});
609+
610+
test('SuggestedActions throws error when missing state', () => {
611+
// @ts-expect-error - missing state
612+
<SuggestedActions
613+
configurationParameters={[{ key: 'test', value: 'test' }]}
614+
onClickApply={() => {}}
615+
/>;
616+
});
617+
618+
test('SuggestedActions accepts configurationParameters with allowed state values', () => {
619+
<SuggestedActions
620+
state={State.Success}
621+
configurationParameters={[
622+
{ key: 'unset', value: 'value' },
623+
{ key: 'explicit-unset', value: 'value', state: 'unset' },
624+
{ key: 'success', value: 'value', state: 'success' },
625+
{ key: 'error', value: 'value', state: 'error' },
626+
]}
627+
onClickApply={() => {}}
628+
/>;
629+
});
630+
631+
test('SuggestedActions does not accept configurationParameters with state "apply"', () => {
632+
<SuggestedActions
633+
state={State.Success}
634+
configurationParameters={[
635+
// @ts-expect-error - "apply" is not a valid state for configurationParameters
636+
{ key: 'apply', value: 'value', state: State.Apply },
637+
]}
638+
onClickApply={() => {}}
639+
/>;
640+
});
641+
});
589642
});

chat/suggestions/src/SuggestedActions/SuggestedActions.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface SuggestedActionsProps
99
DarkModeProps {
1010
/**
1111
* Configuration parameters with their individual state values.
12-
* Each parameter includes a key, value, and state (unset/apply/success/error).
12+
* Each parameter includes a key, value, and state (unset/success/error).
1313
*/
1414
configurationParameters: ConfigurationParameters;
1515

chat/suggestions/src/shared.types.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,30 @@ export const State = {
99
} as const;
1010
export type State = (typeof State)[keyof typeof State];
1111

12-
/**
13-
* A single configuration parameter with its key, value, and current state
14-
*/
15-
export interface ConfigurationParameter {
12+
interface BaseConfigurationParameter {
1613
key: string;
1714
value: string;
18-
state?: State; // Defaults to 'unset' if not specified
1915
}
2016

17+
interface UnsetConfigurationParameter extends BaseConfigurationParameter {
18+
state?: 'unset';
19+
}
20+
21+
export interface SuccessConfigurationParameter
22+
extends BaseConfigurationParameter {
23+
state: 'success';
24+
}
25+
26+
export interface ErrorConfigurationParameter
27+
extends BaseConfigurationParameter {
28+
state: 'error';
29+
}
30+
31+
export type ConfigurationParameter =
32+
| SuccessConfigurationParameter
33+
| ErrorConfigurationParameter
34+
| UnsetConfigurationParameter;
35+
2136
/**
2237
* Array of configuration parameters, each with their own state.
2338
*/

0 commit comments

Comments
 (0)