From 2cc69588173c1445c1c7417c5a88dcd12558cbe8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=84=EB=B9=9B=EB=82=98?= Date: Tue, 9 Jan 2024 20:37:48 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=9D=BC=EC=9A=B0=ED=84=B0=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/__test__/index.test.js | 24 +++++++++++++----------- src/router/index.js | 2 +- src/router/utils.js | 6 ++++-- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/router/__test__/index.test.js b/src/router/__test__/index.test.js index 27a163d..f751b11 100644 --- a/src/router/__test__/index.test.js +++ b/src/router/__test__/index.test.js @@ -17,29 +17,31 @@ const routes = [ describe('router test', () => { test('should verify that the pathToRegex function works properly', () => { - expect(pathToRegex('/')).toEqual(/^\/$/); - expect(pathToRegex('/posts/:id')).toEqual(/^\/posts\/(.+)$/); - expect(pathToRegex('/posts')).toEqual(/^\/posts$/); + const regex = pathToRegex('/'); + const regex2 = pathToRegex('/posts/:id'); - expect(pathToRegex('/posts/:id')).not.toEqual(/^\/posts$/); - expect(pathToRegex('/posts')).not.toEqual(/^\/posts\/(.+)$/); + expect(regex.test('/')).toBeTruthy(); + expect(regex.test('/posts')).toBeFalsy(); + + expect(regex2.test('/posts/3')).toBeTruthy(); + expect(regex2.test('/posts')).toBeFalsy(); }); test('should return correct params when the getParams function works properly', () => { const pathname = '/posts/1'; - const targetRoute = findRoute(routes, pathname); - expect(getParams(targetRoute)).toEqual({ id: '1' }); expect(getParams(targetRoute).id).not.toBe(2); expect(getParams(targetRoute)).not.toEqual({ no: '1' }); }); - test('should return correct queryParams when the findRoute function works properly', () => { - const search = '?a=1&b=2'; + test('should return correct queryParams when the getQueryParams function works properly', () => { + const url = 'https://www.google.com?a=1&b=2'; + const url2 = 'https://www.google.com?a=1b=2'; - expect(getQueryParams(search)).toEqual({ a: '1', b: '2' }); - expect(getQueryParams(search)).not.toEqual({ a: '1', b: '3' }); + expect(getQueryParams(url)).toEqual({ a: '1', b: '2' }); + expect(getQueryParams(url)).not.toEqual({ a: '1', b: '3' }); + expect(getQueryParams(url2)).not.toEqual({ a: '1', b: '2' }); }); test('should verify that the findRoute function works properly', () => { diff --git a/src/router/index.js b/src/router/index.js index 3250f84..50a0dcd 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -19,7 +19,7 @@ const renderRoute = () => { if (!targetRoute) return; const params = getParams(targetRoute); - const queryParams = getQueryParams(location.search); + const queryParams = getQueryParams(location); document.querySelector('#app').innerHTML = targetRoute.route.page({ params, diff --git a/src/router/utils.js b/src/router/utils.js index d3e87c0..5396625 100644 --- a/src/router/utils.js +++ b/src/router/utils.js @@ -11,8 +11,10 @@ export const getParams = (target) => { return Object.fromEntries(keys.map((key, index) => [key, values[index]])); }; -export const getQueryParams = (search) => { - const urlSearchParams = new URLSearchParams(search); +export const getQueryParams = (location) => { + const url = new URL(location); + + const urlSearchParams = new URLSearchParams(url.search); return Object.fromEntries(urlSearchParams.entries()); };