Skip to content

Commit

Permalink
[Autocomplete] Add test cases for createFilterOptions (#20499)
Browse files Browse the repository at this point in the history
  • Loading branch information
netochaves authored Apr 12, 2020
1 parent c2e088b commit dc0578c
Showing 1 changed file with 129 additions and 0 deletions.
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],
]);
});
});
});

0 comments on commit dc0578c

Please sign in to comment.