265 lines
7.0 KiB
Swift
265 lines
7.0 KiB
Swift
//
|
|
// FeedListViewModelTests.swift
|
|
// ReviewsFeedTests
|
|
//
|
|
// Created by Javier Cicchelli on 22/03/2024.
|
|
// Copyright © 2024 Röck+Cöde. All rights reserved.
|
|
//
|
|
|
|
import Combine
|
|
import ReviewsFoundationKit
|
|
import ReviewsiTunesKit
|
|
import XCTest
|
|
|
|
@testable import ReviewsFeed
|
|
|
|
final class FeedListViewModelTests: XCTestCase {
|
|
|
|
// MARK: Properties
|
|
private var sut: FeedListViewController.ViewModel!
|
|
|
|
private var cancellables: Set<AnyCancellable> = []
|
|
|
|
// MARK: Setup
|
|
override func setUp() async throws {
|
|
sut = .init(
|
|
configuration: .init(session: .mock),
|
|
coordination: nil
|
|
)
|
|
}
|
|
|
|
override func tearDown() async throws {
|
|
cancellables.removeAll()
|
|
}
|
|
|
|
// MARK: Initialisers tests
|
|
func testInit() {
|
|
// GIVEN
|
|
// WHEN
|
|
// THEN
|
|
XCTAssertEqual(sut.filter, .all)
|
|
XCTAssertFalse(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isFiltering)
|
|
XCTAssertFalse(sut.isLoading)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertTrue(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.itemsCount, 0)
|
|
XCTAssertEqual(sut.state, .initial)
|
|
XCTAssertTrue(sut.words.isEmpty)
|
|
}
|
|
|
|
// MARK: Functions tests
|
|
func testFetch_allItems_whenResponseOK_withSomeItems() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
MockURLProtocol.response = .init(
|
|
statusCode: 200,
|
|
object: Feed(entries: .sample)
|
|
)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertTrue(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertFalse(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.items.count, 5)
|
|
XCTAssertEqual(sut.itemsCount, 5)
|
|
XCTAssertEqual(sut.state, .populated)
|
|
}
|
|
|
|
func testFetch_allItems_whenResponseOK_withNoItems() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
MockURLProtocol.response = .init(
|
|
statusCode: 200,
|
|
object: Feed(entries: .none)
|
|
)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertFalse(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertTrue(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.itemsCount, 0)
|
|
XCTAssertEqual(sut.state, .empty)
|
|
}
|
|
|
|
func testFetch_allItems_whenResponseNotOK() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
MockURLProtocol.response = .init(statusCode: 404)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertFalse(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertTrue(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.itemsCount, 0)
|
|
XCTAssertEqual(sut.state, .error)
|
|
}
|
|
|
|
func testFetch_filteredItems_whenResponseOK_withSomeItems() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
sut.filter = .only1Star
|
|
|
|
MockURLProtocol.response = .init(
|
|
statusCode: 200,
|
|
object: Feed(entries: .sample)
|
|
)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertTrue(sut.isFilterEnabled)
|
|
XCTAssertTrue(sut.isWordsShowing)
|
|
XCTAssertFalse(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.items.count, 1)
|
|
XCTAssertEqual(sut.itemsCount, 2)
|
|
XCTAssertEqual(sut.state, .populated)
|
|
XCTAssertFalse(sut.words.isEmpty)
|
|
XCTAssertEqual(sut.words.count, 3)
|
|
}
|
|
|
|
func testFetch_filteredItems_whenResponseOK_withNoItems() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
sut.filter = .only1Star
|
|
|
|
MockURLProtocol.response = .init(
|
|
statusCode: 200,
|
|
object: Feed(entries: .none)
|
|
)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertFalse(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertTrue(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.itemsCount, 0)
|
|
XCTAssertEqual(sut.state, .empty)
|
|
XCTAssertTrue(sut.words.isEmpty)
|
|
}
|
|
|
|
func testFetch_filteredItems_whenResponseNotOK() {
|
|
let expectation = XCTestExpectation()
|
|
|
|
var isLoading: [Bool] = []
|
|
|
|
// GIVEN
|
|
sut.filter = .only1Star
|
|
|
|
MockURLProtocol.response = .init(statusCode: 404)
|
|
|
|
// WHEN
|
|
sut.fetch()
|
|
|
|
sut.$isLoading
|
|
.collect(3)
|
|
.sink { value in
|
|
isLoading.append(contentsOf: value)
|
|
|
|
expectation.fulfill()
|
|
}
|
|
.store(in: &cancellables)
|
|
|
|
wait(for: [expectation], timeout: 2)
|
|
|
|
// THEN
|
|
XCTAssertEqual(isLoading, [false, true, false])
|
|
XCTAssertFalse(sut.isFilterEnabled)
|
|
XCTAssertFalse(sut.isWordsShowing)
|
|
XCTAssertTrue(sut.items.isEmpty)
|
|
XCTAssertEqual(sut.itemsCount, 0)
|
|
XCTAssertEqual(sut.state, .error)
|
|
XCTAssertTrue(sut.words.isEmpty)
|
|
}
|
|
|
|
func test
|
|
|
|
}
|