Add Unit Tests for CategoryEditSearchRecyclerViewAdapter (#4775)

* Add Unit Tests for CategoryEditSearchRecyclerViewAdapter

* Use mock context instead of robolectric context
This commit is contained in:
Madhur Gupta 2022-02-02 11:54:32 +05:30 committed by GitHub
parent 54676ba0d7
commit ff8f067841
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 20 deletions

View file

@ -47,32 +47,12 @@ public class CategoryEditSearchRecyclerViewAdapter
}
}
public void addToCategories(String categoryToBeAdded) {
if (!categories.contains(categoryToBeAdded)) {
categories.add(categoryToBeAdded);
}
}
public void removeFromCategories(String categoryToBeRemoved) {
if (categories.contains(categoryToBeRemoved)) {
categories.remove(categoryToBeRemoved);
}
}
public void removeFromNewCategories(String categoryToBeRemoved) {
if (newCategories.contains(categoryToBeRemoved)) {
newCategories.remove(categoryToBeRemoved);
}
}
public void addToNewCategories(List<String> newCategories) {
for(String category : newCategories) {
if (!this.newCategories.contains(category)) {
this.newCategories.add(category);
}
}
}
public void addToNewCategories(String addedCategory) {
if (!newCategories.contains(addedCategory)) {
newCategories.add(addedCategory);

View file

@ -0,0 +1,88 @@
package fr.free.nrw.commons.category
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import fr.free.nrw.commons.nearby.Label
import org.junit.Before
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.powermock.reflect.Whitebox
class CategoryEditSearchRecyclerViewAdapterUnitTest {
private lateinit var adapter: CategoryEditSearchRecyclerViewAdapter
@Mock
private lateinit var context: Context
@Mock
private lateinit var labels: ArrayList<Label>
@Mock
private lateinit var recyclerView: RecyclerView
@Mock
private lateinit var categoryClient: CategoryClient
@Mock
private lateinit var callback: CategoryEditSearchRecyclerViewAdapter.Callback
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
adapter =
CategoryEditSearchRecyclerViewAdapter(
context,
labels,
recyclerView,
categoryClient,
callback
)
}
@Test
fun testAddToCategories() {
val categories = mutableListOf<String>()
Whitebox.setInternalState(adapter, "categories", categories)
val testCategories = listOf("someString")
adapter.addToCategories(testCategories)
assertEquals(categories.size, testCategories.size)
}
@Test
fun testRemoveFromNewCategories() {
val testCategory = "someString"
val newCategories = mutableListOf(testCategory)
val originalSize = newCategories.size
Whitebox.setInternalState(adapter, "newCategories", newCategories)
adapter.removeFromNewCategories(testCategory)
assertEquals(newCategories.size, originalSize - 1)
}
@Test
fun testAddToNewCategories() {
val testCategory = "someString"
val newCategories = mutableListOf<String>()
val originalSize = newCategories.size
Whitebox.setInternalState(adapter, "newCategories", newCategories)
adapter.addToNewCategories(testCategory)
assertEquals(newCategories.size, originalSize + 1)
}
@Test
fun testGetCategories() {
val categories = mutableListOf<String>()
Whitebox.setInternalState(adapter, "categories", categories)
assertEquals(adapter.categories, categories)
}
@Test
fun testGetNewCategories() {
val newCategories = mutableListOf<String>()
Whitebox.setInternalState(adapter, "newCategories", newCategories)
assertEquals(adapter.newCategories, newCategories)
}
}