Skip to content

Commit

Permalink
🎨 Fix: path param 가져오는 함수 이름 변경 및 테스트 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ckhe1215 committed Mar 17, 2024
1 parent 4e14353 commit 13cee26
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/pages/Record.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Record extends Component {
}
mount() {
const pTag = document.querySelector(".date_info");
const { date } = router.getParams();
const { date } = router.getPathParams();
const today = new Date();
if (date) {
pTag.innerText = date;
Expand Down
10 changes: 5 additions & 5 deletions src/utils/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const route = (path) => {
if (route.params.length === 0) {
route.component();
} else {
const params = getParamsFromRoute(route, path);
const params = getPathParamsFromRoute(route, path);
route.component(params);
}
} else {
Expand All @@ -42,13 +42,13 @@ const navigate = (path) => {
route(path);
};

const getParams = () => {
const getPathParams = () => {
const pathname = window.location.pathname;
const route = routerRegistry.find((route) => route.testRegExp.test(pathname));
return getParamsFromRoute(route, pathname);
return getPathParamsFromRoute(route, pathname);
};

const getParamsFromRoute = (route, path) => {
export const getPathParamsFromRoute = (route, path) => {
const params = {};
if (route && route.params.length > 0) {
const matches = RegExp(route.testRegExp).exec(path);
Expand Down Expand Up @@ -81,5 +81,5 @@ const init = () => {
export default {
init,
navigate,
getParams,
getPathParams,
};
24 changes: 21 additions & 3 deletions test/utils/router.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { addRoute, routerRegistry } from "../../src/utils/router";
import {
addRoute,
getPathParamsFromRoute,
routerRegistry,
} from "../../src/utils/router";

describe("addRoute 테스트", () => {
test("path가 pathParam을 포함하지 않는 경우", () => {
addRoute("/test", () => {});
expect(RegExp(routerRegistry[0].testRegExp).exec("/test")).toBeTruthy();
expect(RegExp(routerRegistry[0].testRegExp).exec("/text")).toBeFalsy();
routerRegistry.pop();
});

test("path가 pathParam을 포함하는 경우", () => {
addRoute("/test/:data", () => {});
expect(
RegExp(routerRegistry[1].testRegExp).exec("/test/testData")
RegExp(routerRegistry[0].testRegExp).exec("/test/testData")
).toBeTruthy();
expect(RegExp(routerRegistry[1].testRegExp).exec("/test")).toBeFalsy();
expect(RegExp(routerRegistry[0].testRegExp).exec("/test")).toBeFalsy();
routerRegistry.pop();
});
});

describe("getPathParamsFromRoute 테스트", () => {
test("route에 저장된 정규표현식과 일치하는 데이터 리턴", () => {
addRoute("/test/:data", () => {});
const route = routerRegistry[0];
const param = getPathParamsFromRoute(route, "/test/123");
expect(param).toEqual({
data: "123",
});
routerRegistry.pop();
});
});

0 comments on commit 13cee26

Please sign in to comment.