diff --git a/src/criteria/index.test.js b/src/criteria/index.test.js index f4791cae..99da1cc9 100644 --- a/src/criteria/index.test.js +++ b/src/criteria/index.test.js @@ -104,6 +104,18 @@ describe( 'criteria matching', () => { expect( criteria.matches( { value: '' } ) ).toEqual( false ); expect( criteria.matches( { value: [] } ) ).toEqual( false ); } ); + it( 'should match "list__in" matching function with loose type comparison', () => { + setMatchingAttribute( criteriaId, () => 2 ); + setMatchingFunction( criteriaId, 'list__in' ); + const criteria = getCriteria( criteriaId ); + expect( criteria.matches( { value: [ '1', '2' ] } ) ).toEqual( true ); + expect( criteria.matches( { value: [ 1, 2 ] } ) ).toEqual( true ); + expect( criteria.matches( { value: '1, 2' } ) ).toEqual( true ); + expect( criteria.matches( { value: '2' } ) ).toEqual( true ); + expect( criteria.matches( { value: [ 1 ] } ) ).toEqual( false ); + expect( criteria.matches( { value: '1' } ) ).toEqual( false ); + expect( criteria.matches( { value: 1 } ) ).toEqual( false ); + } ); it( 'should match "list__not_in" matching function', () => { setMatchingAttribute( criteriaId, () => 'bar' ); setMatchingFunction( criteriaId, 'list__not_in' ); @@ -128,6 +140,17 @@ describe( 'criteria matching', () => { expect( criteria.matches( { value: '' } ) ).toEqual( true ); expect( criteria.matches( { value: [] } ) ).toEqual( true ); } ); + it( 'should match "list__not_in" matching function with loose type comparison', () => { + setMatchingAttribute( criteriaId, () => 2 ); + setMatchingFunction( criteriaId, 'list__not_in' ); + const criteria = getCriteria( criteriaId ); + expect( criteria.matches( { value: [ '1', '2' ] } ) ).toEqual( false ); + expect( criteria.matches( { value: [ 1, 2 ] } ) ).toEqual( false ); + expect( criteria.matches( { value: '2' } ) ).toEqual( false ); + expect( criteria.matches( { value: [ 1 ] } ) ).toEqual( true ); + expect( criteria.matches( { value: '1' } ) ).toEqual( true ); + expect( criteria.matches( { value: 1 } ) ).toEqual( true ); + } ); it( 'should match custom matching function', () => { setMatchingFunction( criteriaId, segmentConfig => { return segmentConfig.value === 'foo'; diff --git a/src/criteria/matching-functions.js b/src/criteria/matching-functions.js index b2b35ed9..aeeda1d2 100644 --- a/src/criteria/matching-functions.js +++ b/src/criteria/matching-functions.js @@ -1,3 +1,5 @@ +/* eslint-disable eqeqeq */ + /** * Common matching functions that can be used by criteria. */ @@ -22,9 +24,9 @@ export default { return false; } if ( Array.isArray( criteria.value ) ) { - return criteria.value.some( value => list.includes( value ) ); + return criteria.value.some( value => list.some( configValue => configValue == value ) ); } - if ( ! criteria.value || ! list.includes( criteria.value ) ) { + if ( ! criteria.value || ! list.some( configValue => configValue == criteria.value ) ) { return false; } return true; @@ -45,9 +47,9 @@ export default { return true; } if ( Array.isArray( criteria.value ) ) { - return ! criteria.value.some( value => list.includes( value ) ); + return ! criteria.value.some( value => list.some( configValue => configValue == value ) ); } - if ( ! criteria.value || ! list.includes( criteria.value ) ) { + if ( ! criteria.value || ! list.some( configValue => configValue == criteria.value ) ) { return true; } return false;