diff --git a/.all-contributorsrc b/.all-contributorsrc index b6c6ba53..aa9d9ec4 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1068,6 +1068,15 @@ "contributions": [ "doc" ] + }, + { + "login": "jbutz", + "name": "Jason Butz", + "avatar_url": "https://avatars2.githubusercontent.com/u/736696?v=4", + "profile": "http://www.jasonbutz.info", + "contributions": [ + "code" + ] } ], "repoType": "github" diff --git a/README.md b/README.md index 039be3c8..5439f565 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Netlify CMS -[![All Contributors](https://img.shields.io/badge/all_contributors-121-orange.svg)](#contributors) +[![All Contributors](https://img.shields.io/badge/all_contributors-122-orange.svg)](#contributors) [![Open Source Helpers](https://www.codetriage.com/netlify/netlify-cms/badges/users.svg)](https://www.codetriage.com/netlify/netlify-cms) [![](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/netlify/netlifycms) diff --git a/packages/netlify-cms-core/src/__tests__/backend.spec.js b/packages/netlify-cms-core/src/__tests__/backend.spec.js new file mode 100644 index 00000000..6a362a60 --- /dev/null +++ b/packages/netlify-cms-core/src/__tests__/backend.spec.js @@ -0,0 +1,111 @@ +import { resolveBackend } from '../backend'; +import registry from 'Lib/registry'; + +jest.mock('Lib/registry'); + +const configWrapper = inputObject => ({ + get: prop => inputObject[prop], +}); + +describe('Backend', () => { + describe('filterEntries', () => { + let backend; + + beforeEach(() => { + registry.getBackend.mockReturnValue({ + init: jest.fn(), + }); + backend = resolveBackend({ + getIn: jest.fn().mockReturnValue('git-gateway'), + }); + }); + + it('filters string values', () => { + const result = backend.filterEntries( + { + entries: [ + { + data: { + testField: 'testValue', + }, + }, + { + data: { + testField: 'testValue2', + }, + }, + ], + }, + configWrapper({ field: 'testField', value: 'testValue' }), + ); + + expect(result.length).toBe(1); + }); + + it('filters number values', () => { + const result = backend.filterEntries( + { + entries: [ + { + data: { + testField: 42, + }, + }, + { + data: { + testField: 5, + }, + }, + ], + }, + configWrapper({ field: 'testField', value: 42 }), + ); + + expect(result.length).toBe(1); + }); + + it('filters boolean values', () => { + const result = backend.filterEntries( + { + entries: [ + { + data: { + testField: false, + }, + }, + { + data: { + testField: true, + }, + }, + ], + }, + configWrapper({ field: 'testField', value: false }), + ); + + expect(result.length).toBe(1); + }); + + it('filters list values', () => { + const result = backend.filterEntries( + { + entries: [ + { + data: { + testField: ['valueOne', 'valueTwo', 'testValue'], + }, + }, + { + data: { + testField: ['valueThree'], + }, + }, + ], + }, + configWrapper({ field: 'testField', value: 'testValue' }), + ); + + expect(result.length).toBe(1); + }); + }); +}); diff --git a/packages/netlify-cms-core/src/backend.js b/packages/netlify-cms-core/src/backend.js index 7e2a700e..43a9f444 100644 --- a/packages/netlify-cms-core/src/backend.js +++ b/packages/netlify-cms-core/src/backend.js @@ -495,9 +495,13 @@ class Backend { } filterEntries(collection, filterRule) { - return collection.entries.filter( - entry => entry.data[filterRule.get('field')] === filterRule.get('value'), - ); + return collection.entries.filter(entry => { + const fieldValue = entry.data[filterRule.get('field')]; + if (Array.isArray(fieldValue)) { + return fieldValue.includes(filterRule.get('value')); + } + return fieldValue === filterRule.get('value'); + }); } }