@@ -12,6 +12,7 @@ import { Role } from '../role/role.entity';
1212import { User } from '../user/user.entity' ;
1313import { TokenService } from '../auth/token/token.service' ;
1414import { ForbiddenAccessException } from '../exception/access.exception' ;
15+ import { Snowflake } from '@theinternetfolks/snowflake' ;
1516
1617describe ( 'WorkspaceService' , ( ) => {
1718 let service : WorkspaceService ;
@@ -72,64 +73,68 @@ describe('WorkspaceService', () => {
7273 } ) ;
7374
7475 describe ( 'createWorkspace' , ( ) => {
75- it ( '워크스페이스를 성공적으로 생성한다.' , async ( ) => {
76+ it ( '성공적으로 워크스페이스를 생성한다.' , async ( ) => {
77+ // Mock 데이터
7678 const userId = 1 ;
7779 const dto : CreateWorkspaceDto = {
7880 title : 'New Workspace' ,
79- description : 'A test workspace' ,
81+ description : 'Description of workspace' ,
8082 visibility : 'private' ,
81- thumbnailUrl : 'http://example.com/thumbnail .png' ,
83+ thumbnailUrl : 'http://example.com/image .png' ,
8284 } ;
8385
84- const owner = { id : userId } as User ;
85- const newDate = new Date ( ) ;
86- const newWorkspace = {
87- id : 1 ,
88- snowflakeId : 'snowflake-id' ,
89- owner,
90- title : dto . title ,
91- description : dto . description ,
92- visibility : dto . visibility ,
93- createdAt : newDate ,
94- updatedAt : newDate ,
95- thumbnailUrl : dto . thumbnailUrl ,
96- edges : [ ] ,
97- pages : [ ] ,
98- nodes : [ ] ,
86+ const mockUser = { id : userId } as User ;
87+ const generatedSnowflakeId = Snowflake . generate ( ) ;
88+ const mockWorkspace = {
89+ id : 10 ,
90+ ...dto ,
91+ snowflakeId : generatedSnowflakeId ,
9992 } as Workspace ;
10093
101- jest . spyOn ( userRepository , 'findOneBy' ) . mockResolvedValue ( owner ) ;
102- jest . spyOn ( workspaceRepository , 'save' ) . mockResolvedValue ( newWorkspace ) ;
94+ // Mocking
95+ jest . spyOn ( userRepository , 'findOneBy' ) . mockResolvedValue ( mockUser ) ;
96+ jest . spyOn ( Snowflake , 'generate' ) . mockReturnValue ( generatedSnowflakeId ) ;
97+ jest . spyOn ( workspaceRepository , 'save' ) . mockResolvedValue ( mockWorkspace ) ;
98+ jest . spyOn ( roleRepository , 'save' ) . mockResolvedValue ( undefined ) ;
10399
100+ // 실행
104101 const result = await service . createWorkspace ( userId , dto ) ;
105102
106- expect ( result ) . toEqual ( newWorkspace ) ;
103+ // 검증
107104 expect ( userRepository . findOneBy ) . toHaveBeenCalledWith ( { id : userId } ) ;
108105 expect ( workspaceRepository . save ) . toHaveBeenCalledWith ( {
109- owner,
110- ...dto ,
111- visibility : 'private' ,
106+ snowflakeId : generatedSnowflakeId ,
107+ owner : mockUser ,
108+ title : dto . title ,
109+ description : dto . description ,
110+ visibility : 'private' , // 기본값 확인
111+ thumbnailUrl : dto . thumbnailUrl ,
112112 } ) ;
113113 expect ( roleRepository . save ) . toHaveBeenCalledWith ( {
114- userId : owner . id ,
115- workspaceId : newWorkspace . id ,
114+ userId : mockUser . id ,
115+ workspaceId : mockWorkspace . id ,
116116 role : 'owner' ,
117117 } ) ;
118+ expect ( result ) . toEqual ( mockWorkspace ) ;
118119 } ) ;
119120
120- it ( '사용자가 존재하지 않으면 UserNotFoundException을 throw한다.' , async ( ) => {
121+ it ( '사용자를 찾을 수 없으면 UserNotFoundException을 던진다.' , async ( ) => {
122+ // Mocking
121123 jest . spyOn ( userRepository , 'findOneBy' ) . mockResolvedValue ( null ) ;
122124
123- const dto : CreateWorkspaceDto = {
124- title : 'New Workspace' ,
125- description : 'A test workspace' ,
126- visibility : 'private' ,
127- thumbnailUrl : 'http://example.com/thumbnail.png' ,
128- } ;
129-
130- await expect ( service . createWorkspace ( 1 , dto ) ) . rejects . toThrow (
131- UserNotFoundException ,
132- ) ;
125+ // 실행 및 검증
126+ await expect (
127+ service . createWorkspace ( 1 , {
128+ title : 'New Workspace' ,
129+ description : 'Description of workspace' ,
130+ visibility : 'private' ,
131+ thumbnailUrl : 'http://example.com/image.png' ,
132+ } ) ,
133+ ) . rejects . toThrow ( UserNotFoundException ) ;
134+
135+ expect ( userRepository . findOneBy ) . toHaveBeenCalledWith ( { id : 1 } ) ;
136+ expect ( workspaceRepository . save ) . not . toHaveBeenCalled ( ) ;
137+ expect ( roleRepository . save ) . not . toHaveBeenCalled ( ) ;
133138 } ) ;
134139 } ) ;
135140
0 commit comments