Skip to content

Commit 54e06ad

Browse files
committed
Add unit tests for GeneralAppSettingsStorage
1 parent 3b102eb commit 54e06ad

File tree

5 files changed

+126
-7
lines changed

5 files changed

+126
-7
lines changed

Storage/Storage.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@
218218
DEC51AE0275B41BE009F3DF4 /* WooCommerce.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = DEC51AA4275B41BE009F3DF4 /* WooCommerce.xcdatamodeld */; };
219219
DEFD6D422641B70400E51E0D /* SitePlugin+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFD6D412641B70400E51E0D /* SitePlugin+CoreDataProperties.swift */; };
220220
DEFD6D432641B70400E51E0D /* SitePlugin+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = DEFD6D402641B70400E51E0D /* SitePlugin+CoreDataClass.swift */; };
221+
E16D3741284F1CDA005676BC /* GeneralAppSettingsStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16D3740284F1CDA005676BC /* GeneralAppSettingsStorageTests.swift */; };
222+
E16D3743284F1E76005676BC /* MockInMemoryStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E16D3742284F1E76005676BC /* MockInMemoryStorage.swift */; };
221223
E1E632C02846245D00878082 /* GeneralAppSettingsStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = E1E632BF2846245D00878082 /* GeneralAppSettingsStorage.swift */; };
222224
FEDD70AD26A5DBDD00194C3A /* EligibilityErrorInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = FEDD70AC26A5DBDD00194C3A /* EligibilityErrorInfo.swift */; };
223225
/* End PBXBuildFile section */
@@ -531,6 +533,8 @@
531533
DEFD6D402641B70400E51E0D /* SitePlugin+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SitePlugin+CoreDataClass.swift"; sourceTree = "<group>"; };
532534
DEFD6D412641B70400E51E0D /* SitePlugin+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SitePlugin+CoreDataProperties.swift"; sourceTree = "<group>"; };
533535
DF3D3B298350F68191CD1DAD /* Pods_Storage.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Storage.framework; sourceTree = BUILT_PRODUCTS_DIR; };
536+
E16D3740284F1CDA005676BC /* GeneralAppSettingsStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralAppSettingsStorageTests.swift; sourceTree = "<group>"; };
537+
E16D3742284F1E76005676BC /* MockInMemoryStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockInMemoryStorage.swift; sourceTree = "<group>"; };
534538
E1E632BF2846245D00878082 /* GeneralAppSettingsStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GeneralAppSettingsStorage.swift; sourceTree = "<group>"; };
535539
F0439F2ADB3B211DF5C44D83 /* Pods-StorageTests.release-alpha.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-StorageTests.release-alpha.xcconfig"; path = "../Pods/Target Support Files/Pods-StorageTests/Pods-StorageTests.release-alpha.xcconfig"; sourceTree = "<group>"; };
536540
FEDD70AC26A5DBDD00194C3A /* EligibilityErrorInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EligibilityErrorInfo.swift; sourceTree = "<group>"; };
@@ -567,6 +571,7 @@
567571
574B774D24AA883D0042116F /* MockFileManager.swift */,
568572
572C099525475208005372E1 /* SpyFileManager.swift */,
569573
5772842625BF5BFB0092FB2C /* SpyPersistentStoreCoordinator.swift */,
574+
E16D3742284F1E76005676BC /* MockInMemoryStorage.swift */,
570575
);
571576
path = Mocks;
572577
sourceTree = "<group>";
@@ -802,6 +807,7 @@
802807
2619F71825AF95020006DAFF /* StorageTypeExtensionsTests.swift */,
803808
2685C11C263DEF0B00D9EE97 /* StorageTypeDeletionsTests.swift */,
804809
2619F78525B5D29B0006DAFF /* TypedPredicateTests.swift */,
810+
E16D3740284F1CDA005676BC /* GeneralAppSettingsStorageTests.swift */,
805811
);
806812
path = Tools;
807813
sourceTree = "<group>";
@@ -1362,6 +1368,7 @@
13621368
B59E11E020A9F5E6004121A4 /* Constants.swift in Sources */,
13631369
B54CA5C320A4BF6900F38CD1 /* NSManagedObjectContextStorageTests.swift in Sources */,
13641370
02A098272480D160002F8C7A /* MockCrashLogger.swift in Sources */,
1371+
E16D3743284F1E76005676BC /* MockInMemoryStorage.swift in Sources */,
13651372
B54CA5C720A4BFDC00F38CD1 /* DummyStack.swift in Sources */,
13661373
574B774E24AA883D0042116F /* MockFileManager.swift in Sources */,
13671374
B54CA5C220A4BF6900F38CD1 /* NSManagedObjectStorageTests.swift in Sources */,
@@ -1374,6 +1381,7 @@
13741381
57A29FB825226BDC004DEE01 /* MigrationTests.swift in Sources */,
13751382
2685C11D263DEF0C00D9EE97 /* StorageTypeDeletionsTests.swift in Sources */,
13761383
2619F78625B5D29B0006DAFF /* TypedPredicateTests.swift in Sources */,
1384+
E16D3741284F1CDA005676BC /* GeneralAppSettingsStorageTests.swift in Sources */,
13771385
5772842725BF5BFB0092FB2C /* SpyPersistentStoreCoordinator.swift in Sources */,
13781386
5772842C25BF62A90092FB2C /* Assertions.swift in Sources */,
13791387
26EA01D524EC44B300176A57 /* GeneralAppSettingsTests.swift in Sources */,

