Skip to content

Commit

Permalink
Add button action tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tonisevener committed Jan 8, 2024
1 parent c6a3cad commit 52964a1
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@ extension WKSourceEditorFormatterLink {
}

func editLink(in textView: UITextView, newPageTitle: String, newPageLabel: String?, preselectedTextRange: UITextRange) {

expandSelectedRangeUpToNearestFormattingStrings(startingFormattingString: "[[", endingFormattingString: "]]", in: textView)

if let newPageLabel, !newPageLabel.isEmpty {
textView.replace(preselectedTextRange, withText: "\(newPageTitle)|\(newPageLabel)")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,123 @@ final class WKSourceEditorFormatterButtonActionTests: XCTestCase {
mediator.strikethroughFormatter?.toggleStrikethroughFormatting(action: .remove, in: mediator.textView)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two Three Four")
}

func testLinkWizardParametersEdit() throws {
let text = "Testing [[Cat]] Testing"
mediator.textView.attributedText = NSAttributedString(string: text)
mediator.textView.selectedRange = NSRange(location: 10, length:3)
let wizardParameters = mediator.linkFormatter?.linkWizardParameters(action: .edit, in: mediator.textView)
XCTAssertEqual(wizardParameters?.editPageTitle, "Cat")
XCTAssertNil(wizardParameters?.editPageLabel)
XCTAssertEqual(wizardParameters?.preselectedTextRange, mediator.textView.selectedTextRange)
}

func testLinkWizardParametersEditWithLabel() throws {
let text = "Testing [[Cat|Kitty]] Testing"
mediator.textView.attributedText = NSAttributedString(string: text)
mediator.textView.selectedRange = NSRange(location: 10, length:3)
let wizardParameters = mediator.linkFormatter?.linkWizardParameters(action: .edit, in: mediator.textView)
XCTAssertEqual(wizardParameters?.editPageTitle, "Cat")
XCTAssertEqual(wizardParameters?.editPageLabel, "Kitty")
XCTAssertEqual(wizardParameters?.preselectedTextRange, mediator.textView.selectedTextRange)
}

func testLinkWizardParametersInsert() throws {
let text = "Testing Cat Testing"
mediator.textView.attributedText = NSAttributedString(string: text)
mediator.textView.selectedRange = NSRange(location: 8, length:3)
let wizardParameters = mediator.linkFormatter?.linkWizardParameters(action: .insert, in: mediator.textView)
XCTAssertEqual(wizardParameters?.insertSearchTerm, "Cat")
XCTAssertEqual(wizardParameters?.preselectedTextRange, mediator.textView.selectedTextRange)
}

func testLinkInsert() {
let text = "One Two Three Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)

guard let startPos = textView.position(from: textView.beginningOfDocument, offset: 4),
let endPos = textView.position(from: textView.beginningOfDocument, offset: 7),
let preselectedTextRange = textView.textRange(from: startPos, to: endPos) else {
XCTFail("Failure creating preselectedTextRange")
return
}

mediator.linkFormatter?.insertLink(in: textView, pageTitle: "Two", preselectedTextRange: preselectedTextRange)
XCTAssertEqual(mediator.textView.attributedText.string, "One [[Two]] Three Four")
}

func testLinkEdit() {
let text = "One Two [[Three]] Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)

guard let startPos = textView.position(from: textView.beginningOfDocument, offset: 10),
let endPos = textView.position(from: textView.beginningOfDocument, offset: 15),
let preselectedTextRange = textView.textRange(from: startPos, to: endPos) else {
XCTFail("Failure creating preselectedTextRange")
return
}

mediator.linkFormatter?.editLink(in: textView, newPageTitle: "Five", newPageLabel: nil, preselectedTextRange: preselectedTextRange)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two [[Five]] Four")
}

func testLinkEditWithLabel() {
let text = "One Two [[Three]] Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)

guard let startPos = textView.position(from: textView.beginningOfDocument, offset: 10),
let endPos = textView.position(from: textView.beginningOfDocument, offset: 15),
let preselectedTextRange = textView.textRange(from: startPos, to: endPos) else {
XCTFail("Failure creating preselectedTextRange")
return
}

mediator.linkFormatter?.editLink(in: textView, newPageTitle: "Five", newPageLabel: "fiver", preselectedTextRange: preselectedTextRange)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two [[Five|fiver]] Four")
}

func testLinkRemove() {
let text = "One Two [[Three]] Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)

guard let startPos = textView.position(from: textView.beginningOfDocument, offset: 10),
let endPos = textView.position(from: textView.beginningOfDocument, offset: 15),
let preselectedTextRange = textView.textRange(from: startPos, to: endPos) else {
XCTFail("Failure creating preselectedTextRange")
return
}

mediator.linkFormatter?.removeLink(in: textView, preselectedTextRange: preselectedTextRange)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two Three Four")
}

func testLinkRemoveWithLabel() {
let text = "One Two [[Three|3]] Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)

guard let startPos = textView.position(from: textView.beginningOfDocument, offset: 10),
let endPos = textView.position(from: textView.beginningOfDocument, offset: 17),
let preselectedTextRange = textView.textRange(from: startPos, to: endPos) else {
XCTFail("Failure creating preselectedTextRange")
return
}

mediator.linkFormatter?.removeLink(in: textView, preselectedTextRange: preselectedTextRange)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two Three|3 Four")
}

func testLinkInsertImage() {
let text = "One Two Three Four"
let textView = mediator.textView
textView.attributedText = NSAttributedString(string: text)
mediator.textView.selectedRange = NSRange(location: 8, length:0)

mediator.linkFormatter?.insertImage(wikitext: "[[File:Cat November 2010-1a.jpg | thumb | 220x124px | right]]", in: textView)
XCTAssertEqual(mediator.textView.attributedText.string, "One Two [[File:Cat November 2010-1a.jpg | thumb | 220x124px | right]]Three Four")
}
}
9 changes: 9 additions & 0 deletions Components/Tests/ComponentsTests/WKSourceEditorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,22 @@ final class WKSourceEditorTests: XCTestCase {
}

extension WKSourceEditorTests: WKSourceEditorViewControllerDelegate {

func sourceEditorViewControllerDidRemoveFindInputAccessoryView(sourceEditorViewController: Components.WKSourceEditorViewController) {

}

func sourceEditorViewControllerDidTapFind(sourceEditorViewController: Components.WKSourceEditorViewController) {

}

func sourceEditorViewControllerDidTapLink(parameters: Components.WKSourceEditorFormatterLinkWizardParameters) {

}

func sourceEditorViewControllerDidTapImage() {

}
}

extension WKSourceEditorLocalizedStrings {
Expand Down

0 comments on commit 52964a1

Please sign in to comment.