From ec05b2c2bce6b899b76ce3db8ff779b3a2bf0b69 Mon Sep 17 00:00:00 2001 From: Erik Doernenburg Date: Thu, 18 Apr 2024 23:12:54 +0200 Subject: [PATCH] Added test for repo timed fetch. Made some small refactorings. --- .../Source/Miscellaneous/DebouncedText.swift | 18 ++++----- .../AddGitHubPipelineSheet.swift | 8 ++-- CCMenuUITests/GitHubTests.swift | 38 +++++++++++++++++-- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/CCMenu/Source/Miscellaneous/DebouncedText.swift b/CCMenu/Source/Miscellaneous/DebouncedText.swift index 6453cba..ffa14f4 100644 --- a/CCMenu/Source/Miscellaneous/DebouncedText.swift +++ b/CCMenu/Source/Miscellaneous/DebouncedText.swift @@ -11,21 +11,21 @@ class DebouncedText : ObservableObject { @Published var input = "" @Published var text = "" - private var subscriptions = Set() + private var subscriber = Set() 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 } } } diff --git a/CCMenu/Source/Pipeline Window/GitHub Sheets/AddGitHubPipelineSheet.swift b/CCMenu/Source/Pipeline Window/GitHub Sheets/AddGitHubPipelineSheet.swift index a2a6b7c..0ae9aac 100644 --- a/CCMenu/Source/Pipeline Window/GitHub Sheets/AddGitHubPipelineSheet.swift +++ b/CCMenu/Source/Pipeline Window/GitHub Sheets/AddGitHubPipelineSheet.swift @@ -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) { diff --git a/CCMenuUITests/GitHubTests.swift b/CCMenuUITests/GitHubTests.swift index 896ddbf..c52f899 100644 --- a/CCMenuUITests/GitHubTests.swift +++ b/CCMenuUITests/GitHubTests.swift @@ -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"] @@ -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"] @@ -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"] @@ -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) + } }