Skip to content

Commit

Permalink
Implement IS ALL/IS NOT ALL operators plus improve all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Mar 20, 2024
1 parent 4dc6de5 commit 20ce6ed
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 5 deletions.
20 changes: 20 additions & 0 deletions packages/dataviews/src/filter-and-sort-data-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
OPERATOR_IS_NOT,
OPERATOR_IS_NONE,
OPERATOR_IS_ANY,
OPERATOR_IS_ALL,
OPERATOR_IS_NOT_ALL,
} from './constants';
import { normalizeFields } from './normalize-fields';

Expand Down Expand Up @@ -72,6 +74,24 @@ export function filterAndSortDataView( data, view, fields ) {
field.getValue( { item } )
);
} );
} else if (
filter.operator === OPERATOR_IS_ALL &&
filter?.value?.length > 0
) {
filteredData = filteredData.filter( ( item ) => {
return filter.value.every( ( value ) => {
return field.getValue( { item } ).includes( value );
} );
} );
} else if (
filter.operator === OPERATOR_IS_NOT_ALL &&
filter?.value?.length > 0
) {
filteredData = filteredData.filter( ( item ) => {
return filter.value.every( ( value ) => {
return ! field.getValue( { item } ).includes( value );
} );
} );
} else if ( filter.operator === OPERATOR_IS ) {
filteredData = filteredData.filter( ( item ) => {
return filter.value === field.getValue( { item } );
Expand Down
17 changes: 17 additions & 0 deletions packages/dataviews/src/stories/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,76 +21,87 @@ export const data = [
description: 'Apollo description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Not a planet',
categories: [ 'Space', 'NASA' ],
},
{
id: 2,
title: 'Space',
description: 'Space description',
image: 'https://live.staticflickr.com/5678/21911065441_92e2d44708_b.jpg',
type: 'Not a planet',
categories: [ 'Space' ],
},
{
id: 3,
title: 'NASA',
description: 'NASA photo',
image: 'https://live.staticflickr.com/742/21712365770_8f70a2c91e_b.jpg',
type: 'Not a planet',
categories: [ 'NASA' ],
},
{
id: 4,
title: 'Neptune',
description: 'Neptune description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Ice giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 5,
title: 'Mercury',
description: 'Mercury description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 6,
title: 'Venus',
description: 'Venus description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 7,
title: 'Earth',
description: 'Earth description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 8,
title: 'Mars',
description: 'Mars description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Terrestrial',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 9,
title: 'Jupiter',
description: 'Jupiter description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Gas giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 10,
title: 'Saturn',
description: 'Saturn description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Gas giant',
categories: [ 'Space', 'Planet', 'Solar system' ],
},
{
id: 11,
title: 'Uranus',
description: 'Uranus description',
image: 'https://live.staticflickr.com/5725/21726228300_51333bd62c_b.jpg',
type: 'Ice giant',
categories: [ 'Space', 'Ice giant', 'Solar system' ],
},
];

Expand Down Expand Up @@ -175,4 +186,10 @@ export const fields = [
enableSorting: false,
isSearchField: true,
},
{
header: 'Categories',
id: 'categories',
enableSorting: false,
type: 'enumeration',
},
];
100 changes: 95 additions & 5 deletions packages/dataviews/src/test/filter-and-sort-data-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ describe( 'filters', () => {
expect( result[ 0 ].description ).toBe( 'NASA photo' );
} );

it( 'should search using IS ANY filter', () => {
it( 'should search using IS filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'type',
operator: 'isAny',
value: [ 'Ice giant' ],
operator: 'is',
value: 'Ice giant',
},
],
},
Expand All @@ -72,24 +72,114 @@ describe( 'filters', () => {
expect( result[ 1 ].title ).toBe( 'Uranus' );
} );

it( 'should search using IS filter', () => {
it( 'should search using IS NOT filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'type',
operator: 'is',
operator: 'isNot',
value: 'Ice giant',
},
],
},
fields
);
expect( result ).toHaveLength( 9 );
expect( result[ 0 ].title ).toBe( 'Apollo' );
expect( result[ 1 ].title ).toBe( 'Space' );
expect( result[ 2 ].title ).toBe( 'NASA' );
expect( result[ 3 ].title ).toBe( 'Mercury' );
expect( result[ 4 ].title ).toBe( 'Venus' );
expect( result[ 5 ].title ).toBe( 'Earth' );
expect( result[ 6 ].title ).toBe( 'Mars' );
expect( result[ 7 ].title ).toBe( 'Jupiter' );
expect( result[ 8 ].title ).toBe( 'Saturn' );
} );

it( 'should search using IS ANY filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'type',
operator: 'isAny',
value: [ 'Ice giant' ],
},
],
},
fields
);
expect( result ).toHaveLength( 2 );
expect( result[ 0 ].title ).toBe( 'Neptune' );
expect( result[ 1 ].title ).toBe( 'Uranus' );
} );

it( 'should search using IS NONE filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'type',
operator: 'isNone',
value: [ 'Ice giant', 'Gas giant', 'Terrestrial' ],
},
],
},
fields
);
expect( result ).toHaveLength( 3 );
expect( result[ 0 ].title ).toBe( 'Apollo' );
expect( result[ 1 ].title ).toBe( 'Space' );
expect( result[ 2 ].title ).toBe( 'NASA' );
} );

it( 'should search using IS ALL filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'categories',
operator: 'isAll',
value: [ 'Planet', 'Solar system' ],
},
],
},
fields
);
expect( result ).toHaveLength( 7 );
expect( result[ 0 ].title ).toBe( 'Neptune' );
expect( result[ 1 ].title ).toBe( 'Mercury' );
expect( result[ 2 ].title ).toBe( 'Venus' );
expect( result[ 3 ].title ).toBe( 'Earth' );
expect( result[ 4 ].title ).toBe( 'Mars' );
expect( result[ 5 ].title ).toBe( 'Jupiter' );
expect( result[ 6 ].title ).toBe( 'Saturn' );
} );

it( 'should search using IS NOT ALL filter', () => {
const { data: result } = filterAndSortDataView(
data,
{
filters: [
{
field: 'categories',
operator: 'isNotAll',
value: [ 'Planet', 'Solar system' ],
},
],
},
fields
);
expect( result ).toHaveLength( 3 );
expect( result[ 0 ].title ).toBe( 'Apollo' );
expect( result[ 1 ].title ).toBe( 'Space' );
expect( result[ 2 ].title ).toBe( 'NASA' );
} );
} );

describe( 'sorting', () => {
Expand Down

0 comments on commit 20ce6ed

Please sign in to comment.