-
Notifications
You must be signed in to change notification settings - Fork 135
Tests | CardScannerViewController #2059
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
AdyenCardScannerTests/CardScannerViewControllerTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| // | ||
| // Copyright (c) 2025 Adyen N.V. | ||
| // | ||
| // This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| // | ||
|
|
||
| @testable import AdyenCardScanner | ||
| import XCTest | ||
|
|
||
| final class CardScannerViewControllerTests: XCTestCase { | ||
|
|
||
| var viewModel: CardScannerViewModelMock! | ||
| var sut: CardScannerViewController! | ||
|
|
||
| override func tearDownWithError() throws { | ||
| viewModel = nil | ||
| sut = nil | ||
| try super.tearDownWithError() | ||
| } | ||
|
|
||
| func testViewDidLoadShouldCallViewModelConfigureSession() throws { | ||
| // Given | ||
| viewModel = CardScannerViewModelMock() | ||
| sut = CardScannerViewController(viewModel: viewModel) | ||
|
|
||
| // When | ||
| sut.loadViewIfNeeded() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(viewModel.configureSessionCallsCount, 1) | ||
| } | ||
|
|
||
| func testViewWillAppearShouldStartCaptureSession() throws { | ||
| // Given | ||
| viewModel = CardScannerViewModelMock() | ||
| sut = CardScannerViewController(viewModel: viewModel) | ||
|
|
||
| // When | ||
| sut.viewWillAppear(true) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(viewModel.startCaptureSessionCallsCount, 1) | ||
| } | ||
|
|
||
| func testViewWillAppearShouldStopCaptureSession() throws { | ||
| // Given | ||
| viewModel = CardScannerViewModelMock() | ||
| sut = CardScannerViewController(viewModel: viewModel) | ||
|
|
||
| // When | ||
| sut.viewWillDisappear(true) | ||
|
|
||
| // Then | ||
| XCTAssertEqual(viewModel.stopCaptureSessionCallsCount, 1) | ||
| } | ||
|
|
||
| func testViewDidLayoutSubviewsShouldUpdateVideoOrientation() throws { | ||
| // Given | ||
| viewModel = CardScannerViewModelMock() | ||
| sut = CardScannerViewController(viewModel: viewModel) | ||
|
|
||
| // When | ||
| sut.viewDidLayoutSubviews() | ||
|
|
||
| // Then | ||
| XCTAssertEqual(viewModel.updateVideoOrientationCallsCount, 1) | ||
| } | ||
|
|
||
| func testVideoPreviewLayetIsSetOnInit() throws { | ||
|
nauaros marked this conversation as resolved.
|
||
| // Given | ||
| let expectedVideoPreviewLayer = CALayer() | ||
| viewModel = CardScannerViewModelMock() | ||
| viewModel.videoPreviewLayer = expectedVideoPreviewLayer | ||
| sut = CardScannerViewController(viewModel: viewModel) | ||
|
|
||
| // When | ||
| sut.loadViewIfNeeded() | ||
|
|
||
| // Then | ||
| let layer = try XCTUnwrap(sut.view.layer.sublayers?.first) | ||
| XCTAssertTrue(layer === expectedVideoPreviewLayer) | ||
| } | ||
| } | ||
91 changes: 91 additions & 0 deletions
91
AdyenCardScannerTests/Mocks/CardScannerViewModelMock.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // | ||
| // Copyright (c) 2025 Adyen N.V. | ||
| // | ||
| // This file is open source and available under the MIT license. See the LICENSE file for more info. | ||
| // | ||
|
|
||
| @testable import AdyenCardScanner | ||
| import Foundation | ||
| import QuartzCore | ||
|
|
||
| class CardScannerViewModelMock: CardScannerViewModelProtocol { | ||
| // MARK: - videoPreviewLayer | ||
|
|
||
| var videoPreviewLayer: CALayer = .init() | ||
|
|
||
| // MARK: - configureSession | ||
|
|
||
| var configureSessionCallsCount = 0 | ||
| var configureSessionCalled: Bool { | ||
| configureSessionCallsCount > 0 | ||
| } | ||
|
|
||
| var configureSessionClosure: (() -> Void)? | ||
|
|
||
| func configureSession() { | ||
| configureSessionCallsCount += 1 | ||
| configureSessionClosure?() | ||
| } | ||
|
|
||
| // MARK: - startCaptureSession | ||
|
|
||
| var startCaptureSessionCallsCount = 0 | ||
| var startCaptureSessionCalled: Bool { | ||
| startCaptureSessionCallsCount > 0 | ||
| } | ||
|
|
||
| var startCaptureSessionClosure: (() -> Void)? | ||
|
|
||
| func startCaptureSession() { | ||
| startCaptureSessionCallsCount += 1 | ||
| startCaptureSessionClosure?() | ||
| } | ||
|
|
||
| // MARK: - stopCaptureSession | ||
|
|
||
| var stopCaptureSessionCallsCount = 0 | ||
| var stopCaptureSessionCalled: Bool { | ||
| stopCaptureSessionCallsCount > 0 | ||
| } | ||
|
|
||
| var stopCaptureSessionClosure: (() -> Void)? | ||
|
|
||
| func stopCaptureSession() { | ||
| stopCaptureSessionCallsCount += 1 | ||
| stopCaptureSessionClosure?() | ||
| } | ||
|
|
||
| // MARK: - updateVideoOrientation | ||
|
|
||
| var updateVideoOrientationCallsCount = 0 | ||
| var updateVideoOrientationCalled: Bool { | ||
| updateVideoOrientationCallsCount > 0 | ||
| } | ||
|
|
||
| var updateVideoOrientationClosure: (() -> Void)? | ||
|
|
||
| func updateVideoOrientation() { | ||
| updateVideoOrientationCallsCount += 1 | ||
| updateVideoOrientationClosure?() | ||
| } | ||
|
|
||
| // MARK: - update | ||
|
|
||
| var updateCallsCount = 0 | ||
| var updateCalled: Bool { | ||
| updateCallsCount > 0 | ||
| } | ||
|
|
||
| var updateReceivedPreviewLayerFrame: CGRect? | ||
| var updateReceivedROIInPreviewLayer: CGRect? | ||
| var updateReceivedInvocations: [(CGRect, CGRect)] = [] | ||
| var updateClosure: ((CGRect, CGRect) -> Void)? | ||
|
|
||
| func update(previewLayerFrame: CGRect, roiInPreviewLayer: CGRect) { | ||
| updateCallsCount += 1 | ||
| updateReceivedPreviewLayerFrame = previewLayerFrame | ||
| updateReceivedROIInPreviewLayer = roiInPreviewLayer | ||
| updateReceivedInvocations.append((previewLayerFrame, roiInPreviewLayer)) | ||
| updateClosure?(previewLayerFrame, roiInPreviewLayer) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disappear