Skip to content

Commit

Permalink
Fix getMany fails when called with an empty array of ids (#165)
Browse files Browse the repository at this point in the history
Closes #158
  • Loading branch information
fzaninotto authored Sep 4, 2024
1 parent 7c896f5 commit 812f2c4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,9 @@ export default (config: IDataProviderConfig): DataProvider => ({

getMany: (resource, params: Partial<GetManyParams> = {}) => {
const ids = params.ids;
if (ids.length === 0) {
return Promise.resolve({ data: [] });
}
const primaryKey = getPrimaryKey(resource, config.primaryKeys);

const query = getQuery(primaryKey, ids, resource, params.meta);
Expand Down
25 changes: 25 additions & 0 deletions tests/dataProvider/getMany.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { makeTestFromCase, Case } from './helper';
import dataProviderBuilder from '../../src';

describe('getMany specific', () => {
const method = 'getMany';

const cases: Case[] = [
{
test: 'Read a list of resource, by ids',
method,
resource: 'posts',
params: { ids: [1, 2, 3] },
expectedUrl: `/posts?id=in.%28${encodeURIComponent('1,2,3')}%29`,
expectedOptions: { headers: {} },
},
];

cases.forEach(makeTestFromCase);

it('should not fail when no ids are provided', async () => {
const dataProvider = dataProviderBuilder({} as any);
const { data } = await dataProvider.getMany('posts', { ids: [] });
expect(data).toEqual([]);
});
});

0 comments on commit 812f2c4

Please sign in to comment.