Skip to content

Commit

Permalink
1099-improve-the-list-of-available-tokens-in-a-dropdown (#1191)
Browse files Browse the repository at this point in the history
  • Loading branch information
swordEdge authored Aug 22, 2022
1 parent 3eabc04 commit 31e3c60
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 4 deletions.
76 changes: 76 additions & 0 deletions src/app/components/DownshiftInput/DownShiftInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
import React from 'react';
import { DownshiftInput } from './DownshiftInput';
import { render } from '../../../../tests/config/setupTest';
import { SingleToken } from '@/types/tokens';

const resolvedTokens = [
{
internal__Parent: 'core',
name: 'border-radius.0',
rawValue: '64px',
type: 'borderRadius',
value: '64px',
},
{
internal__Parent: 'core',
name: 'border-radius.1',
rawValue: '1px',
type: 'borderRadius',
value: '1px',
},
{
internal__Parent: 'core',
name: 'border-radius.2',
rawValue: '2px',
type: 'borderRadius',
value: '2px',
},
{
internal__Parent: 'core',
name: 'color.slate.200',
rawValue: '#e2e8f0',
type: 'color',
value: '#e2e8f0',
},
{
internal__Parent: 'core',
name: 'color.slate.300',
rawValue: '#cbd5e1',
type: 'color',
value: '#cbd5e1',
},
{
internal__Parent: 'core',
name: 'size.0',
rawValue: '0',
type: 'sizing',
value: 0,
},
{
internal__Parent: 'core',
name: 'size.12',
rawValue: '1',
type: 'sizing',
value: 1,
},
] as SingleToken[];

const mockSetInputValue = jest.fn();
const mockHandleChange = jest.fn();

describe('DownShiftInput', () => {
it('filteredValue should only replace {} or $ and remain all letters', () => {
const dataStore = [
Expand Down Expand Up @@ -30,4 +90,20 @@ describe('DownShiftInput', () => {
expect(data.input.replace(/[{}$]/g, '')).toBe(data.output);
});
});

it('should return matching tokens', () => {
const result = render(
<DownshiftInput
type="color"
resolvedTokens={resolvedTokens}
setInputValue={mockSetInputValue}
handleChange={mockHandleChange}
value="{"
suffix
/>,
);
result.getByTestId('downshift-input-suffix-button').click();
expect(result.getByText('#e2e8f0')).toBeInTheDocument();
expect(result.getByText('#cbd5e1')).toBeInTheDocument();
});
});
7 changes: 4 additions & 3 deletions src/app/components/DownshiftInput/DownshiftInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export const DownshiftInput: React.FunctionComponent<DownShiftProps> = ({
}) => {
const [showAutoSuggest, setShowAutoSuggest] = React.useState<boolean>(false);
const [isFirstLoading, setisFirstLoading] = React.useState<boolean>(true);

const filteredValue = useMemo(() => ((showAutoSuggest || typeof value !== 'string') ? '' : value?.replace(/[{}$]/g, '')), [
showAutoSuggest,
value,
Expand All @@ -140,7 +139,9 @@ export const DownshiftInput: React.FunctionComponent<DownShiftProps> = ({
.filter(
(token: SingleToken) => !filteredValue || token.name.toLowerCase().includes(filteredValue.toLowerCase()),
)
.filter((token: SingleToken) => token?.type === type && token.name !== initialName),
.filter((token: SingleToken) => token?.type === type && token.name !== initialName).sort((a, b) => (
a.name.localeCompare(b.name)
)),
[resolvedTokens, filteredValue, type],
);

Expand Down Expand Up @@ -206,7 +207,7 @@ export const DownshiftInput: React.FunctionComponent<DownShiftProps> = ({
getInputProps={getInputProps}
/>
{suffix && (
<StyledInputSuffix type="button" onClick={handleAutoSuggest}>
<StyledInputSuffix type="button" data-testid="downshift-input-suffix-button" onClick={handleAutoSuggest}>
<StyledIconDisclosure />
</StyledInputSuffix>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { TokenTypes } from '@/constants/TokenTypes';
import { AnyTokenList } from '@/types/tokens';
import React from 'react';
import { Provider } from 'react-redux';
import {
render, fireEvent, createMockStore,
Expand Down

0 comments on commit 31e3c60

Please sign in to comment.