From 10080fefb1c61e800ce84b94543fbb5efead382c Mon Sep 17 00:00:00 2001 From: Fernando Rojo Date: Wed, 26 Apr 2023 21:40:33 -0400 Subject: [PATCH] close #379 --- src/image/helpers.test.ts | 2 +- src/image/image.types.test.ts | 32 ------------------------------ src/router/parse-next-path.test.ts | 25 +++++++++++++++++++++++ src/router/parse-next-path.ts | 13 ++++++++++-- tsconfig.json | 2 +- 5 files changed, 38 insertions(+), 36 deletions(-) delete mode 100644 src/image/image.types.test.ts create mode 100644 src/router/parse-next-path.test.ts diff --git a/src/image/helpers.test.ts b/src/image/helpers.test.ts index d5c2c4ac..05d6ff85 100644 --- a/src/image/helpers.test.ts +++ b/src/image/helpers.test.ts @@ -112,6 +112,6 @@ describe('resolveSourceFromImgAttributes', () => { sizes: `(max-width: 600px) 480px, 800px`, src: 'elva-fairy.jpg', }).uri - ).toThrow() + ).not.toThrow() }) }) diff --git a/src/image/image.types.test.ts b/src/image/image.types.test.ts deleted file mode 100644 index 771b30bd..00000000 --- a/src/image/image.types.test.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type NextImage from 'next/image' - -import { SolitoImageProps } from './image.types' - -type NextImageProps = React.ComponentProps - -// eslint-disable-next-line @typescript-eslint/no-unused-vars -const test: Array = [ - { - src: '', - height: 100, - width: 100, - alt: '', - }, - { - unoptimized: true, - src: '', - height: 100, - width: 100, - alt: '', - }, - { - src: null as any as Exclude, - height: 100, - width: 100, - alt: '', - }, - { - src: 1, - alt: '', - }, -] diff --git a/src/router/parse-next-path.test.ts b/src/router/parse-next-path.test.ts new file mode 100644 index 00000000..3e5db010 --- /dev/null +++ b/src/router/parse-next-path.test.ts @@ -0,0 +1,25 @@ +import { parseNextPath } from './parse-next-path' + +describe('parseNextPath', () => { + it('should handle arrays', () => { + expect( + parseNextPath({ + pathname: '/', + query: { + ids: [1, 2], + }, + }) + ).toEqual('/?ids=1&ids=2') + }) + + it('supports dynamic routes', () => { + expect( + parseNextPath({ + pathname: '/[id]', + query: { + id: 1, + }, + }) + ).toEqual('/1') + }) +}) diff --git a/src/router/parse-next-path.ts b/src/router/parse-next-path.ts index f8f80a87..7de5c632 100644 --- a/src/router/parse-next-path.ts +++ b/src/router/parse-next-path.ts @@ -9,6 +9,8 @@ const parseNextPath = (from: Parameters[0]) => { // but I can't see why you would use this with RN + Next.js if (typeof from == 'object' && from.query && typeof from.query == 'object') { const query = { ...from.query } + // replace dynamic routes + // and [...param] syntax for (const key in query) { if (path.includes(`[${key}]`)) { path = path.replace(`[${key}]`, `${query[key] ?? ''}`) @@ -21,11 +23,18 @@ const parseNextPath = (from: Parameters[0]) => { } } } + if (Object.keys(query).length) { + // add query param separator path += '?' for (const key in query) { - if (query[key] != null) { - path += `${key}=${query[key]}&` + const value = query[key] + if (Array.isArray(value)) { + value.forEach((item) => { + path += `${key}=${item}&` + }) + } else if (value != null) { + path += `${key}=${value}&` } } if (path.endsWith('&') || path.endsWith('?')) { diff --git a/tsconfig.json b/tsconfig.json index 9d15c230..7da450fe 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -12,7 +12,7 @@ "jsx": "react-jsx" }, "include": ["./src"], - "exclude": ["**/__mocks__/*", "**/__tests__/*"], + "exclude": ["**/__mocks__/*", "**/__tests__/*", "**.*.test.ts"], "ts-node": { // these options are overrides used only by ts-node "compilerOptions": {