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

Variables: Fixes filtering in picker with null items #31979

Merged
merged 3 commits into from Mar 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -650,19 +650,17 @@ describe('optionsPickerReducer', () => {
});
});

describe('when updateOptionsAndFilter is dispatched and searchFilter exists', () => {
describe('when updateOptionsAndFilter is dispatched and queryValue exists', () => {
it('then state should be correct', () => {
const searchQuery = 'A';
const queryValue = 'A';

const options = [
{ text: 'All', value: '$__all', selected: true },
{ text: 'A', value: 'A', selected: false },
{ text: 'B', value: 'B', selected: false },
];

const { initialState } = getVariableTestContext({
queryValue: searchQuery,
});
const { initialState } = getVariableTestContext({ queryValue });

reducerTester<OptionsPickerState>()
.givenReducer(optionsPickerReducer, cloneDeep(initialState))
Expand All @@ -674,23 +672,46 @@ describe('optionsPickerReducer', () => {
{ text: 'A', value: 'A', selected: false },
],
selectedValues: [{ text: 'All', value: '$__all', selected: true }],
queryValue: searchQuery,
queryValue: 'A',
highlightIndex: 0,
});
});

describe('but option is null', () => {
it('then state should be correct', () => {
const queryValue = 'A';

const options: any = [
{ text: 'All', value: '$__all', selected: true },
{ text: null, value: null, selected: false },
{ text: [null], value: [null], selected: false },
];

const { initialState } = getVariableTestContext({ queryValue });

reducerTester<OptionsPickerState>()
.givenReducer(optionsPickerReducer, cloneDeep(initialState))
.whenActionIsDispatched(updateOptionsAndFilter(options))
.thenStateShouldEqual({
...initialState,
options: [{ text: 'All', value: '$__all', selected: true }],
selectedValues: [{ text: 'All', value: '$__all', selected: true }],
queryValue: 'A',
highlightIndex: 0,
});
});
});

describe('and option count is are greater then OPTIONS_LIMIT', () => {
it('then state should be correct', () => {
const searchQuery = 'option:1337';
const queryValue = 'option:1337';

const options = [];
for (let index = 0; index <= OPTIONS_LIMIT + 337; index++) {
options.push({ text: `option:${index}`, value: `option:${index}`, selected: false });
}

const { initialState } = getVariableTestContext({
queryValue: searchQuery,
});
const { initialState } = getVariableTestContext({ queryValue });

reducerTester<OptionsPickerState>()
.givenReducer(optionsPickerReducer, cloneDeep(initialState))
Expand All @@ -708,7 +729,7 @@ describe('optionsPickerReducer', () => {

describe('when value is selected and filter is applied but then removed', () => {
it('then state should be correct', () => {
const searchQuery = 'A';
const queryValue = 'A';

const options: VariableOption[] = [
{ text: 'All', value: '$__all', selected: false },
Expand All @@ -732,7 +753,7 @@ describe('optionsPickerReducer', () => {
],
selectedValues: [{ text: 'B', value: 'B', selected: true }],
})
.whenActionIsDispatched(updateSearchQuery(searchQuery))
.whenActionIsDispatched(updateSearchQuery(queryValue))
.thenStateShouldEqual({
...initialState,
options: [
Expand All @@ -741,7 +762,7 @@ describe('optionsPickerReducer', () => {
{ text: 'B', value: 'B', selected: true },
],
selectedValues: [{ text: 'B', value: 'B', selected: true }],
queryValue: searchQuery,
queryValue: 'A',
})
.whenActionIsDispatched(updateOptionsAndFilter(options))
.thenStateShouldEqual({
Expand All @@ -751,7 +772,7 @@ describe('optionsPickerReducer', () => {
{ text: 'A', value: 'A', selected: false },
],
selectedValues: [{ text: 'B', value: 'B', selected: true }],
queryValue: searchQuery,
queryValue: 'A',
highlightIndex: 0,
})
.whenActionIsDispatched(updateSearchQuery(''))
Expand Down
Expand Up @@ -242,7 +242,8 @@ const optionsPickerSlice = createSlice({
const searchQuery = trim((state.queryValue ?? '').toLowerCase());

state.options = action.payload.filter((option) => {
const text = Array.isArray(option.text) ? option.text.toString() : option.text;
const optionsText = option.text ?? '';
const text = Array.isArray(optionsText) ? optionsText.toString() : optionsText;
return text.toLowerCase().indexOf(searchQuery) !== -1;
});

Expand Down