Storage/Storage/Model/GeneralAppSettings.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public struct GeneralAppSettings: Codable, Equatable, GeneratedCopiable {
6262
self.lastJetpackBenefitsBannerDismissedTime = lastJetpackBenefitsBannerDismissedTime
6363
}
6464

65+
public static var `default`: Self {
66+
.init(installationDate: nil,
67+
feedbacks: [:],
68+
isViewAddOnsSwitchEnabled: false,
69+
isProductSKUInputScannerSwitchEnabled: false,
70+
isCouponManagementSwitchEnabled: false,
71+
knownCardReaders: [],
72+
lastEligibilityErrorInfo: nil)
73+
}
74+
6575
/// Returns the status of a given feedback type. If the feedback is not stored in the feedback array. it is assumed that it has a pending status.
6676
///
6777
public func feedbackStatus(of type: FeedbackType) -> FeedbackSettings.Status {

Storage/Storage/Tools/GeneralAppSettingsStorage.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,7 @@ private extension GeneralAppSettingsStorage {
7474
/// Load the `GeneralAppSettings` from file or create an empty one if it doesn't exist.
7575
func loadOrCreateGeneralAppSettings() -> GeneralAppSettings {
7676
guard let settings: GeneralAppSettings = try? fileStorage.data(for: Constants.generalAppSettingsFileURL) else {
77-
return GeneralAppSettings(installationDate: nil,
78-
feedbacks: [:],
79-
isViewAddOnsSwitchEnabled: false,
80-
isProductSKUInputScannerSwitchEnabled: false,
81-
isCouponManagementSwitchEnabled: false,
82-
knownCardReaders: [],
83-
lastEligibilityErrorInfo: nil)
77+
return GeneralAppSettings.default
8478
}
8579

8680
return settings
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import Storage
3+
4+
/// Mock implementation of the FileStorage protocol.
5+
/// It reads and writes the data from and to an object in memory.
6+
///
7+
final class MockInMemoryStorage: FileStorage {
8+
/// A boolean value to test if a write to disk is requested
9+
///
10+
var dataWriteIsHit: Bool = false
11+
12+
/// A boolean value to test if a file deletion is requested
13+
///
14+
var deleteIsHit: Bool = false
15+
16+
private(set) var data: [URL: Codable] = [:]
17+
18+
func data<T>(for fileURL: URL) throws -> T where T: Decodable {
19+
guard let data = data[fileURL] as? T else {
20+
throw Error.readFailed
21+
}
22+
return data
23+
}
24+
25+
func write<T>(_ data: T, to fileURL: URL) throws where T: Encodable {
26+
self.data[fileURL] = data as? Codable
27+
dataWriteIsHit = true
28+
}
29+
30+
func deleteFile(at fileURL: URL) throws {
31+
data.removeValue(forKey: fileURL)
32+
deleteIsHit = true
33+
}
34+
}
35+
36+
extension MockInMemoryStorage {
37+
enum Error: Swift.Error {
38+
case readFailed
39+
}
40+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import XCTest
2+
import Storage
3+
4+
class GeneralAppSettingsStorageTests: XCTestCase {
5+
private var fileStorage: MockInMemoryStorage!
6+
private var storage: GeneralAppSettingsStorage!
7+
8+
override func setUp() {
9+
fileStorage = MockInMemoryStorage()
10+
storage = GeneralAppSettingsStorage(fileStorage: fileStorage)
11+
}
12+
13+
override func tearDown() {
14+
storage = nil
15+
fileStorage = nil
16+
}
17+
18+
func test_default_settings_when_settings_file_does_not_exist() {
19+
// Given
20+
21+
// Make sure settings file doesn't exist
22+
XCTAssertTrue(fileStorage.data.isEmpty)
23+
24+
// When
25+
let settings = storage.settings
26+
27+
// Then
28+
XCTAssertEqual(settings, GeneralAppSettings.default)
29+
}
30+
31+
func test_save_settings_saves_to_file() throws {
32+
// Given
33+
34+
// Make sure settings file doesn't exist
35+
XCTAssertTrue(fileStorage.data.isEmpty)
36+
37+
// When
38+
var settings = GeneralAppSettings.default
39+
settings.installationDate = .init()
40+
try storage.saveSettings(settings)
41+
42+
// Then
43+
XCTAssertTrue(fileStorage.dataWriteIsHit)
44+
XCTAssertEqual(storage.settings, settings)
45+
}
46+
47+
func test_value_reads_settings_default_value() {
48+
// Given
49+
50+
// When
51+
let value = storage.value(for: \.installationDate)
52+
53+
// Then
54+
XCTAssertEqual(value, GeneralAppSettings.default.installationDate)
55+
}
56+
57+
func test_setValue_saves_value() throws {
58+
// Given
59+
60+
// When
61+
let date = Date()
62+
try storage.setValue(date, for: \.installationDate)
63+
64+
// Then
65+
XCTAssertEqual(storage.value(for: \.installationDate), date)
66+
}
67+
}

0 commit comments

Comments
 (0)