Skip to content

Commit

Permalink
refactor in home view controller and added home view controller tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
trnserdar committed Jan 9, 2021
1 parent e29f51d commit 900e27c
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 20 deletions.
34 changes: 14 additions & 20 deletions Netflix/ViewControllers/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import UIKit
class HomeViewController: UIViewController {

let homeView = HomeView()
lazy var netflixClient = NetflixClient()
private let group = DispatchGroup()
private let queue = DispatchQueue.global(qos: .utility)
private var newReleases: [SearchResult] = []
private var actions: [SearchResult] = []
lazy var netflixClient: NetflixClientProtocol = NetflixClient()
let group = DispatchGroup()
let queue = DispatchQueue.global(qos: .utility)
var newReleases: [SearchResult] = []
var actions: [SearchResult] = []
lazy var favoriteManager: FavoriteManagerProtocol = FavoriteManager()
weak var coordinator: HomeCoordinator?

Expand All @@ -38,29 +38,24 @@ class HomeViewController: UIViewController {

func listenEvents() {
homeView.newReleaseView.showAllTapped = { [weak self] results in
guard let self = self else { return }
self.coordinator?.showResult(navigationTitle: TextConstants.newReleases, results: results)
self?.coordinator?.showResult(navigationTitle: TextConstants.newReleases, results: results)
}

homeView.actionView.showAllTapped = { [weak self] results in
guard let self = self else { return }
self.coordinator?.showResult(navigationTitle: TextConstants.crimeActionAdventure, results: results)
self?.coordinator?.showResult(navigationTitle: TextConstants.crimeActionAdventure, results: results)
}

homeView.resultSelected = { [weak self] searchResult in
guard let self = self else { return }
self.coordinator?.showResultDetail(result: searchResult)
self?.coordinator?.showResultDetail(result: searchResult)
}

homeView.favoriteSelected = { [weak self] searchResult in
guard let self = self else { return }
self.favoriteManager.favoriteAction(result: searchResult)
self?.favoriteManager.favoriteAction(result: searchResult)
}

favoriteManager.favoritesChanged = { [weak self] favorites in
guard let self = self else { return }
self.homeView.viewModel.newRelease.favorites = favorites
self.homeView.viewModel.action.favorites = favorites
self?.homeView.viewModel.newRelease.favorites = favorites
self?.homeView.viewModel.action.favorites = favorites
}

}
Expand All @@ -78,10 +73,9 @@ class HomeViewController: UIViewController {
}

group.notify(queue: .main) { [weak self] in
guard let self = self else { return }
self.homeView.baseViewModel.isActivityIndicatorEnabled = false
self.homeView.viewModel.newRelease.searchResults = self.newReleases
self.homeView.viewModel.action.searchResults = self.actions
self?.homeView.baseViewModel.isActivityIndicatorEnabled = false
self?.homeView.viewModel.newRelease.searchResults = self?.newReleases ?? []
self?.homeView.viewModel.action.searchResults = self?.actions ?? []
}

}
Expand Down
242 changes: 242 additions & 0 deletions NetflixTests/Cases/ViewControllers/HomeViewControllerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
//
// HomeViewControllerTests.swift
// NetflixTests
//
// Created by SERDAR TURAN on 6.01.2021.
//

import XCTest
@testable import Netflix

class HomeViewControllerTests: XCTestCase {

var sut: HomeViewController!
var mockNetflixClient: MockNetflixClient!

override func setUp() {
super.setUp()
sut = HomeViewController()
mockNetflixClient = MockNetflixClient()
sut.netflixClient = mockNetflixClient
sut.loadViewIfNeeded()
}

override func tearDown() {
sut = nil
mockNetflixClient = nil
super.tearDown()
}

func test_initHome_setHomeView() {
XCTAssertTrue((sut.view as Any) is HomeView)
}

func test_listenEvents_newReleaseView_whenShowAllTapped_whenSearchResultsEmpty_setResults() {
sut.homeView.newReleaseView.showAllTapped = { results in
XCTAssertEqual(results.count, 0)
}
sut.homeView.newReleaseView.showAllTapped?([])
}

func test_listenEvents_newReleaseView_whenShowAllTapped_whenGiven1SearchResult_setResults() {
sut.homeView.newReleaseView.showAllTapped = { results in
XCTAssertEqual(results.count, 1)
}
sut.homeView.newReleaseView.showAllTapped?([SearchResult.dummy])
}

func test_listenEvents_newReleaseView_whenShowAllTapped_whenGiven2SearchResults_setResults() {
sut.homeView.newReleaseView.showAllTapped = { results in
XCTAssertEqual(results.count, 2)
}
sut.homeView.newReleaseView.showAllTapped?([SearchResult.dummy, SearchResult.dummy])
}

func test_listenEvents_newReleaseView_whenShowAllTapped_setWeakReference() {
sut.homeView.newReleaseView.showAllTapped = { [weak sut] results in
XCTAssertNotNil(sut)
}
sut.homeView.newReleaseView.showAllTapped?([SearchResult.dummy, SearchResult.dummy])
}

func test_listenEvents_actionView_whenShowAllTapped_whenSearchResultsEmpty_setResults() {
sut.homeView.actionView.showAllTapped = { results in
XCTAssertEqual(results.count, 0)
}
sut.homeView.actionView.showAllTapped?([])
}

func test_listenEvents_actionView_whenShowAllTapped_whenGiven1SearchResult_setResults() {
sut.homeView.actionView.showAllTapped = { results in
XCTAssertEqual(results.count, 1)
}
sut.homeView.actionView.showAllTapped?([SearchResult.dummy])
}

func test_listenEvents_actionView_whenShowAllTapped_whenGiven2SearchResults_setResults() {
sut.homeView.actionView.showAllTapped = { results in
XCTAssertEqual(results.count, 2)
}
sut.homeView.actionView.showAllTapped?([SearchResult.dummy, SearchResult.dummy])
}

func test_listenEvents_actionView_whenShowAllTapped_setWeakReference() {
sut.homeView.actionView.showAllTapped = { [weak sut] results in
XCTAssertNotNil(sut)
}
sut.homeView.actionView.showAllTapped?([SearchResult.dummy, SearchResult.dummy])
}

func test_listenEvents_whenResultSelected_setResult() {
sut.homeView.resultSelected = { result in
XCTAssertEqual(result.title, SearchResult.dummy.title)
}
sut.homeView.resultSelected?(SearchResult.dummy)
}

func test_listenEvents_whenResultSelected_setWeakReference() {
sut.homeView.resultSelected = { [weak sut] result in
XCTAssertNotNil(sut)
}
sut.homeView.resultSelected?(SearchResult.dummy)
}

func test_listenEvents_whenFavoriteSelected_setResult() {
sut.homeView.favoriteSelected = { result in
XCTAssertEqual(result.title, SearchResult.dummy.title)
}
sut.homeView.favoriteSelected?(SearchResult.dummy)
}

func test_listenEvents_whenFavoriteSelected_setWeakReference() {
sut.homeView.favoriteSelected = { [weak sut] result in
XCTAssertNotNil(sut)
}
sut.homeView.favoriteSelected?(SearchResult.dummy)
}

func test_listenEvents_whenFavoriteChanged_setFavorites() {
sut.favoriteManager.favoritesChanged = { favorites in
XCTAssertEqual(favorites.count, 2)
}
sut.favoriteManager.favoritesChanged?([TitleDetail.dummy, TitleDetail.dummy])
}

func test_listenEvents_whenFavoriteChanged_setWeakReference() {
sut.favoriteManager.favoritesChanged = { [weak sut] favorites in
XCTAssertNotNil(sut)
}
sut.favoriteManager.favoritesChanged?([TitleDetail.dummy, TitleDetail.dummy])
}

func test_getNewReleases_whenGiven2SearchResults_setNewReleases() {
let expectationNewReleases = expectation(description: "newReleases")
sut.group.enter()
sut.getNewReleases()
sut.group.notify(queue: .main) {
expectationNewReleases.fulfill()
}
mockNetflixClient.newReleasesCompletion?([SearchResult.dummy, SearchResult.dummy], nil)
wait(for: [expectationNewReleases], timeout: 3.0)
XCTAssertEqual(sut.newReleases.count, 2)
}

func test_getNewReleases_whenEmptySearchResult_setNewReleases() {
let expectationNewReleases = expectation(description: "newReleases")
sut.group.enter()
sut.getNewReleases()
sut.group.notify(queue: .main) {
expectationNewReleases.fulfill()
}
mockNetflixClient.newReleasesCompletion?([], nil)
wait(for: [expectationNewReleases], timeout: 3.0)
XCTAssertEqual(sut.newReleases.count, 0)
}

func test_getNew100_whenGiven2SearchResults_setActions() {
let expectationActions = expectation(description: "actions")
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: [1]))
sut.group.notify(queue: .main) {
expectationActions.fulfill()
}
mockNetflixClient.searchCompletion?([SearchResult.dummy, SearchResult.dummy], nil)
wait(for: [expectationActions], timeout: 3.0)
XCTAssertEqual(sut.actions.count, 2)
}

