Skip to content

Commit

Permalink
ci: fix linting and test coverage (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeul authored Dec 14, 2023
1 parent 751c903 commit 3eeebea
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 59 deletions.
40 changes: 23 additions & 17 deletions Tests/PayPalMessagesTests/CloseButtonTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import XCTest

class CloseButtonTests: XCTestCase {

var closeButton: CloseButton!
var closeButton: CloseButton?
var onTapCalled = false

override func setUp() {
Expand All @@ -21,54 +21,60 @@ class CloseButtonTests: XCTestCase {
}

func testButtonInitialization() {
XCTAssertNotNil(closeButton)
XCTAssertEqual(closeButton.alpha, 1.0)
XCTAssertEqual(closeButton.accessibilityLabel, "Cancel")
XCTAssertEqual(closeButton.imageView?.contentMode, .scaleAspectFit)
guard let button = closeButton else { return }
XCTAssertNotNil(button)
XCTAssertEqual(button.alpha, 1.0)
XCTAssertEqual(button.accessibilityLabel, "Cancel")
XCTAssertEqual(button.imageView?.contentMode, .scaleAspectFit)
}

func testButtonHighlightAnimation() {
guard let button = closeButton else { return }
// Initially, the button should not be highlighted
XCTAssertFalse(closeButton.isHighlighted)
XCTAssertEqual(closeButton.alpha, 1.0)
XCTAssertFalse(button.isHighlighted)
XCTAssertEqual(button.alpha, 1.0)

// Simulate the button being highlighted
closeButton.isHighlighted = true
button.isHighlighted = true

// Use XCTAssertEqual with a tolerance parameter
XCTAssertEqual(closeButton.alpha, CloseButton.Constants.fadeOutAlpha, accuracy: 0.001)
XCTAssertEqual(button.alpha, CloseButton.Constants.fadeOutAlpha, accuracy: 0.001)

// Simulate the button being unhighlighted
closeButton.isHighlighted = false
button.isHighlighted = false

// Use XCTAssertEqual with a tolerance parameter
XCTAssertEqual(closeButton.alpha, 1.0, accuracy: 0.001)
XCTAssertEqual(button.alpha, 1.0, accuracy: 0.001)
}

func testIntrinsicContentSize() {
guard let button = closeButton else { return }
// Calculate the expected intrinsic content size based on your constants
let expectedWidth = CloseButton.Constants.buttonSize.width + (CloseButton.Constants.contenInset.left + CloseButton.Constants.contenInset.right)
let expectedHeight = CloseButton.Constants.buttonSize.height + (CloseButton.Constants.contenInset.top + CloseButton.Constants.contenInset.bottom)
let expectedWidth = CloseButton.Constants.buttonSize.width +
(CloseButton.Constants.contenInset.left + CloseButton.Constants.contenInset.right)
let expectedHeight = CloseButton.Constants.buttonSize.height +
(CloseButton.Constants.contenInset.top + CloseButton.Constants.contenInset.bottom)

// Get the actual intrinsic content size from the button
let intrinsicSize = closeButton.intrinsicContentSize
let intrinsicSize = button.intrinsicContentSize

// Assert that the actual intrinsic content size matches the expected size
XCTAssertEqual(intrinsicSize.width, expectedWidth)
XCTAssertEqual(intrinsicSize.height, expectedHeight)
}

func testInitWithoutClosure() {
guard let button = closeButton else { return }
// Initialize a CloseButton using the init() method
closeButton = CloseButton()

// Assert that the button is not nil
XCTAssertNotNil(closeButton)

// Assert that the button is properly configured
XCTAssertEqual(closeButton.alpha, 1.0)
XCTAssertEqual(closeButton.accessibilityLabel, "Cancel")
XCTAssertEqual(closeButton.imageView?.contentMode, .scaleAspectFit)
XCTAssertEqual(button.alpha, 1.0)
XCTAssertEqual(button.accessibilityLabel, "Cancel")
XCTAssertEqual(button.imageView?.contentMode, .scaleAspectFit)
}

func testTappedCloseButtonCallsOnTap() {
Expand Down
7 changes: 7 additions & 0 deletions Tests/PayPalMessagesTests/PayPalMessageLoggerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
logger.sender = mockSender
}

// swiftlint:disable:next function_body_length
func testMessageLoggerEvents() {
let messageLogger = Logger.createMessageLogger(
environment: .live,
Expand Down Expand Up @@ -58,6 +59,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
"type": "com.paypal.credit.upstream-presentment.v1",
"source": "urn:paypal:event-src:v1:ios:messages",
"datacontenttype": "application/json",
// swiftlint:disable:next line_length
"dataschema": "ppaas:events.credit.FinancingPresentmentAsyncAPISpecification/v1/schema/json/credit_upstream_presentment_event.json",
"time": "2023-11-01T11:12:05.791-0400",
"data": [
Expand Down Expand Up @@ -100,6 +102,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
assert(payload: data, equals: expectedPayload)
}

// swiftlint:disable:next function_body_length
func testModalLoggerEvents() {
let modalLogger = Logger.createModalLogger(
environment: .live,
Expand Down Expand Up @@ -137,6 +140,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
"type": "com.paypal.credit.upstream-presentment.v1",
"source": "urn:paypal:event-src:v1:ios:messages",
"datacontenttype": "application/json",
// swiftlint:disable:next line_length
"dataschema": "ppaas:events.credit.FinancingPresentmentAsyncAPISpecification/v1/schema/json/credit_upstream_presentment_event.json",
"time": "2023-11-01T11:12:05.791-0400",
"data": [
Expand Down Expand Up @@ -220,6 +224,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
"type": "com.paypal.credit.upstream-presentment.v1",
"source": "urn:paypal:event-src:v1:ios:messages",
"datacontenttype": "application/json",
// swiftlint:disable:next line_length
"dataschema": "ppaas:events.credit.FinancingPresentmentAsyncAPISpecification/v1/schema/json/credit_upstream_presentment_event.json",
"time": "2023-11-01T11:12:05.791-0400",
"data": [
Expand Down Expand Up @@ -302,6 +307,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
"type": "com.paypal.credit.upstream-presentment.v1",
"source": "urn:paypal:event-src:v1:ios:messages",
"datacontenttype": "application/json",
// swiftlint:disable:next line_length
"dataschema": "ppaas:events.credit.FinancingPresentmentAsyncAPISpecification/v1/schema/json/credit_upstream_presentment_event.json",
"time": "2023-11-01T11:12:05.791-0400",
"data": [
Expand Down Expand Up @@ -367,6 +373,7 @@ final class PayPalMessageLoggerTests: XCTestCase {
"type": "com.paypal.credit.upstream-presentment.v1",
"source": "urn:paypal:event-src:v1:ios:messages",
"datacontenttype": "application/json",
// swiftlint:disable:next line_length
"dataschema": "ppaas:events.credit.FinancingPresentmentAsyncAPISpecification/v1/schema/json/credit_upstream_presentment_event.json",
"time": "2023-11-01T11:12:05.791-0400",
"data": [
Expand Down
53 changes: 31 additions & 22 deletions Tests/PayPalMessagesTests/PayPalMessageModalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import XCTest
@testable import PayPalMessages

class PayPalMessageModalTests: XCTestCase {

let config = PayPalMessageModalConfig(
data: .init(
clientID: "Test123",
Expand All @@ -12,12 +13,12 @@ class PayPalMessageModalTests: XCTestCase {
)
)

var modalViewController: PayPalMessageModal!
var modalViewController: PayPalMessageModal?

override func setUp() {
super.setUp()
modalViewController = PayPalMessageModal(config: config)
modalViewController.loadViewIfNeeded()
modalViewController?.loadViewIfNeeded()
}

override func tearDown() {
Expand All @@ -38,45 +39,53 @@ class PayPalMessageModalTests: XCTestCase {
}

func testViewOnLoadingDelegate() {
let stateDelegateMock = PayPalMessageModalStateDelegateMock()
modalViewController.stateDelegate = stateDelegateMock
if let viewController = modalViewController {
let stateDelegateMock = PayPalMessageModalStateDelegateMock()
viewController.stateDelegate = stateDelegateMock

modalViewController.viewDidLoad()
viewController.viewDidLoad()

XCTAssertTrue(stateDelegateMock.onLoadingCalled)
XCTAssertTrue(stateDelegateMock.onLoadingCalled)
}
}

func testViewWillAppearCallsOnShowDelegate() {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
modalViewController.eventDelegate = eventDelegateMock
if let viewController = modalViewController {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
viewController.eventDelegate = eventDelegateMock

modalViewController.viewWillAppear(false)
viewController.viewWillAppear(false)

XCTAssertTrue(eventDelegateMock.onShowCalled)
XCTAssertTrue(eventDelegateMock.onShowCalled)
}
}

func testModalDismissalCallsOnCloseDelegate() {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
modalViewController.eventDelegate = eventDelegateMock
if let viewController = modalViewController {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
viewController.eventDelegate = eventDelegateMock

modalViewController.viewDidDisappear(false)
viewController.viewDidDisappear(false)

XCTAssertTrue(eventDelegateMock.onCloseCalled)
XCTAssertTrue(eventDelegateMock.onCloseCalled)
}
}

func testModalPresentationAndDismissal() {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
modalViewController.eventDelegate = eventDelegateMock
if let viewController = modalViewController {
let eventDelegateMock = PayPalMessageModalEventDelegateMock()
viewController.eventDelegate = eventDelegateMock

modalViewController.show()
modalViewController.viewWillAppear(false)
viewController.show()
viewController.viewWillAppear(false)

XCTAssertTrue(eventDelegateMock.onShowCalled)
XCTAssertTrue(eventDelegateMock.onShowCalled)

modalViewController.hide()
modalViewController.viewDidDisappear(false)
viewController.hide()
viewController.viewDidDisappear(false)

XCTAssertTrue(eventDelegateMock.onCloseCalled)
XCTAssertTrue(eventDelegateMock.onCloseCalled)
}
}

func testIntegrationInitializer() {
Expand Down
11 changes: 9 additions & 2 deletions Tests/PayPalMessagesTests/PayPalMessageModalViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import WebKit
import XCTest

// swiftlint:disable:next type_body_length
final class PayPalMessageModalViewModelTests: XCTestCase {

let navigation = WKNavigation()
Expand Down Expand Up @@ -113,7 +114,10 @@ final class PayPalMessageModalViewModelTests: XCTestCase {

// Check if the actualJSONString matches the desired pattern
let pattern = "^window\\.actions\\.updateProps\\(.+\\)$"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
XCTFail("Failed to create NSRegularExpression")
return
}
let matches = regex.matches(in: actualJSONString, options: [], range: NSRange(location: 0, length: actualJSONString.count))

XCTAssertTrue(!matches.isEmpty)
Expand Down Expand Up @@ -153,7 +157,10 @@ final class PayPalMessageModalViewModelTests: XCTestCase {

// Check if the actualJSONString matches the desired pattern
let pattern = "^window\\.actions\\.updateProps\\(.+\\)$"
let regex = try! NSRegularExpression(pattern: pattern, options: [])
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
XCTFail("Failed to create NSRegularExpression")
return
}
let matches = regex.matches(in: actualJSONString, options: [], range: NSRange(location: 0, length: actualJSONString.count))

XCTAssertTrue(!matches.isEmpty)
Expand Down
4 changes: 3 additions & 1 deletion Tests/PayPalMessagesTests/PayPalMessageResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class MessageResponseTests: XCTestCase {
}
}
}
""".data(using: .utf8)!
"""
// swiftlint:disable force_unwrapping
.data(using: .utf8)!

let decoder = JSONDecoder()
let messageResponse = try decoder.decode(MessageResponse.self, from: json)
Expand Down
35 changes: 19 additions & 16 deletions Tests/PayPalMessagesTests/UIViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import XCTest

class UIViewExtensionTests: XCTestCase {

var testView: UIView!
var testView: UIView?

override func setUp() {
super.setUp()
Expand All @@ -16,22 +16,25 @@ class UIViewExtensionTests: XCTestCase {
super.tearDown()
}

func testRotateIndefinitely() {
// Ensure that the view is not already rotating
XCTAssertNil(testView.layer.animation(forKey: "rotation_animation"))

// Apply the rotation animation using your extension method
testView.rotateIndefinitely()


// Check properties of the animation
if let rotationAnimation = testView.layer.animation(forKey: "rotation_animation") as? CABasicAnimation {
XCTAssertEqual(rotationAnimation.keyPath, "transform.rotation.z")
XCTAssertEqual(rotationAnimation.toValue as? Double, Double.pi * 2)
XCTAssertEqual(rotationAnimation.duration, 1)
XCTAssertEqual(rotationAnimation.isCumulative, true)
XCTAssertEqual(rotationAnimation.repeatCount, .infinity)
XCTAssertEqual(rotationAnimation.isRemovedOnCompletion, false)
func testRotateIndefinitely() {
if let view = testView {
// Ensure that the view is not already rotating
XCTAssertNil(view.layer.animation(forKey: "rotation_animation"))

// Apply the rotation animation using your extension method
view.rotateIndefinitely()


// Check properties of the animation
if let rotationAnimation = view.layer.animation(forKey: "rotation_animation") as? CABasicAnimation {
XCTAssertEqual(rotationAnimation.keyPath, "transform.rotation.z")
XCTAssertEqual(rotationAnimation.toValue as? Double, Double.pi * 2)
XCTAssertEqual(rotationAnimation.duration, 1)
XCTAssertEqual(rotationAnimation.isCumulative, true)
XCTAssertEqual(rotationAnimation.repeatCount, .infinity)
XCTAssertEqual(rotationAnimation.isRemovedOnCompletion, false)
}
}
}
}
6 changes: 5 additions & 1 deletion fastlane/Fastfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ platform :ios do
markdown_report: true,
minimum_coverage_percentage: 80.0
)
rescue => e
error = e # Store the exception
ensure
if options[:summary]
# Add coverage report to GitHub Actions summary page
sh "cat ./coverage/report.md > #{options[:summary]}"
end
raise error if error # Re-raise the exception if it exists
end
end

Expand All @@ -60,11 +63,12 @@ platform :ios do
reporter: "markdown",
output_file: "fastlane/swiftlint/report.md"
)
rescue # only need the summary if there are lint issues
rescue => e # only need the summary if there are lint issues
if options[:summary]
# Add SwiftLint report to GitHub Actions summary page
sh "cat ./swiftlint/report.md > #{options[:summary]}"
end
raise e # Re-raise the exception
end
end

Expand Down

0 comments on commit 3eeebea

Please sign in to comment.