improvement(list-widget): support filtering entries in list widget (#1578)
This commit is contained in:
parent
deaac3d78b
commit
cdcd51157f
@ -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"
|
||||
|
@ -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)
|
||||
|
||||
|
111
packages/netlify-cms-core/src/__tests__/backend.spec.js
Normal file
111
packages/netlify-cms-core/src/__tests__/backend.spec.js
Normal file
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
@ -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');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user