//
//  SearchBarPluginTests.swift
//  Astro
//
//  Created by Crystal To on 2016-09-13.
//  Copyright © 2016 Mobify Research & Development Inc. All rights reserved.
//

import XCTest
@testable import Astro

class SearchBarPluginTests: XCTestCase {

    var messageBus: MessageBus!
    var pluginResolver: StubPluginResolver!

    var searchBarPlugin: SearchBarPlugin!
    var searchBar: UISearchBar!
    var respond: CapturedRpcMethodResult!

    override func setUp() {
        messageBus = MessageBus()
        pluginResolver = StubPluginResolver()

        searchBarPlugin = SearchBarPlugin(address: "SearchBarPlugin:0", messageBus: messageBus, pluginResolver: pluginResolver, options: nil)
        searchBar = searchBarPlugin.viewController.view.subviews.first as! UISearchBar
        respond = CapturedRpcMethodResult()
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testSetQuery() {
        let test = "coconut"
        searchBarPlugin.setQuery(test) { _ in }
        XCTAssertEqual(searchBar.text, test)
    }

    func testSetPlaceholderText() {
        let test = "wahoo"
        searchBarPlugin.setPlaceholderText(test) { _ in }
        XCTAssertEqual(searchBar.placeholder, test)
    }

    func testSetBackgroundColor() {
        searchBarPlugin.setBackgroundColor("#0000FF") { _ in }
        XCTAssertEqual(searchBar.barTintColor, UIColor.blue)
    }

    func testSubmit() {
        let receiver = TestReceiver(address: searchBarPlugin.eventAddress)
        messageBus.listen(on: searchBarPlugin.eventAddress, receiver: receiver)

        let test = "dragonfruit"
        searchBarPlugin.setQuery(test) { _ in }
        XCTAssertEqual(searchBar.text, test)
        searchBarPlugin.submit { _ in }
        XCTAssertNotNil(receiver.receivedMessage)
        XCTAssertEqual(searchBar.text, "")
    }

}
