Skip to content

Commit

Permalink
Validate create polls button
Browse files Browse the repository at this point in the history
  • Loading branch information
laevandus committed May 28, 2024
1 parent 1ec2b2a commit e3328af
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ struct CreatePollView: View {
Image(systemName: "paperplane.fill")
.foregroundColor(colors.tintColor)
}
.disabled(!viewModel.canCreatePoll)
}
}
.navigationBarTitleDisplayMode(.inline)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,16 @@ class CreatePollViewModel: ObservableObject {
}
}

var canCreatePoll: Bool {
guard !question.trimmed.isEmpty else { return false }
guard optionsErrorIndices.isEmpty else { return false }
guard options.contains(where: { !$0.trimmed.isEmpty }) else { return false }
return true
}

var canShowDiscardConfirmation: Bool {
guard question.isEmpty else { return true }
return options.contains(where: { !$0.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty })
guard question.trimmed.isEmpty else { return true }
return options.contains(where: { !$0.trimmed.isEmpty })
}

func showsOptionError(for index: Int) -> Bool {
Expand Down
8 changes: 8 additions & 0 deletions Sources/StreamChatSwiftUI/Utils/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ extension String {
var containsOnlyEmoji: Bool { !isEmpty && !contains { !$0.isEmoji } }
}

// MARK: -

extension String {
/// computes levenshtein distance with another string (0
public func levenshtein(_ other: String) -> Int {
Expand Down Expand Up @@ -126,3 +128,9 @@ extension String {
return result
}
}

extension String {
var trimmed: Self {
trimmingCharacters(in: .whitespacesAndNewlines)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import XCTest

final class CreatePollViewModel_Tests: StreamChatTestCase {

// MARK: - Can Show Discard Confirmation

func test_canShowDiscardConfirmation_whenEmpty() {
// Given
// When
Expand Down Expand Up @@ -45,4 +48,38 @@ final class CreatePollViewModel_Tests: StreamChatTestCase {
// Then
XCTAssertEqual(viewModel.canShowDiscardConfirmation, true)
}

// MARK: - Can Create Poll

func test_canCreatePoll_whenRequiredInformationAdded() {
// Given
// When
let viewModel = CreatePollViewModel(chatController: chatClient.channelController(for: .unique))
viewModel.question = " A "
viewModel.options = ["O "]

// Then
XCTAssertEqual(viewModel.canCreatePoll, true)
}

func test_canCreatePoll_whenEmptyOrChangedToggles() {
// Given
// When
let viewModel = CreatePollViewModel(chatController: chatClient.channelController(for: .unique))
viewModel.allowComments.toggle()

// Then
XCTAssertEqual(viewModel.canCreatePoll, false)
}

func test_canCreatePoll_whenInsertingInformation() {
let viewModel = CreatePollViewModel(chatController: chatClient.channelController(for: .unique))
XCTAssertEqual(viewModel.canCreatePoll, false)
viewModel.question = "A"
XCTAssertEqual(viewModel.canCreatePoll, false)
viewModel.options = ["A", "a"] // duplicate error
XCTAssertEqual(viewModel.canCreatePoll, false)
viewModel.options = ["A", "aa"]
XCTAssertEqual(viewModel.canCreatePoll, true)
}
}

0 comments on commit e3328af

Please sign in to comment.