Implemented some test cases for the "openItemAt(:)" function of the FeedListViewModel view model in the Feed framework.

This commit is contained in:
Javier Cicchelli 2024-03-22 15:40:47 +01:00
parent de9f53e497
commit 9436244b34
2 changed files with 99 additions and 9 deletions

View File

@ -116,16 +116,19 @@ extension FeedListViewController {
}
func item(for index: Int) -> Review? {
guard
!items.isEmpty,
index < items.count
else {
guard !items.isEmpty else {
return nil
}
return isWordsShowing
? items[index - 1]
: items[index]
let indexToUse = isWordsShowing
? index - 1
: index
guard indexToUse < items.count else {
return nil
}
return items[indexToUse]
}
func openItem(at index: Int) {

View File

@ -15,6 +15,9 @@ import XCTest
final class FeedListViewModelTests: XCTestCase {
// MARK: Constants
private let coordination: FeedListCoordinationSpy = .init()
// MARK: Properties
private var sut: FeedListViewController.ViewModel!
@ -24,7 +27,7 @@ final class FeedListViewModelTests: XCTestCase {
override func setUp() async throws {
sut = .init(
configuration: .init(session: .mock),
coordination: nil
coordination: coordination
)
}
@ -454,7 +457,7 @@ final class FeedListViewModelTests: XCTestCase {
let item = sut.item(for: 1)
// THEN
XCTAssertNil(item)
XCTAssertNotNil(item)
}
func testItemFor_indexOutOfBounds_withFilteredItems() async {
@ -491,4 +494,88 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertNil(item)
}
func testOpenItemAt_index_withSomeItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
sut.openItem(at: 0)
// THEN
XCTAssertTrue(coordination.itemOpened)
}
func testOpenItemAt_indexOutOfBounds_withSomeItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
sut.openItem(at: 10)
// THEN
XCTAssertFalse(coordination.itemOpened)
}
func testOpenItemAt_index_withFilteredItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
sut.filter(by: .only1Star)
await sut.fetch()
sut.openItem(at: 1)
// THEN
XCTAssertTrue(coordination.itemOpened)
}
func testOpenItemAt_indexOutOfBounds_withFilteredItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
sut.filter(by: .only1Star)
await sut.fetch()
sut.openItem(at: 2)
// THEN
XCTAssertFalse(coordination.itemOpened)
}
func testOpenItemAt_index_withNoItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .none)
)
// WHEN
await sut.fetch()
sut.openItem(at: 0)
// THEN
XCTAssertFalse(coordination.itemOpened)
}
}