-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refcator: builder pattern을 이용한 `PathBuilder` 구현 path를 일관되게 얻을 수 있는 클래스 구현 * feat: PathBuilder 테스트 코드 추가
- Loading branch information
Showing
9 changed files
with
159 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
apps/realworld/src/entities/comment/ui/induce-sign-in/induce-sign-in.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { PathBuilder } from './routes'; | ||
|
||
const context = describe; | ||
|
||
describe('PathBuilder에서', () => { | ||
context('home path를 빌드할 때', () => { | ||
it('/ 경로가 생성된다.', () => { | ||
const homePath = PathBuilder.buildHome().getPath(); | ||
|
||
expect(homePath).toEqual('/'); | ||
}); | ||
|
||
it('addPage로 경로가 생성된다.', () => { | ||
const page = 2; | ||
const homePathWithPageParam = PathBuilder.buildHome().addPage(page).getPath(); | ||
|
||
expect(homePathWithPageParam).toEqual(`/?page=${page}`); | ||
}); | ||
}); | ||
|
||
context('register path를 빌드할 때 ', () => { | ||
it('/register 경로가 생성된다.', () => { | ||
const buildedPath = PathBuilder.buildRegister().getPath(); | ||
|
||
expect(buildedPath).toEqual('/register'); | ||
}); | ||
}); | ||
|
||
context('login path를 빌드할 때 ', () => { | ||
it('/login 경로가 생성된다.', () => { | ||
const buildedPath = PathBuilder.buildLogin().getPath(); | ||
|
||
expect(buildedPath).toEqual('/login'); | ||
}); | ||
}); | ||
|
||
context('article path를 빌드할 때', () => { | ||
it('addSlug로 경로가 생성된다.', () => { | ||
const slug = 'this is slug'; | ||
const path = PathBuilder.buildArticle().addSlug(slug).getPath(); | ||
|
||
expect(path).toEqual(`/article/${slug}`); | ||
}); | ||
|
||
it('addSlug가 호출되지 않으면 에러가 발생한다.', () => { | ||
try { | ||
PathBuilder.buildArticle().getPath(); | ||
expect(true).toBe(false); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
} | ||
}); | ||
|
||
it('addSlug가 호출되지 않으면 에러가 발생한다.', () => { | ||
try { | ||
PathBuilder.buildArticle().addSlug('1').addSlug('2').getPath(); | ||
expect(true).toBe(false); | ||
} catch (e) { | ||
expect(e).toBeInstanceOf(Error); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
export class PathBuilder { | ||
private path: string; | ||
|
||
private constructor(path: string) { | ||
this.path = path; | ||
} | ||
|
||
static buildHome(): HomePathBuilder { | ||
return new HomePathBuilder('/'); | ||
} | ||
|
||
static buildRegister(): PathBuilder { | ||
return new PathBuilder('/register'); | ||
} | ||
|
||
static buildLogin(): PathBuilder { | ||
return new PathBuilder('/login'); | ||
} | ||
|
||
static buildArticle(): ArticlePathBuilder { | ||
return new ArticlePathBuilder('/article'); | ||
} | ||
getPath(): string { | ||
return this.path; | ||
} | ||
} | ||
|
||
class HomePathBuilder { | ||
private path: string; | ||
private params: string; | ||
constructor(path: '/') { | ||
this.path = path; | ||
this.params = '?'; | ||
} | ||
|
||
private getParamsString() { | ||
if (this.params === '?') { | ||
return ''; | ||
} | ||
return this.params; | ||
} | ||
|
||
addPage(page: number) { | ||
this.params += `page=${page}`; | ||
return this; | ||
} | ||
|
||
getPath() { | ||
return this.path + this.getParamsString(); | ||
} | ||
} | ||
|
||
class ArticlePathBuilder { | ||
private path: string; | ||
private count: number; | ||
|
||
constructor(path: '/article') { | ||
this.path = path; | ||
this.count = 0; | ||
} | ||
|
||
addSlug(slug: string): ArticlePathBuilder { | ||
this.path += `/${slug}`; | ||
this.count += 1; | ||
return this; | ||
} | ||
|
||
getPath(): string { | ||
if (this.count !== 1) { | ||
throw new Error('article path must have slug or only once'); | ||
} | ||
return this.path; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters