Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Select): selected values appearance #848

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {getSelectedOptionsContent} from '../utils';

const options = [
{value: 'val1', content: 'content1'},
{value: 'val2', content: 'content2'},
];

const presenceValue = ['val1'];
const notPresenceValue = ['val3'];

describe('getSelectedOptionsContent', () => {
describe('default appearance', () => {
test('option presence. Should return content', async () => {
const result = getSelectedOptionsContent(options, presenceValue);

expect(result).toEqual('content1');
});
test('option NOT presence. Should return value', async () => {
const result = getSelectedOptionsContent(options, notPresenceValue);

expect(result).toEqual('val3');
});
test('some of option NOT presence. Should return value', async () => {
const result = getSelectedOptionsContent(options, [
...presenceValue,
...notPresenceValue,
]);

expect(result).toEqual('content1, val3');
});
});
describe('renderSelectedOption callback', () => {
const renderSelectedOptionPostfix = 'from callback';
const renderSelectedOption = jest.fn(
(opt) => `${opt.content || opt.value}${renderSelectedOptionPostfix}` as any,
);

test('option presence. Should be called with option', async () => {
renderSelectedOption.mockClear();
getSelectedOptionsContent(options, presenceValue, renderSelectedOption);

expect(renderSelectedOption).toBeCalledTimes(1);
expect(renderSelectedOption).toBeCalledWith(options[0], 0);
});
test('option NOT presence. Should be called with generated object', async () => {
renderSelectedOption.mockClear();
getSelectedOptionsContent(options, notPresenceValue, renderSelectedOption);

expect(renderSelectedOption).toBeCalledTimes(1);
expect(renderSelectedOption).toBeCalledWith({value: notPresenceValue[0]}, 0);
});
});
});
15 changes: 6 additions & 9 deletions src/components/Select/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,14 @@ export const getSelectedOptionsContent = (
return null;
}

const selectedOptions = flattenOptions.reduce((acc, option) => {
if ('label' in option) {
return acc;
}

const optionSelected = value.includes(option.value);
const flattenSimpleOptions = flattenOptions.filter(
(opt) => !('label' in opt),
) as SelectOption[];

if (optionSelected) {
acc.push(option);
}
const selectedOptions = value.reduce((acc, val) => {
const selectedOption = flattenSimpleOptions.find((opt) => opt.value === val);

acc.push(selectedOption || {value: val});
return acc;
}, [] as SelectOption[]);

Expand Down
Loading