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

[Autocomplete] Add test cases for createFilterOptions #20499

Merged
merged 2 commits into from
Apr 12, 2020
Merged
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
129 changes: 129 additions & 0 deletions packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,133 @@ describe('createFilterOptions', () => {
]);
});
});

describe('option: limit', () => {
it('limits the number of suggested options to be shown', () => {
const filterOptions = createFilterOptions({ limit: 2 });

const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'a1',
},
{
id: '5678',
name: 'a2',
},
{
id: '9abc',
name: 'a3',
},
{
id: '9abc',
name: 'a4',
},
];

expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal([
options[0],
options[1],
]);
});
});

describe('option: matchFrom', () => {
let filterOptions;
let getOptionLabel;
let options;
beforeEach(() => {
filterOptions = createFilterOptions({ matchFrom: 'any' });
getOptionLabel = (option) => option.name;
options = [
{
id: '1234',
name: 'ab',
},
{
id: '5678',
name: 'ba',
},
{
id: '9abc',
name: 'ca',
},
];
});

describe('any', () => {
it('show all results that match', () => {
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options);
});
});

describe('start', () => {
it('show only results that start with search', () => {
expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options);
});
});
});

describe('option: ignoreAccents', () => {
it('does not ignore accents', () => {
const filterOptions = createFilterOptions({ ignoreAccents: false });

const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'áb',
},
{
id: '5678',
name: 'ab',
},
{
id: '9abc',
name: 'áe',
},
{
id: '9abc',
name: 'ae',
},
];

expect(filterOptions(options, { inputValue: 'á', getOptionLabel })).to.deep.equal([
options[0],
options[2],
]);
});
});

describe('option: ignoreCase', () => {
it('matches results with case insensitive', () => {
const filterOptions = createFilterOptions({ ignoreCase: false });

const getOptionLabel = (option) => option.name;
const options = [
{
id: '1234',
name: 'Ab',
},
{
id: '5678',
name: 'ab',
},
{
id: '9abc',
name: 'Ae',
},
{
id: '9abc',
name: 'ae',
},
];

expect(filterOptions(options, { inputValue: 'A', getOptionLabel })).to.deep.equal([
options[0],
options[2],
]);
});
});
});