Skip to content

Commit

Permalink
Added test for repo timed fetch. Made some small refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikdoe committed Apr 18, 2024
1 parent a7c23d8 commit ec05b2c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
18 changes: 9 additions & 9 deletions CCMenu/Source/Miscellaneous/DebouncedText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ class DebouncedText : ObservableObject {
@Published var input = ""
@Published var text = ""

private var subscriptions = Set<AnyCancellable>()
private var subscriber = Set<AnyCancellable>()

init() {
$input
.debounce(for: .seconds(1), scheduler: DispatchQueue.main)
.sink(receiveValue: { [weak self] t in
self?.takeText(t)
.debounce(for: .milliseconds(750), scheduler: DispatchQueue.main)
.sink(receiveValue: { [weak self] val in
self?.takeInput(val)
})
.store(in: &subscriptions)
.store(in: &subscriber)
}

func takeText(_ val: String? = nil) {
let newVal = val ?? input
if newVal != text {
text = newVal
func takeInput(_ val: String? = nil) {
let val = val ?? input
if val != text {
text = val
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ struct AddGitHubPipelineSheet: View {
.accessibilityIdentifier("Owner field")
// TODO: figure out why .prefersDefaultFocus(in:) doesn't work
.autocorrectionDisabled(true)
.onReceive(owner.$text) { val in
if val.isEmpty {
.onReceive(owner.$text) { t in
if t.isEmpty {
repositoryList.clearRepositories()
} else {
Task {
await repositoryList.updateRepositories(owner: val, token: authenticator.token)
await repositoryList.updateRepositories(owner: t, token: authenticator.token)
}
}
}
.onSubmit {
owner.takeText()
owner.takeInput()
}

Picker("Repository:", selection: $repositoryList.selected) {
Expand Down
38 changes: 34 additions & 4 deletions CCMenuUITests/GitHubTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class GitHubTests: XCTestCase {

// Enter owner
sheet.textFields["Owner field"].click()
sheet.typeText("erikdoe\n")
sheet.typeText("erikdoe" + "\n")

// Make sure that the repositories and workflows are loaded and the default display name is set
let repositoryPicker = sheet.popUpButtons["Repository picker"]
Expand Down Expand Up @@ -155,7 +155,7 @@ class GitHubTests: XCTestCase {

// Enter owner
sheet.textFields["Owner field"].click()
sheet.typeText("erikdoe\n")
sheet.typeText("erikdoe" + "\n")

// Make sure that the repositories and branches are loaded
let repositoryPicker = sheet.popUpButtons["Repository picker"]
Expand Down Expand Up @@ -199,7 +199,7 @@ class GitHubTests: XCTestCase {
// Enter owner and wait for the repo list to load
let ownerField = sheet.textFields["Owner field"]
ownerField.click()
sheet.typeText("erikdoe\n")
sheet.typeText("erikdoe" + "\n")

// Make sure that the repositories are loaded and sorted
let repositoryPicker = sheet.popUpButtons["Repository picker"]
Expand All @@ -216,8 +216,38 @@ class GitHubTests: XCTestCase {
XCTAssertTrue(repositoryPicker.menuItems["jekyll-site-test"].exists)
}

func testDoesntDoubleFetchWhenPressingEnter() throws {
func testDoesntDoubleFetchRepositories() throws {
var fetchCount = 0
webapp.router.get("/users/erikdoe/repos") { _ in
fetchCount += 1
return try TestHelper.contentsOfFile("GitHubReposByUserResponse.json")
}

let app = TestHelper.launchApp(pipelines: "EmptyPipelines.json")
let window = app.windows["Pipelines"]
let sheet = window.sheets.firstMatch

// Navigate to add workflow sheet
window.toolbars.popUpButtons["Add pipeline menu"].click()
window.toolbars.menuItems["Add GitHub Actions workflow..."].click()

// Enter owner and wait for the repo list to load
let ownerField = sheet.textFields["Owner field"]
ownerField.click()
sheet.typeText("erikdoe") // Note: not pressing return here

// Make sure that the repositories are loaded and sorted
let repositoryPicker = sheet.popUpButtons["Repository picker"]
expectation(for: NSPredicate(format: "value == 'ccmenu'"), evaluatedWith: repositoryPicker)
waitForExpectations(timeout: 3)

// Now press return and wait for a little while
sheet.typeText("\n")
Thread.sleep(forTimeInterval: 1)

// Assert that no further fetch occured
XCTAssertEqual(1, fetchCount)

}

}

0 comments on commit ec05b2c

Please sign in to comment.