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

This commit is contained in:
Javier Cicchelli 2024-03-22 15:15:47 +01:00
parent 8eec6f5666
commit a5ec80015a

View File

@ -49,7 +49,7 @@ final class FeedListViewModelTests: XCTestCase {
}
// MARK: Functions tests
func testFetch_allItems_whenResponseOK_withSomeItems() {
func testFetch_allItems_whenResponseOK_withSomeItems() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -70,7 +70,7 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -84,7 +84,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.state, .populated)
}
func testFetch_allItems_whenResponseOK_withNoItems() {
func testFetch_allItems_whenResponseOK_withNoItems() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -105,7 +105,7 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -118,7 +118,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.state, .empty)
}
func testFetch_allItems_whenResponseNotOK() {
func testFetch_allItems_whenResponseNotOK() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -136,7 +136,7 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -149,7 +149,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.state, .error)
}
func testFetch_filteredItems_whenResponseOK_withSomeItems() {
func testFetch_filteredItems_whenResponseOK_withSomeItems() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -171,7 +171,8 @@ final class FeedListViewModelTests: XCTestCase {
.store(in: &cancellables)
sut.filter(by: .only1Star)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -188,7 +189,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.words.count, 3)
}
func testFetch_filteredItems_whenResponseOK_withNoItems() {
func testFetch_filteredItems_whenResponseOK_withNoItems() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -210,7 +211,8 @@ final class FeedListViewModelTests: XCTestCase {
.store(in: &cancellables)
sut.filter(by: .only1Star)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -225,7 +227,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertTrue(sut.words.isEmpty)
}
func testFetch_filteredItems_whenResponseNotOK() {
func testFetch_filteredItems_whenResponseNotOK() async {
let expectation = XCTestExpectation()
var isLoading: [Bool] = []
@ -244,7 +246,8 @@ final class FeedListViewModelTests: XCTestCase {
.store(in: &cancellables)
sut.filter(by: .only1Star)
sut.fetch()
await sut.fetch()
wait(for: [expectation], timeout: 2)
@ -259,7 +262,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertTrue(sut.words.isEmpty)
}
func testFilter_forNewOption_withSomeItems() {
func testFilter_forNewOption_withSomeItems() async {
let expectation = XCTestExpectation()
var filter: [FilterOption] = []
@ -280,7 +283,8 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
sut.filter(by: .only1Star)
wait(for: [expectation], timeout: 2)
@ -294,7 +298,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.words.count, 3)
}
func testFilter_forNewOption_withNoItems() {
func testFilter_forNewOption_withNoItems() async {
let expectation = XCTestExpectation()
var filter: [FilterOption] = []
@ -315,7 +319,8 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
sut.filter(by: .only1Star)
wait(for: [expectation], timeout: 2)
@ -329,7 +334,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.words.count, 0)
}
func testFilter_forSameOption_withSomeItems() {
func testFilter_forSameOption_withSomeItems() async {
let expectation = XCTestExpectation()
var filter: [FilterOption] = []
@ -350,7 +355,8 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
sut.filter(by: .only1Star)
sut.filter(by: .only1Star)
@ -365,7 +371,7 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.words.count, 3)
}
func testFilter_forSameOption_withNoItems() {
func testFilter_forSameOption_withNoItems() async {
let expectation = XCTestExpectation()
var filter: [FilterOption] = []
@ -386,7 +392,8 @@ final class FeedListViewModelTests: XCTestCase {
}
.store(in: &cancellables)
sut.fetch()
await sut.fetch()
sut.filter(by: .only1Star)
wait(for: [expectation], timeout: 2)
@ -400,4 +407,88 @@ final class FeedListViewModelTests: XCTestCase {
XCTAssertEqual(sut.words.count, 0)
}
func testItemFor_index_withSomeItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
let item = sut.item(for: 0)
// THEN
XCTAssertNotNil(item)
}
func testItemFor_indexOutOfBounds_withSomeItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
let item = sut.item(for: 10)
// THEN
XCTAssertNil(item)
}
func testItemFor_index_withFilteredItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
sut.filter(by: .only1Star)
let item = sut.item(for: 1)
// THEN
XCTAssertNil(item)
}
func testItemFor_indexOutOfBounds_withFilteredItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .sample)
)
// WHEN
await sut.fetch()
sut.filter(by: .only1Star)
let item = sut.item(for: 2)
// THEN
XCTAssertNil(item)
}
func testItemFor_index_withNoItems() async {
// GIVEN
MockURLProtocol.response = .init(
statusCode: 200,
object: Feed(entries: .none)
)
// WHEN
await sut.fetch()
let item = sut.item(for: 0)
// THEN
XCTAssertNil(item)
}
}