func test_getNew100_whenEmptySearchResult_setActions() {
let expectationActions = expectation(description: "actions")
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: [1]))
sut.group.notify(queue: .main) {
expectationActions.fulfill()
}
mockNetflixClient.searchCompletion?([], nil)
wait(for: [expectationActions], timeout: 3.0)
XCTAssertEqual(sut.actions.count, 0)
}

func test_getNew100_whenGiven1SearchResult_whenGenreIdsEmpty_setActions() {
let expectationActions = expectation(description: "actions")
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: nil))
sut.group.notify(queue: .main) {
expectationActions.fulfill()
}
mockNetflixClient.searchCompletion?([SearchResult.dummy], nil)
wait(for: [expectationActions], timeout: 3.0)
XCTAssertEqual(sut.actions.count, 1)
}

func test_configureOperations_whenOperationStarted_setActivityIndicator() {
sut.configureNetworkOperations()
XCTAssertTrue(sut.homeView.baseViewModel.isActivityIndicatorEnabled)
}

func test_configureOperations_whenOperationFinished_whenGiven2SearchResult_setNewReleases() {
let expectationOperations = expectation(description: "operations")
sut.group.enter()
sut.getNewReleases()
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: [1]))
sut.group.notify(queue: .main) {
expectationOperations.fulfill()
}
mockNetflixClient.newReleasesCompletion?([SearchResult.dummy, SearchResult.dummy], nil)
mockNetflixClient.searchCompletion?([SearchResult.dummy], nil)
wait(for: [expectationOperations], timeout: 3.0)
XCTAssertEqual(sut.newReleases.count, 2)
}

