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

test: FilterSets-utils #14028

Merged
merged 5 commits into from
Apr 9, 2021
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,144 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { FilterSet } from 'src/dashboard/reducers/types';
import { findExistingFilterSet } from '.';

const createDataMaskSelected = () => ({
filterId: { currentState: { value: 'value-1' } },
filterId2: { currentState: { value: 'value-2' } },
});

test('Should find correct filter', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues = [
{
id: 'id-01',
name: 'name-01',
nativeFilters: {},
dataMask: {
nativeFilters: {
filterId: { currentState: { value: 'value-1' } },
filterId2: { currentState: { value: 'value-2' } },
},
} as any,
},
];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toEqual({
dataMask: {
nativeFilters: {
filterId: { currentState: { value: 'value-1' } },
filterId2: { currentState: { value: 'value-2' } },
},
},
id: 'id-01',
name: 'name-01',
nativeFilters: {},
});
});

test('Should return undefined when nativeFilters has less values', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues = [
{
id: 'id-01',
name: 'name-01',
nativeFilters: {},
dataMask: {
nativeFilters: {
filterId: { currentState: { value: 'value-1' } },
},
} as any,
},
];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toBeUndefined();
});

test('Should return undefined when nativeFilters has different values', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues = [
{
id: 'id-01',
name: 'name-01',
nativeFilters: {},
dataMask: {
nativeFilters: {
filterId: { currentState: { value: 'value-1' } },
filterId2: { currentState: { value: 'value-1' } },
},
} as any,
},
];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toBeUndefined();
});

test('Should return undefined when dataMask:{}', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues = [
{
id: 'id-01',
name: 'name-01',
nativeFilters: {},
dataMask: {},
},
];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toBeUndefined();
});

test('Should return undefined when dataMask.nativeFilters is undefined}', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues = [
{
id: 'id-01',
name: 'name-01',
nativeFilters: {},
dataMask: { nativeFilters: undefined },
},
];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toBeUndefined();
});

test('Should return undefined when filterSetFilterValues is []', () => {
const dataMaskSelected = createDataMaskSelected();
const filterSetFilterValues: FilterSet[] = [];
const response = findExistingFilterSet({
filterSetFilterValues,
dataMaskSelected,
});
expect(response).toBeUndefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { generateFiltersSetId } from '.';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also check for expected ID length?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if the generated string is the same size. But I believe that this is not so important


test('Should follow the pattern "FILTERS_SET-"', () => {
const id = generateFiltersSetId();
expect(id.startsWith('FILTERS_SET-', 0)).toBe(true);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { getFilterValueForDisplay } from '.';

test('Should return "" when value is null or undefined', () => {
expect(getFilterValueForDisplay(null)).toBe('');
expect(getFilterValueForDisplay(undefined)).toBe('');
expect(getFilterValueForDisplay()).toBe('');
});

test('Should return "string value" when value is string or number', () => {
expect(getFilterValueForDisplay(123)).toBe('123');
expect(getFilterValueForDisplay('123')).toBe('123');
});

test('Should return a string with values ​​separated by commas', () => {
expect(getFilterValueForDisplay(['a', 'b', 'c'])).toBe('a, b, c');
});

test('Should return a JSON.stringify from objects', () => {
expect(getFilterValueForDisplay({ any: 'value' })).toBe('{"any":"value"}');
});

test('Should return an error message when the type is invalid', () => {
expect(getFilterValueForDisplay(true as any)).toBe('Unknown value');
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ export const findExistingFilterSet = ({
if (dataMaskFromFilterSet?.nativeFilters) {
const dataMaskSelectedEntries = Object.entries(dataMaskSelected);
return dataMaskSelectedEntries.every(
([id, filterFromSelectedFilters]) =>
areObjectsEqual(
([id, filterFromSelectedFilters]) => {
const isEqual = areObjectsEqual(
filterFromSelectedFilters.currentState,
dataMaskFromFilterSet?.nativeFilters?.[id]?.currentState,
) &&
dataMaskSelectedEntries.length ===
Object.keys(dataMaskFromFilterSet?.nativeFilters ?? {}).length,
);
const hasSamePropsNumber =
dataMaskSelectedEntries.length ===
Object.keys(dataMaskFromFilterSet?.nativeFilters ?? {}).length;
return isEqual && hasSamePropsNumber;
},
);
}
return false;
Expand Down