|
| 1 | +package org.wordpress.android.ui.posts |
| 2 | + |
| 3 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 4 | +import org.junit.Assert.assertEquals |
| 5 | +import org.junit.Assert.assertNull |
| 6 | +import org.junit.Before |
| 7 | +import org.mockito.kotlin.any |
| 8 | +import org.mockito.kotlin.mock |
| 9 | +import org.mockito.kotlin.never |
| 10 | +import org.mockito.kotlin.verify |
| 11 | +import org.mockito.kotlin.whenever |
| 12 | +import org.wordpress.android.BaseUnitTest |
| 13 | +import org.wordpress.android.fluxc.Dispatcher |
| 14 | +import org.wordpress.android.fluxc.model.MediaModel |
| 15 | +import org.wordpress.android.fluxc.model.SiteModel |
| 16 | +import org.wordpress.android.fluxc.store.MediaStore |
| 17 | +import kotlin.test.Test |
| 18 | + |
| 19 | +@Suppress("UNCHECKED_CAST") |
| 20 | +@ExperimentalCoroutinesApi |
| 21 | +class PostListFeaturedImageTrackerTest : BaseUnitTest() { |
| 22 | + private val dispatcher: Dispatcher = mock() |
| 23 | + private val mediaStore: MediaStore = mock() |
| 24 | + |
| 25 | + private lateinit var tracker: PostListFeaturedImageTracker |
| 26 | + |
| 27 | + private val site = SiteModel().apply { id = 123 } |
| 28 | + |
| 29 | + @Before |
| 30 | + fun setup() { |
| 31 | + tracker = PostListFeaturedImageTracker(dispatcher, mediaStore) |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun `given id exists in map, when getFeaturedImageUrl invoked, then return url`() { |
| 36 | + val imageId = 123L |
| 37 | + val imageUrl = "https://example.com/image.jpg" |
| 38 | + tracker.featuredImageMap[imageId] = imageUrl |
| 39 | + |
| 40 | + val result = tracker.getFeaturedImageUrl(site, imageId) |
| 41 | + |
| 42 | + assertEquals(imageUrl, result) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `given id is 0, when getFeaturedImageUrl invoked, then return null`() { |
| 47 | + val result = tracker.getFeaturedImageUrl(site, 0L) |
| 48 | + |
| 49 | + assertNull(result) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `given id not in map and exists in store, when invoked, then return url from media store`() { |
| 54 | + val imageId = 456L |
| 55 | + val imageUrl = "https://example.com/image.jpg" |
| 56 | + val mediaModel = MediaModel(site.id, imageId).apply { |
| 57 | + url = imageUrl |
| 58 | + } |
| 59 | + |
| 60 | + whenever(mediaStore.getSiteMediaWithId(site, imageId)).thenReturn(mediaModel) |
| 61 | + |
| 62 | + val result = tracker.getFeaturedImageUrl(site, imageId) |
| 63 | + |
| 64 | + assertEquals(imageUrl, result) |
| 65 | + assertEquals(imageUrl, tracker.featuredImageMap[imageId]) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `given id not in map or store, when invoked, then return null and dispatch fetch request`() { |
| 70 | + val imageId = 123L |
| 71 | + |
| 72 | + whenever(mediaStore.getSiteMediaWithId(site, imageId)).thenReturn(null) |
| 73 | + |
| 74 | + val result = tracker.getFeaturedImageUrl(site, imageId) |
| 75 | + |
| 76 | + assertNull(result) |
| 77 | + verify(dispatcher).dispatch(any()) |
| 78 | + assert(tracker.ongoingRequests.contains(imageId)) |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + fun `given request ongoing for id, when invoked, should return null`() { |
| 83 | + val imageId = 123L |
| 84 | + |
| 85 | + tracker.ongoingRequests.add(imageId) |
| 86 | + |
| 87 | + val result = tracker.getFeaturedImageUrl(site, imageId) |
| 88 | + |
| 89 | + assertNull(result) |
| 90 | + verify(mediaStore, never()).getSiteMediaWithId(site, imageId) |
| 91 | + verify(dispatcher, never()).dispatch(any()) |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + fun `given id in map and ongoingRequests, when invalidate, then remove id from map and ongoingRequests`() { |
| 96 | + val imageId1 = 123L |
| 97 | + val imageId2 = 456L |
| 98 | + |
| 99 | + tracker.featuredImageMap[imageId1] = "https://example.com/image1.jpg" |
| 100 | + tracker.featuredImageMap[imageId2] = "https://example.com/image2.jpg" |
| 101 | + tracker.ongoingRequests.add(imageId1) |
| 102 | + tracker.ongoingRequests.add(imageId2) |
| 103 | + |
| 104 | + tracker.invalidateFeaturedMedia(listOf(imageId1, imageId2)) |
| 105 | + |
| 106 | + assertNull(tracker.featuredImageMap[imageId1]) |
| 107 | + assertNull(tracker.featuredImageMap[imageId2]) |
| 108 | + assert(!tracker.ongoingRequests.contains(imageId1)) |
| 109 | + assert(!tracker.ongoingRequests.contains(imageId2)) |
| 110 | + } |
| 111 | +} |
0 commit comments