describe('Testing filterArray()', () => { it('should filter an array of objects by custom predicates', () => { const products = [ { name: 'A', color: 'Blue', size: 50, locations: ['USA', 'Europe'], details: { length: 20, width: 70 } }, { name: 'B', color: 'Blue', size: 60, locations: [], details: { length: 20, width: 70 } }, { name: 'C', color: 'Black', size: 70, locations: ['Japan'], details: { length: 20, width: 71 } }, { name: 'D', color: 'Green', size: 50, locations: ['USA'], details: { length: 20, width: 71 } }, ]; const filters = { size: size => size === 50 || size === 70, color: color => ['blue', 'black'].includes(color.toLowerCase()), locations: locations => locations.find(x => ['JAPAN', 'USA'].includes(x.toUpperCase())), details: details => details.length < 30 && details.width >= 70, }; const filters = { size: (size) => size === 50 || size === 70, color: (color) => ['blue', 'black'].includes(color.toLowerCase()), details: (details) => details.length < 30 && details.width >= 70, locations: (locations) => { if (locations.includes('USA')) return true; // case sensitive if (locations.includes('Japan')) return true; // case sensitive const url = window.location.pathname.toLowerCase(); if (url.includes('/en-us/')) return true; // not case sensitive if (url.includes('/es/')) return true; // not case sensitive return false; } }; const filtered = filterArray(products, filters); const expected = [ { name: 'A', color: 'Blue', size: 50, locations: ['USA', 'Europe'], details: { length: 20, width: 70 } }, { name: 'C', color: 'Black', size: 70, locations: ['Japan'], details: { length: 20, width: 71 } }, ]; expect(filtered).toStrictEqual(expected); }); });