This repository was archived by the owner on Aug 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Adding unit tests for Share view model #97
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c0ef938
Adding unit tests for Share view model
etoledom 9972a6a
Merge branch 'trunk' into etoledom/share-view-model-tests
etoledom 2441bea
Update tests reflecting new business logic
etoledom 9a18c08
Adding missing cleanups
etoledom 1d4fb51
Merge branch 'trunk' into etoledom/share-view-model-tests
etoledom 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
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
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
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
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
138 changes: 138 additions & 0 deletions
138
GravatarAppTests/ShareScreenTests/ShareViewModelTests.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,138 @@ | ||
| import Foundation | ||
| import Gravatar | ||
| @testable import GravatarApp | ||
| import SnapshotTesting | ||
| import Testing | ||
|
|
||
| @MainActor | ||
| @Suite(.snapshots(record: .failed, diffTool: .ksdiff)) | ||
| struct ShareViewModelTests { | ||
| let networkMonitor = TestNetworkMonitor() | ||
| let urlSession = URLSessionMock() | ||
|
|
||
| @Test("Tets the vcard share link URL is generated with the correct data") | ||
| func shareVCardWithFullData() async throws { | ||
| let viewModel = createViewModel() | ||
|
|
||
| await viewModel.shareVCard() | ||
|
|
||
| #expect(viewModel.shareVCardURL != nil) | ||
|
|
||
| let vCard = try String(contentsOf: viewModel.shareVCardURL!, encoding: .utf8) | ||
| print("Stored data 1: \(String(describing: UserDefaults.testUserDefaults.value(forKey: "https://notreal.wordpress.com") as? Bool))") | ||
|
|
||
| #expect(vCard.contains("N:Appleseed;John;")) | ||
| #expect(vCard.contains("FN:\n")) // Always empty | ||
| #expect(vCard.contains("NICKNAME:John Appleseed")) | ||
| #expect(vCard.contains("ORG:A company")) | ||
| #expect(vCard.contains("TITLE:Engineer")) | ||
| #expect(vCard.contains("URL:https://gravatar.com/notreal")) | ||
| #expect(vCard.contains("ADR;CHARSET=UTF-8;TYPE=HOME:;;;Atlanta GA;;;")) | ||
| #expect(vCard.contains("URL;TYPE=\"WordPress\":https://notreal.wordpress.com")) | ||
| #expect(vCard.contains("NOTE:I'm a ")) | ||
| #expect(vCard.contains("EMAIL:notreal@example.com")) | ||
| #expect(vCard.contains("TEL:+1234567890")) | ||
| #expect(vCard.contains("PHOTO;ENCODING=b;TYPE=JPEG:/9j/4")) | ||
|
|
||
| UserDefaults.deleteTestData(named: #function) | ||
| } | ||
|
|
||
| @Test("Tets the vcard share link URL is generated with the correct data when no data is shared") | ||
| func shareVCardWithNoData() async throws { | ||
| let viewModel = createViewModel() | ||
| viewModel.share.email = false | ||
| viewModel.share.phone = false | ||
| viewModel.share.name = false | ||
| viewModel.share.location = false | ||
| viewModel.share.jobTitle = false | ||
| viewModel.share.company = false | ||
| viewModel.share.description = false | ||
| viewModel.share.profileURL = false | ||
| viewModel.share.set(verifiedAccount, to: false) | ||
|
|
||
| await viewModel.shareVCard() | ||
|
|
||
| #expect(viewModel.shareVCardURL != nil) | ||
|
|
||
| let vCard = try String(contentsOf: viewModel.shareVCardURL!, encoding: .utf8) | ||
|
|
||
| // Expected | ||
| #expect(vCard.contains("FN:\n")) // Always empty | ||
| #expect(vCard.contains("NICKNAME:John Appleseed")) | ||
| #expect(vCard.contains("PHOTO;ENCODING=b;TYPE=JPEG:/9j/4")) | ||
| // Not expected | ||
| #expect(!vCard.contains("N:Appleseed;John;")) | ||
| #expect(!vCard.contains("ORG:A company")) | ||
| #expect(!vCard.contains("TITLE:Engineer")) | ||
| #expect(!vCard.contains("URL:https://gravatar.com/notreal")) | ||
| #expect(!vCard.contains("ADR;CHARSET=UTF-8;TYPE=HOME:;;;Atlanta GA;;;")) | ||
| #expect(!vCard.contains("URL;TYPE=\"WordPress\":https://notreal.wordpress.com")) | ||
| #expect(!vCard.contains("NOTE:I'm a ")) | ||
| #expect(!vCard.contains("EMAIL:notreal@example.com")) | ||
| #expect(!vCard.contains("TEL:+1234567890")) | ||
|
|
||
| UserDefaults.deleteTestData(named: #function) | ||
| } | ||
|
|
||
| @Test("Test the qr code is generated correctly with all data") | ||
| func qrCodeGeneratedFullData() async throws { | ||
| let viewModel = createViewModel() | ||
|
|
||
| await viewModel.generateVCardQR() | ||
|
|
||
| #expect(viewModel.qrCodeImage != nil) | ||
| let image = viewModel.qrCodeImage!.resizable().frame(width: 100, height: 100) | ||
|
|
||
| assertSnapshots(of: image, as: [.image]) | ||
|
|
||
| UserDefaults.deleteTestData(named: #function) | ||
| } | ||
|
|
||
| @Test("Test the qr code is generated correctly with minimal data") | ||
| func qrCodeGeneratedMinimalData() async throws { | ||
| let viewModel = createViewModel() | ||
|
|
||
| viewModel.share.email = false | ||
| viewModel.share.phone = false | ||
| viewModel.share.name = false | ||
| viewModel.share.location = false | ||
| viewModel.share.jobTitle = false | ||
| viewModel.share.company = false | ||
| viewModel.share.description = false | ||
| viewModel.share.profileURL = false | ||
| viewModel.share.set(verifiedAccount, to: false) | ||
|
|
||
| await viewModel.generateVCardQR() | ||
|
|
||
| #expect(viewModel.qrCodeImage != nil) | ||
| let image = viewModel.qrCodeImage!.resizable().frame(width: 100, height: 100) | ||
|
|
||
| assertSnapshots(of: image, as: [.image]) | ||
|
|
||
| UserDefaults.deleteTestData(named: #function) | ||
| } | ||
| } | ||
|
|
||
| @MainActor | ||
| private func createViewModel(_ testUnitName: String = #function) -> ShareViewModel { | ||
| let viewModel = ShareViewModel( | ||
| userSession: UserSession( | ||
| profile: .full, | ||
| accessToken: "", | ||
| context: .testContext, | ||
| networkMonitor: TestNetworkMonitor(), | ||
| urlSession: URLSessionMock() | ||
| ), | ||
| urlSession: URLSessionMock(), | ||
| networkMonitor: TestNetworkMonitor(), | ||
| // These tests run in parallel, so we need different UserDefaults for each one of them. | ||
| userDefaults: .testUserDefaults(named: testUnitName) | ||
| ) | ||
| viewModel.storedUserEmail = "notreal@example.com" | ||
| viewModel.storedPhoneNumber = "+1234567890" | ||
| return viewModel | ||
| } | ||
|
|
||
| private var verifiedAccount: VerifiedAccount { | ||
| Profile.full.verifiedAccounts[0] | ||
| } |
Binary file added
BIN
+62.4 KB
...hareScreenTests/__Snapshots__/ShareViewModelTests/qrCodeGeneratedFullData.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+31 KB
...eScreenTests/__Snapshots__/ShareViewModelTests/qrCodeGeneratedMinimalData.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
This change was stubbornly appearing every time. Probably from #96