Skip to content

Commit

Permalink
Fix errors in find and filter with an empty store (ardatan#6262)
Browse files Browse the repository at this point in the history
  • Loading branch information
snstanton committed Jun 13, 2024
1 parent 013f970 commit 3f421e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
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
46 changes: 42 additions & 4 deletions packages/mock/tests/store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { buildSchema } from 'graphql';
import { createMockStore } from '../src/index.js';
import { assertIsRef, Ref } from '../src/types.js';
import { makeRef } from '../src/utils.js';
import {buildSchema} from 'graphql';
import {createMockStore, MockStore} from '../src/index.js';
import {assertIsRef, Ref} from '../src/types.js';
import {makeRef} from '../src/utils.js';

const typeDefs = /* GraphQL */ `
type User {
Expand Down Expand Up @@ -626,4 +626,42 @@ 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 3f421e4

Please sign in to comment.