Skip to content

Commit d025295

Browse files
committed
Update type check to check for string or number, update test-utils
1 parent 6466950 commit d025295

File tree

5 files changed

+30
-23
lines changed

5 files changed

+30
-23
lines changed

src/__tests__/snapshot-tests/__snapshots__/test-utils-selectors.test.tsx.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,9 @@ exports[`test-utils selectors 1`] = `
657657
"awsui_root_1epxo",
658658
],
659659
"token-group": [
660+
"awsui_dismiss-button_dm8gx",
660661
"awsui_root_dm8gx",
662+
"awsui_token_dm8gx",
661663
],
662664
"top-navigation": [
663665
"awsui_hidden_k5dlb",

src/test-utils/dom/token-group/index.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export default class TokenGroupWrapper extends ComponentWrapper {
1111
static rootSelector: string = selectors.root;
1212

1313
findTokens(): Array<TokenGroupItemWrapper> {
14-
return this.findAllByClassName(TokenGroupItemWrapper.rootSelector).map(
14+
const legacyTokens = this.findAllByClassName(TokenGroupItemWrapper.legacyRootSelector);
15+
const newTokens = this.findAllByClassName(TokenGroupItemWrapper.newRootSelector);
16+
return (Array(legacyTokens).length > 0 ? legacyTokens : newTokens).map(
1517
tokenElement => new TokenGroupItemWrapper(tokenElement.getElement())
1618
);
1719
}
@@ -22,9 +24,15 @@ export default class TokenGroupWrapper extends ComponentWrapper {
2224
* @param tokenIndex 1-based index of the token to return.
2325
*/
2426
findToken(tokenIndex: number): TokenGroupItemWrapper | null {
25-
return this.findComponent(
26-
`.${tokenListSelectors['list-item']}:nth-child(${tokenIndex}) > .${TokenGroupItemWrapper.rootSelector}`,
27-
TokenGroupItemWrapper
27+
return (
28+
this.findComponent(
29+
`.${tokenListSelectors['list-item']}:nth-child(${tokenIndex}) > .${TokenGroupItemWrapper.legacyRootSelector}`,
30+
TokenGroupItemWrapper
31+
) ??
32+
this.findComponent(
33+
`.${tokenListSelectors['list-item']}:nth-child(${tokenIndex}) > .${TokenGroupItemWrapper.newRootSelector}`,
34+
TokenGroupItemWrapper
35+
)
2836
);
2937
}
3038

src/test-utils/dom/token-group/token.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { ComponentWrapper, ElementWrapper } from '@cloudscape-design/test-utils-
44

55
import OptionWrapper from '../internal/option';
66

7-
import selectors from '../../../token/test-classes/styles.selectors.js';
7+
import newSelectors from '../../../token/test-classes/styles.selectors.js';
8+
import legacySelectors from '../../../token-group/styles.selectors.js';
89

910
export class TokenGroupItemWrapper extends ComponentWrapper {
10-
static rootSelector: string = selectors.root;
11+
static newRootSelector: string = newSelectors.root;
12+
static legacyRootSelector: string = legacySelectors.token;
1113

1214
findOption(): OptionWrapper {
1315
return this.findComponent(`.${OptionWrapper.rootSelector}`, OptionWrapper)!;
@@ -18,6 +20,9 @@ export class TokenGroupItemWrapper extends ComponentWrapper {
1820
}
1921

2022
findDismiss(): ElementWrapper {
21-
return this.findByClassName(selectors['dismiss-button'])!;
23+
const newSelector = newSelectors['dismiss-button'];
24+
const oldSelector = legacySelectors['dismiss-button'];
25+
26+
return this.findAny(oldSelector, newSelector)!;
2227
}
2328
}

src/token-group/styles.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
@use '../internal/styles' as styles;
77
@use '../internal/styles/tokens' as awsui;
88

9+
.dismiss-button,
10+
.token {
11+
/* used in test-utils */
12+
}
13+
914
.root {
1015
@include styles.styles-reset;
1116

src/token/internal.tsx

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ import analyticsSelectors from './analytics-metadata/styles.css.js';
1818
import styles from './styles.css.js';
1919
import testUtilStyles from './test-classes/styles.css.js';
2020

21-
/**
22-
* Checks if a value is a React element without using the legacy React.isValidElement API.
23-
* This approach checks for the internal structure of React elements.
24-
*/
25-
function isReactElement(value: unknown): value is React.ReactElement {
26-
return (
27-
typeof value === 'object' &&
28-
value !== null &&
29-
typeof (value as any).type !== 'undefined' &&
30-
typeof (value as any).props !== 'undefined'
31-
);
32-
}
33-
3421
type InternalTokenProps = TokenProps &
3522
InternalBaseComponentProps & {
3623
role?: string;
@@ -84,11 +71,11 @@ function InternalToken({
8471
});
8572

8673
const buildOptionDefinition = () => {
87-
const isLabelAnElement = isReactElement(label);
88-
const labelObject = !isLabelAnElement ? { label: String(label) } : { labelContent: label };
74+
const isLabelStringOrNumber = typeof label === 'string' || typeof label === 'number';
75+
const labelObject = isLabelStringOrNumber ? { label: String(label) } : { labelContent: label };
8976

9077
if (isInline) {
91-
if (isLabelAnElement) {
78+
if (!isLabelStringOrNumber) {
9279
warnOnce('Label', `Only plain text (strings or numbers) are supported when variant="inline".`);
9380
}
9481

0 commit comments

Comments
 (0)