@@ -36,10 +36,15 @@ function makeMessage(id: string, content: string, hasRead = false) {
3636function renderMessages ( readMsgs : unknown [ ] , unreadMsgs : unknown [ ] ) {
3737 const routeProps = { loaderData : { readMsgs, unreadMsgs } , params : { } , matches : [ ] } as any ;
3838 const router = createMemoryRouter (
39- [ { path : "/my/messages" , element : < Messages { ...routeProps } /> } ] ,
39+ [
40+ { path : "/my/messages" , element : < Messages { ...routeProps } /> } ,
41+ { path : "/topic/:topicId" , element : < div > 话题详情</ div > } ,
42+ { path : "/user/:loginname" , element : < div > 用户详情</ div > } ,
43+ ] ,
4044 { initialEntries : [ "/my/messages" ] } ,
4145 ) ;
42- return render ( < RouterProvider router = { router } /> ) ;
46+ const result = render ( < RouterProvider router = { router } /> ) ;
47+ return { ...result , router } ;
4348}
4449
4550function messageGroup ( title : string ) {
@@ -109,4 +114,39 @@ describe("messages page", () => {
109114 expect ( messageGroup ( "过往消息" ) . getByText ( "不错哦" ) ) . toBeInTheDocument ( ) ;
110115 expect ( screen . queryByText ( / < p > | < \/ p > / ) ) . not . toBeInTheDocument ( ) ;
111116 } ) ;
117+
118+ it ( "marks an unread message as read while opening its topic" , async ( ) => {
119+ mocks . apiFetch . mockResolvedValue ( { success : true } ) ;
120+ const { router } = renderMessages ( [ ] , [ makeMessage ( "opening" , "新回复" ) ] ) ;
121+
122+ await userEvent . click ( messageGroup ( "新消息" ) . getByRole ( "link" , { name : "Markdown 摘要" } ) ) ;
123+
124+ expect ( mocks . apiFetch ) . toHaveBeenCalledWith ( "/api/v1/message/mark_one/opening" , {
125+ method : "POST" ,
126+ body : JSON . stringify ( { } ) ,
127+ } ) ;
128+ expect ( router . state . location . pathname ) . toBe ( "/topic/2" ) ;
129+ expect ( router . state . location . hash ) . toBe ( "#3" ) ;
130+ expect ( screen . getByText ( "话题详情" ) ) . toBeInTheDocument ( ) ;
131+ } ) ;
132+
133+ it ( "does not mark an unread message as read when opening its author" , async ( ) => {
134+ const { router } = renderMessages ( [ ] , [ makeMessage ( "author" , "新回复" ) ] ) ;
135+
136+ await userEvent . click ( messageGroup ( "新消息" ) . getByRole ( "link" , { name : "alice" } ) ) ;
137+
138+ expect ( mocks . apiFetch ) . not . toHaveBeenCalled ( ) ;
139+ expect ( router . state . location . pathname ) . toBe ( "/user/alice" ) ;
140+ expect ( screen . getByText ( "用户详情" ) ) . toBeInTheDocument ( ) ;
141+ } ) ;
142+
143+ it ( "does not mark an already-read message again when opening its topic" , async ( ) => {
144+ const { router } = renderMessages ( [ makeMessage ( "read" , "旧回复" , true ) ] , [ ] ) ;
145+
146+ await userEvent . click ( messageGroup ( "过往消息" ) . getByRole ( "link" , { name : "Markdown 摘要" } ) ) ;
147+
148+ expect ( mocks . apiFetch ) . not . toHaveBeenCalled ( ) ;
149+ expect ( router . state . location . pathname ) . toBe ( "/topic/2" ) ;
150+ expect ( router . state . location . hash ) . toBe ( "#3" ) ;
151+ } ) ;
112152} ) ;
0 commit comments