@@ -181,6 +181,63 @@ final class ProductImagesSaverTests: XCTestCase {
181181 XCTAssertEqual ( savedImages, [ image] )
182182 }
183183
184+ func test_savedProduct_is_updated_correctly( ) {
185+ // Given
186+ let stores = MockStoresManager ( sessionManager: . testingInstance)
187+ let actionHandler = ProductImageActionHandler ( siteID: siteID, productID: . product( id: productID) , imageStatuses: [ ] , stores: stores)
188+ let asset : ProductImageAssetType = . phAsset( asset: PHAsset ( ) )
189+ let imagesSaver = ProductImagesSaver ( siteID: siteID, productOrVariationID: . product( id: productID) , stores: stores)
190+ let expectedProduct = Product . fake ( ) . copy ( images: [ ] )
191+
192+ // Mocks successful product images update.
193+ stores. whenReceivingAction ( ofType: ProductAction . self) { action in
194+ if case let . updateProductImages( _, _, _, onCompletion) = action {
195+ onCompletion ( . success( expectedProduct) )
196+ }
197+ }
198+
199+ // Uploads an image and waits for the image upload completion closure to be called later.
200+ let imageUploadCompletion : ( ( Result < Media , Error > ) -> Void ) = waitFor { promise in
201+ stores. whenReceivingAction ( ofType: MediaAction . self) { action in
202+ if case let . uploadMedia( _, _, _, _, _, onCompletion) = action {
203+ promise ( onCompletion)
204+ }
205+ }
206+ actionHandler. uploadMediaAssetToSiteMediaLibrary ( asset: asset)
207+ }
208+
209+ waitFor { promise in
210+ // Saves product images.
211+ imagesSaver. saveProductImagesWhenNoneIsPendingUploadAnymore ( imageActionHandler: actionHandler) { _ in
212+ promise ( ( ) )
213+ }
214+ XCTAssertNil ( imagesSaver. savedProduct)
215+
216+ // When
217+ // Mocks successful image upload.
218+ imageUploadCompletion ( . success( . fake( ) . copy ( mediaID: 645 ) ) )
219+ }
220+
221+ // Then
222+ XCTAssertEqual ( imagesSaver. savedProduct, expectedProduct)
223+
224+ // When
225+ // Uploads another image and save.
226+ let imageUploadCompletionAfterSave : ( ( Result < Media , Error > ) -> Void ) = waitFor { promise in
227+ stores. whenReceivingAction ( ofType: MediaAction . self) { action in
228+ if case let . uploadMedia( _, _, _, _, _, onCompletion) = action {
229+ promise ( onCompletion)
230+ }
231+ }
232+ actionHandler. uploadMediaAssetToSiteMediaLibrary ( asset: asset)
233+ }
234+ imageUploadCompletionAfterSave ( . success( . fake( ) . copy ( mediaID: 606 ) ) )
235+ imagesSaver. saveProductImagesWhenNoneIsPendingUploadAnymore ( imageActionHandler: actionHandler) { _ in }
236+
237+ // Then the saved product is reset immediately
238+ XCTAssertNil ( imagesSaver. savedProduct)
239+ }
240+
184241 // MARK: - Analytics
185242
186243 func test_savingProductAfterBackgroundImageUploadSuccess_is_tracked_on_variation_update_success( ) throws {
0 commit comments