Skip to content

Commit

Permalink
Fix errors in find and filter with an empty store (#6265)
Browse files Browse the repository at this point in the history
* Fix errors in find and filter with an empty store (#6262)

* Fix errors in find and filter with an empty store (#6262)

* fix prettier issues

* add changeset
  • Loading branch information
snstanton authored Jul 23, 2024
1 parent be0e2cc commit ca61aa0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-birds-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/mock': patch
---

fix errors in find and filter with an empty store
4 changes: 2 additions & 2 deletions packages/mock/src/MockStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ export class MockStore implements IMockStore {

filter(key: string, predicate: (val: Entity) => boolean) {
const entity = this.store[key];
return Object.values(entity).filter(predicate);
return entity ? Object.values(entity).filter(predicate) : [];
}

find(key: string, predicate: (val: Entity) => boolean) {
const entity = this.store[key];
return Object.values(entity).find(predicate);
return entity ? Object.values(entity).find(predicate) : undefined;
}

private getImpl<KeyT extends KeyTypeConstraints>(args: GetArgs<KeyT>) {
Expand Down
44 changes: 43 additions & 1 deletion packages/mock/tests/store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { buildSchema } from 'graphql';
import { createMockStore } from '../src/index.js';
import { createMockStore, MockStore } from '../src/index.js';
import { assertIsRef, Ref } from '../src/types.js';
import { makeRef } from '../src/utils.js';

Expand Down Expand Up @@ -626,4 +626,46 @@ describe('MockStore', () => {
expect(store.get('User', '123', 'name', { format: 'fullName' })).toEqual('Alexandre the Great');
expect(store.get('User', '123', 'name', { format: 'shortName' })).toEqual('Alex');
});

describe('filter method', () => {
it('should be able to filter an empty list', () => {
const store = new MockStore({ schema });
store.set('User', 'user1', { name: 'Ross' });
expect(store.filter('User', () => false)).toEqual([]);
store.reset();
expect(store.filter('User', () => true)).toEqual([]);
});

it('should apply the filter', () => {
const store = new MockStore({ schema });
store.set('User', 'user1', { name: 'Ross' });
store.set('User', 'user2', { name: 'Nico' });
expect(store.filter('User', () => true)).toEqual([{ name: 'Ross' }, { name: 'Nico' }]);
expect(store.filter('User', ({ name }) => (name as string).startsWith('R'))).toEqual([
{ name: 'Ross' },
]);
});
});

describe('find method', () => {
it('should return undefined for an empty store', () => {
const store = new MockStore({ schema });
expect(store.find('User', () => true)).toBeUndefined();
});

it('should return undefined for a miss', () => {
const store = new MockStore({ schema });
store.set('User', 'user1', { name: 'Ross' });
expect(store.find('User', () => false)).toBeUndefined();
});

it('should return a match', () => {
const store = new MockStore({ schema });
store.set('User', 'user1', { name: 'Ross' });
store.set('User', 'user2', { name: 'Nico' });
expect(store.find('User', ({ name }) => (name as string).startsWith('N'))).toEqual({
name: 'Nico',
});
});
});
});

0 comments on commit ca61aa0

Please sign in to comment.