//
//  ResultViewControllerTests.swift
//  QuizAppTests
//
//  Created by Abdoulaye Diallo on 10/22/21.
//

import XCTest
@testable import QuizApp


class ResultViewControllerTests: XCTestCase {
    
    func test_viewDidLoad_rendersSummary(){
        XCTAssertEqual(makeSUT(summary: "A summary").headerLabel.text, "A summary")
    }
    
    func test_viewDidLoad_rendersAnswers(){
        XCTAssertEqual(makeSUT(answers: []).tableView.numberOfRows(inSection: 0), 0)
        XCTAssertEqual(makeSUT(answers:[makeAnswer()]).tableView.numberOfRows(inSection: 0), 1)
    }

    func test_viewDidLoad_withCorrectAnswers_configureCell(){

        let answer = makeAnswer(question: "Q1",answer: "A1")
        let sut = makeSUT(answers: [answer])

        let cell =   sut.tableView.cell(at: 0)   as? CorrectAnswerCell
        
        XCTAssertNotNil(cell)
        XCTAssertNotNil(cell?.answerLabel.text,  "A1")
        XCTAssertNotNil(cell?.questionLabel.text,  "Q1")
    }
    
    func test_viewDidLoad_withWrongAnswers_configureCell(){
        let answer = makeAnswer(question: "Q1",answer: "A1", wrongAnswer: "wrong")
        let sut = makeSUT(answers: [answer])
        
        let cell =   sut.tableView.cell(at: 0)   as? WrongAnswerCell
        
        XCTAssertNotNil(cell)
        XCTAssertNotNil(cell?.correctAnswerLabel.text,  "A1")
        XCTAssertNotNil(cell?.questionLabel.text,  "Q1")
        XCTAssertEqual(cell?.wrongAnswerLabel.text, "wrong")
    }
    
    //MARK: - Helper methods
    
    func makeSUT(summary: String = "", answers: [PresentableAnswer] = []) -> ResultsViewController {
        let sut = ResultsViewController(summary: summary, answers:answers)
        _ = sut.view
        return sut
    }
    

    func makeAnswer(question: String = "", answer: String = "",  wrongAnswer: String? = nil) -> PresentableAnswer {
        return PresentableAnswer(question: question, answer: answer, wrongAnswer: wrongAnswer)
    }
}
