From 56c522844a829a44bf569ef0d68c12154486eb38 Mon Sep 17 00:00:00 2001 From: Kotaro Yabe Date: Fri, 10 Jun 2022 17:45:39 +0900 Subject: [PATCH] fix(query): surround and only cannot be used at the same time (#1238) --- src/runtime/query/match/utils.ts | 4 ++-- test/features/query/query.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/runtime/query/match/utils.ts b/src/runtime/query/match/utils.ts index 6a0d12724..577b14baf 100644 --- a/src/runtime/query/match/utils.ts +++ b/src/runtime/query/match/utils.ts @@ -40,7 +40,7 @@ export const detectProperties = (keys: string[]) => { } export const withoutKeys = (keys: string[] = []) => (obj: any) => { - if (keys.length === 0) { + if (keys.length === 0 || !obj) { return obj } const { prefixes, properties } = detectProperties(keys) @@ -48,7 +48,7 @@ export const withoutKeys = (keys: string[] = []) => (obj: any) => { } export const withKeys = (keys: string[] = []) => (obj: any) => { - if (keys.length === 0) { + if (keys.length === 0 || !obj) { return obj } const { prefixes, properties } = detectProperties(keys) diff --git a/test/features/query/query.test.ts b/test/features/query/query.test.ts index 02476a902..6247c2338 100644 --- a/test/features/query/query.test.ts +++ b/test/features/query/query.test.ts @@ -205,6 +205,30 @@ describe('Database Provider', () => { assert(result[2] === null) }) + test('Surround and using only method', async () => { + const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[])) + const result = await createQuery(fetcher) + .only(['_path']) + .findSurround({ id: 3 }, { before: 2, after: 1 }) + + assert((result as Array).length === 3) + assert(result[0]._path === '/a') + assert(result[1]._path === '/b') + assert(result[2] === null) + }) + + test('Surround and using without method', async () => { + const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, _path: '/a' }, { id: 2, _path: '/b' }, { id: 3, _path: '/c' }] as any[])) + const result = await createQuery(fetcher) + .without('id') + .findSurround({ id: 3 }, { before: 2, after: 1 }) + + assert((result as Array).length === 3) + assert(result[0]._path === '/a') + assert(result[1]._path === '/b') + assert(result[2] === null) + }) + test('Chain multiple where conditions', async () => { const fetcher = createPipelineFetcher(() => Promise.resolve([{ id: 1, path: '/a' }, { id: 2, path: '/b' }, { id: 3, path: '/c' }] as any[])) const query = createQuery(fetcher).where({ id: { $in: [1, 2] } })