Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
fix: respect updates to placeholder and callbacks
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
thislooksfun committed Nov 6, 2020
1 parent 37485cd commit e1ad0ac
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 6 deletions.
19 changes: 13 additions & 6 deletions Sources/SwiftlySearch/SwiftlySearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ struct SearchBar<ResultContent: View>: UIViewControllerRepresentable {
controller.searchController = context.coordinator.searchController
controller.hidesSearchBarWhenScrolling = hidesSearchBarWhenScrolling
controller.text = text

context.coordinator.update(placeholder: placeholder, cancelClicked: cancelClicked, searchClicked: searchClicked)

if let resultView = resultContent(text) {
(controller.searchController?.searchResultsController as? UIHostingController<ResultContent>)?.rootView = resultView
}
Expand All @@ -70,8 +73,8 @@ struct SearchBar<ResultContent: View>: UIViewControllerRepresentable {
class Coordinator: NSObject, UISearchResultsUpdating, UISearchBarDelegate {
@Binding
var text: String
let cancelClicked: () -> Void
let searchClicked: () -> Void
var cancelClicked: () -> Void
var searchClicked: () -> Void
let searchController: UISearchController

init(text: Binding<String>, placeholder: String?, hidesNavigationBarDuringPresentation: Bool, resultContent: (String) -> ResultContent?, cancelClicked: @escaping () -> Void, searchClicked: @escaping () -> Void) {
Expand All @@ -90,11 +93,15 @@ struct SearchBar<ResultContent: View>: UIViewControllerRepresentable {
searchController.obscuresBackgroundDuringPresentation = false

searchController.searchBar.delegate = self
if let placeholder = placeholder {
searchController.searchBar.placeholder = placeholder
}
searchController.searchBar.text = self.text
searchController.searchBar.placeholder = placeholder
}

self.searchController.searchBar.text = self.text
func update(placeholder: String?, cancelClicked: @escaping () -> Void, searchClicked: @escaping () -> Void) {
searchController.searchBar.placeholder = placeholder

self.cancelClicked = cancelClicked
self.searchClicked = searchClicked
}

// MARK: - UISearchResultsUpdating
Expand Down
8 changes: 8 additions & 0 deletions Tests/TestApp/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ struct ContentView: View {
NavigationLink(destination: BindingTest()) {
Text("Binding")
}

NavigationLink(destination: PlaceholderTest()) {
Text("Placeholder")
}

NavigationLink(destination: CallbackUpdateTest()) {
Text("Callback Update")
}
}
.navigationBarTitle("Tests")
}
Expand Down
42 changes: 42 additions & 0 deletions Tests/TestApp/TestViews/CallbackUpdateTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import SwiftUI

struct CallbackUpdateTest: View {
@State
var searchString = ""

@State
var doSearchTxt = "a"
@State
var doCancelTxt = "b"

@State
var doSearch = { }
@State
var doCancel = { }

var body: some View {
List {
Button(action: {
self.doSearch = { self.doSearchTxt = "1: \(Date().timeIntervalSince1970)" }
self.doCancel = { self.doCancelTxt = "2: \(Date().timeIntervalSince1970)" }
}) {
Text("Update callbacks")
.foregroundColor(.accentColor)
}
.accessibility(identifier: "callbacks button")

Text(doSearchTxt)
.accessibility(identifier: "do search text")
Text(doCancelTxt)
.accessibility(identifier: "do cancel text")
}
.navigationBarTitle("Callback Update Test")
.navigationBarSearch($searchString, cancelClicked: doCancel, searchClicked: doSearch)
}
}

struct CallbackUpdateTest_Previews: PreviewProvider {
static var previews: some View {
CallbackUpdateTest()
}
}
30 changes: 30 additions & 0 deletions Tests/TestApp/TestViews/PlaceholderTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import SwiftUI

struct PlaceholderTest: View {
@State
var searchString = ""
@State
var placeholder = ""

var body: some View {
List {
Button(action: {
self.placeholder = "\(Date().timeIntervalSince1970)"
}) {
Text("Update placeholder")
.foregroundColor(.accentColor)
}
.accessibility(identifier: "placeholder button")

Text("Expected placeholder: '\(placeholder)'")
}
.navigationBarTitle("Placeholder Test")
.navigationBarSearch($searchString, placeholder: placeholder)
}
}

struct PlaceholderTest_Previews: PreviewProvider {
static var previews: some View {
PlaceholderTest()
}
}
29 changes: 29 additions & 0 deletions Tests/UITests/CallbackUpdateTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import XCTest

class CallbackUpdateTests: UITest {
func testCallbackUpdate() throws {
// Setup test state.
selectTest("Callback Update")
app.children(matching: .any).firstMatch.slowSwipeDown(offset: 0.5, distance: 0.1)

// Ensure the searchbar exists.
let navBar = app.navigationBars.firstMatch
XCTAssertTrue(navBar.children(matching: .searchField).firstMatch.exists, "Searchbar exists")
let searchBar = navBar.children(matching: .searchField).firstMatch

// Test that changing the callbacks actually works.
let searchVal = app.staticTexts["do search text"].label
let cancelVal = app.staticTexts["do cancel text"].label

app.buttons["callbacks button"].tap()

searchBar.tap()
searchBar.typeText("hello")
app.keyboards.buttons["search"].tap()
XCTAssertNotEqual(searchVal, app.staticTexts["do search text"].label, "search callback updated")

navBar.buttons["Cancel"].tap()
XCTAssertNotEqual(cancelVal, app.staticTexts["do cancel text"].label, "cancel callback updated")

}
}
18 changes: 18 additions & 0 deletions Tests/UITests/PlaceholderTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import XCTest

class PlaceholderTests: UITest {
func testPlaceholder() throws {
// Setup test state.
selectTest("Placeholder")
app.children(matching: .any).firstMatch.slowSwipeDown(offset: 0.5, distance: 0.1)

// Ensure the searchbar exists.
XCTAssertTrue(app.navigationBars.firstMatch.children(matching: .searchField).firstMatch.exists, "Searchbar exists")
let searchBar = app.navigationBars.firstMatch.children(matching: .searchField).firstMatch

// Test that tapping the button changes the placeholder text.
let value = searchBar.placeholderValue
app.buttons["placeholder button"].tap()
XCTAssertNotEqual(value, searchBar.placeholderValue, "placeholder text updated")
}
}

0 comments on commit e1ad0ac

Please sign in to comment.