@@ -24,12 +24,15 @@ import org.wordpress.android.fluxc.action.TaxonomyAction
24
24
import org.wordpress.android.fluxc.annotations.action.Action
25
25
import org.wordpress.android.fluxc.model.SiteModel
26
26
import org.wordpress.android.fluxc.network.rest.wpapi.rs.WpApiClientProvider
27
+ import org.wordpress.android.fluxc.model.TermModel
27
28
import org.wordpress.android.fluxc.store.TaxonomyStore.FetchTermsResponsePayload
29
+ import org.wordpress.android.fluxc.store.TaxonomyStore.RemoteTermPayload
28
30
import org.wordpress.android.fluxc.store.TaxonomyStore.TaxonomyErrorType
29
31
import org.wordpress.android.fluxc.utils.AppLogWrapper
30
32
import rs.wordpress.api.kotlin.WpApiClient
31
33
import rs.wordpress.api.kotlin.WpRequestResult
32
34
import uniffi.wp_api.CategoryWithEditContext
35
+ import uniffi.wp_api.CategoriesRequestCreateResponse
33
36
import uniffi.wp_api.CategoriesRequestListWithEditContextResponse
34
37
import uniffi.wp_api.TagWithEditContext
35
38
import uniffi.wp_api.TagsRequestListWithEditContextResponse
@@ -56,6 +59,18 @@ class TaxonomyRsApiRestClientTest {
56
59
url = " https://test.wordpress.com"
57
60
}
58
61
62
+ private val testTermModel = TermModel (
63
+ 1 , // id
64
+ 123 , // localSiteId
65
+ 2L , // remoteTermId
66
+ " category" , // taxonomy
67
+ " Test Category" , // name
68
+ " test-category" , // slug
69
+ " Test category description" , // description
70
+ 0L , // parentRemoteId
71
+ 0 // postCount
72
+ )
73
+
59
74
private val testTagTaxonomyName = " post_tag"
60
75
private val testCategoryTaxonomyName = " category"
61
76
@@ -199,6 +214,70 @@ class TaxonomyRsApiRestClientTest {
199
214
assertNull(payload.error)
200
215
}
201
216
217
+ @Test
218
+ fun `createPostCategory with error response dispatches error action` () = runTest {
219
+ // Use a concrete error type that we can create - UnknownError requires statusCode and response
220
+ val errorResponse = WpRequestResult .UnknownError <Any >(
221
+ statusCode = 500u ,
222
+ response = " Internal Server Error"
223
+ )
224
+
225
+ whenever(wpApiClient.request<Any >(any())).thenReturn(errorResponse)
226
+
227
+ taxonomyClient.createPostCategory(testSite, testTermModel)
228
+
229
+ // Verify dispatcher was called with error action
230
+ val actionCaptor = ArgumentCaptor .forClass(Action ::class .java)
231
+ verify(dispatcher).dispatch(actionCaptor.capture())
232
+
233
+ val capturedAction = actionCaptor.value
234
+ val payload = capturedAction.payload as RemoteTermPayload
235
+ assertEquals(capturedAction.type, TaxonomyAction .PUSHED_TERM )
236
+ assertEquals(testSite, payload.site)
237
+ assertEquals(testTermModel, payload.term)
238
+ assertNotNull(payload.error)
239
+ assertEquals(TaxonomyErrorType .GENERIC_ERROR , payload.error?.type)
240
+ }
241
+
242
+ @Test
243
+ fun `createPostCategory with success response dispatches success action` () = runTest {
244
+ val categoryWithEditContext = createTestCategoryWithEditContext()
245
+
246
+ // Create the correct response structure following the MediaRSApiRestClientTest pattern
247
+ val categoryResponse = CategoriesRequestCreateResponse (
248
+ categoryWithEditContext,
249
+ mock<WpNetworkHeaderMap >()
250
+ )
251
+
252
+ val successResponse: WpRequestResult <CategoriesRequestCreateResponse > = WpRequestResult .Success (
253
+ response = categoryResponse
254
+ )
255
+
256
+ whenever(wpApiClient.request<CategoriesRequestCreateResponse >(any())).thenReturn(successResponse)
257
+
258
+ taxonomyClient.createPostCategory(testSite, testTermModel)
259
+
260
+ // Verify dispatcher was called with success action
261
+ val actionCaptor = ArgumentCaptor .forClass(Action ::class .java)
262
+ verify(dispatcher).dispatch(actionCaptor.capture())
263
+
264
+ val capturedAction = actionCaptor.value
265
+ val payload = capturedAction.payload as RemoteTermPayload
266
+ assertEquals(capturedAction.type, TaxonomyAction .PUSHED_TERM )
267
+ assertEquals(testSite, payload.site)
268
+ assertNotNull(payload.term)
269
+ // Verify the created term has the correct properties
270
+ assertEquals(categoryWithEditContext.id.toInt(), payload.term.id)
271
+ assertEquals(testSite.id, payload.term.localSiteId)
272
+ assertEquals(categoryWithEditContext.id, payload.term.remoteTermId)
273
+ assertEquals(testCategoryTaxonomyName, payload.term.taxonomy)
274
+ assertEquals(categoryWithEditContext.name, payload.term.name)
275
+ assertEquals(categoryWithEditContext.slug, payload.term.slug)
276
+ assertEquals(categoryWithEditContext.description, payload.term.description)
277
+ assertEquals(categoryWithEditContext.count.toInt(), payload.term.postCount)
278
+ assertNull(payload.error)
279
+ }
280
+
202
281
private fun createTestCategoryWithEditContext (): CategoryWithEditContext {
203
282
return CategoryWithEditContext (
204
283
id = 2L ,
0 commit comments