func test_configureOperations_whenOperationFinished_whenGiven3SearchResults_setActions() {
let expectationOperations = expectation(description: "operations")
sut.group.enter()
sut.getNewReleases()
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: [1]))
sut.group.notify(queue: .main) {
expectationOperations.fulfill()
}
mockNetflixClient.newReleasesCompletion?([SearchResult.dummy], nil)
mockNetflixClient.searchCompletion?([SearchResult.dummy, SearchResult.dummy, SearchResult.dummy], nil)
wait(for: [expectationOperations], timeout: 3.0)
XCTAssertEqual(sut.actions.count, 3)
}

func test_configureOperations_whenOperationFinished_setActivityIndicator() {
let expectationOperations = expectation(description: "operations")
sut.group.enter()
sut.getNewReleases()
sut.group.enter()
sut.getNew100(genre: Genre(name: "Test", ids: [1]))
sut.group.notify(queue: .main) {
expectationOperations.fulfill()
}
mockNetflixClient.newReleasesCompletion?([SearchResult.dummy], nil)
mockNetflixClient.searchCompletion?([SearchResult.dummy, SearchResult.dummy, SearchResult.dummy], nil)
wait(for: [expectationOperations], timeout: 3.0)
XCTAssertEqual(sut.homeView.baseViewModel.isActivityIndicatorEnabled, false)
}

}

0 comments on commit 900e27c

Please sign in to comment.