@@ -21,21 +21,22 @@ final class ChaptersViewModelTests: XCTestCase {
2121 libraryServiceMock = LibraryServiceProtocolMock ( )
2222 }
2323
24- private func makeItem( relativePath: String , isBoundBook: Bool ) -> PlayableItem {
25- PlayableItem (
24+ private func makeItem( relativePath: String , isBoundBook: Bool , chapterCount: Int = 1 ) -> PlayableItem {
25+ let chapters = ( 0 ..< chapterCount) . map { index in
26+ PlayableChapter (
27+ title: " chapter \( index + 1 ) " ,
28+ author: " author " ,
29+ start: Double ( index) * 10 ,
30+ duration: 10 ,
31+ relativePath: relativePath,
32+ remoteURL: nil ,
33+ index: Int16 ( index)
34+ )
35+ }
36+ return PlayableItem (
2637 title: " title " ,
2738 author: " author " ,
28- chapters: [
29- PlayableChapter (
30- title: " chapter " ,
31- author: " author " ,
32- start: 0 ,
33- duration: 100 ,
34- relativePath: relativePath,
35- remoteURL: nil ,
36- index: 0
37- )
38- ] ,
39+ chapters: chapters,
3940 currentTime: 0 ,
4041 duration: 100 ,
4142 relativePath: relativePath,
@@ -70,16 +71,22 @@ final class ChaptersViewModelTests: XCTestCase {
7071 XCTAssertFalse ( makeSUT ( ) . canReloadChapters)
7172 }
7273
73- func testReloadChapters_whenMoreFound_reloadsItemAndAlerts ( ) async {
74+ func testReloadChapters_whenMoreFound_reloadsItemAndRefreshesList ( ) async {
7475 createLocalFile ( named: " reparse.m4b " )
75- playerManagerMock. currentItem = makeItem ( relativePath: " reparse.m4b " , isBoundBook: false )
76+ playerManagerMock. currentItem = makeItem ( relativePath: " reparse.m4b " , isBoundBook: false , chapterCount : 1 )
7677 libraryServiceMock. reloadChaptersRelativePathReturnValue = 5
78+ // Simulate PlayerManager rebuilding currentItem from storage with the new chapter count.
79+ let reloadedItem = makeItem ( relativePath: " reparse.m4b " , isBoundBook: false , chapterCount: 5 )
80+ playerManagerMock. reloadCurrentItemClosure = { [ weak self] in
81+ self ? . playerManagerMock. currentItem = reloadedItem
82+ }
7783 let sut = makeSUT ( )
7884
7985 await sut. reloadChapters ( )
8086
8187 XCTAssertTrue ( libraryServiceMock. reloadChaptersRelativePathCalled)
8288 XCTAssertTrue ( playerManagerMock. reloadCurrentItemCalled)
89+ XCTAssertEqual ( sut. chapters. count, 5 )
8390 XCTAssertNotNil ( sut. currentAlert)
8491 XCTAssertFalse ( sut. isReloadingChapters)
8592 }
@@ -94,6 +101,7 @@ final class ChaptersViewModelTests: XCTestCase {
94101 XCTAssertFalse ( libraryServiceMock. reloadChaptersRelativePathCalled)
95102 XCTAssertFalse ( playerManagerMock. reloadCurrentItemCalled)
96103 XCTAssertNotNil ( sut. currentAlert)
104+ XCTAssertFalse ( sut. isReloadingChapters)
97105 }
98106
99107 func testReloadChapters_whenNoAdditionalFound_alertsAndDoesNotReloadItem( ) async {
@@ -107,6 +115,7 @@ final class ChaptersViewModelTests: XCTestCase {
107115 XCTAssertTrue ( libraryServiceMock. reloadChaptersRelativePathCalled)
108116 XCTAssertFalse ( playerManagerMock. reloadCurrentItemCalled)
109117 XCTAssertNotNil ( sut. currentAlert)
118+ XCTAssertFalse ( sut. isReloadingChapters)
110119 }
111120
112121 func testReloadChapters_boundBook_isNoOp( ) async {
0 commit comments