Skip to content

Commit a4e6276

Browse files
committed
Fix unit tests
1 parent dc5803f commit a4e6276

2 files changed

Lines changed: 52 additions & 31 deletions

File tree

WordPress/Classes/ViewRelated/Me/App Settings/Boolean User Defaults/BooleanUserDefaultsDebugViewModel.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
11
import SwiftUI
2+
import Combine
23

34
final class BooleanUserDefaultsDebugViewModel: ObservableObject {
5+
46
private let persistentRepository: UserPersistentRepository
57
private let coreDataStack: CoreDataStackSwift
8+
private var allUserDefaultsSections = Sections()
9+
private var cancellables = Set<AnyCancellable>()
610

7-
private var allUserDefaultsSections = Sections() {
8-
didSet {
9-
self.reloadSections()
10-
}
11-
}
11+
// MARK: - State
1212

1313
@Published var searchQuery: String = "" {
1414
didSet {
15-
self.reloadSections()
15+
Task { @MainActor in
16+
self.reloadSections()
17+
}
1618
}
1719
}
1820

1921
@MainActor @Published private(set) var userDefaultsSections: Sections = []
2022

23+
// MARK: - Init
24+
2125
init(coreDataStack: CoreDataStackSwift = ContextManager.shared,
2226
persistentRepository: UserPersistentRepository = UserPersistentStoreFactory.instance()) {
2327
self.coreDataStack = coreDataStack
2428
self.persistentRepository = persistentRepository
2529
}
2630

31+
// MARK: - Load
32+
2733
func load() {
2834
Task {
2935
await self.load()
3036
}
3137
}
3238

33-
private func load() async {
39+
func load() async {
3440
let allUserDefaults = persistentRepository.dictionaryRepresentation()
3541
var loadedUserDefaultsSections = Sections()
3642
var otherSection = [Row]()
@@ -55,14 +61,18 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
5561
}
5662

5763
self.allUserDefaultsSections = loadedUserDefaultsSections
58-
}
5964

60-
private func reloadSections() {
61-
Task { @MainActor in
62-
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
65+
await MainActor.run {
66+
self.reloadSections()
6367
}
6468
}
6569

70+
// MARK: - Helpers
71+
72+
@MainActor private func reloadSections() {
73+
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
74+
}
75+
6676
private func filterUserDefaults(by query: String) -> Sections {
6777
guard !query.isEmpty else {
6878
return allUserDefaultsSections

WordPress/WordPressTest/Me/App Settings/Boolean User Defaults/BooleanUserDefaultsDebugViewModelTests.swift

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import XCTest
2+
import Combine
23
@testable import WordPress
34

45
private typealias Section = BooleanUserDefaultsDebugViewModel.Section
56
private typealias Row = BooleanUserDefaultsDebugViewModel.Row
67

7-
class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
8+
@MainActor final class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
89

910
var viewModel: BooleanUserDefaultsDebugViewModel!
1011
var mockPersistentRepository: InMemoryUserDefaults!
12+
var cancellables: Set<AnyCancellable>!
1113

1214
override func setUp() {
1315
super.setUp()
16+
cancellables = .init()
1417
mockPersistentRepository = InMemoryUserDefaults()
1518
viewModel = BooleanUserDefaultsDebugViewModel(coreDataStack: contextManager,
1619
persistentRepository: mockPersistentRepository)
@@ -22,12 +25,12 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
2225
super.tearDown()
2326
}
2427

25-
func testLoadUserDefaults_WithOtherSection() {
28+
func testLoadUserDefaults_WithOtherSection() async {
2629
// Given
2730
mockPersistentRepository.set(true, forKey: "entry1")
2831

2932
// When
30-
viewModel.load()
33+
await viewModel.load()
3134

3235
// Then
3336
XCTAssertTrue(viewModel.userDefaultsSections.count == 1)
@@ -38,12 +41,12 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
3841
XCTAssertTrue(viewModel.userDefaultsSections.first?.rows.first?.value == true)
3942
}
4043

41-
func testLoadUserDefaults_WithoutOtherSection() {
44+
func testLoadUserDefaults_WithoutOtherSection() async {
4245
// Given
4346
mockPersistentRepository.set(["entry1": true], forKey: "section1")
4447

4548
// When
46-
viewModel.load()
49+
await viewModel.load()
4750

4851
// Then
4952
XCTAssertTrue(viewModel.userDefaultsSections.count == 1)
@@ -54,10 +57,10 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
5457
XCTAssertTrue(viewModel.userDefaultsSections.first?.rows.first?.value == true)
5558
}
5659

57-
func testUserDefaultsSections_MatchingQuery() {
60+
func testUserDefaultsSections_MatchingQuery() async {
5861
// Given
5962
mockPersistentRepository.set(["match": true], forKey: "section1")
60-
viewModel.load()
63+
await viewModel.load()
6164

6265
// When
6366
viewModel.searchQuery = "mat"
@@ -71,25 +74,33 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
7174
XCTAssertTrue(viewModel.userDefaultsSections.first?.rows.first?.value == true)
7275
}
7376

74-
func testUserDefaultsSections_NotMatchingQuery() {
77+
func testUserDefaultsSections_NotMatchingQuery() async {
7578
// Given
79+
let expectation = expectation(description: "List should be empty when no matching entries")
7680
mockPersistentRepository.set(["entry1": true], forKey: "section1")
77-
viewModel.load()
81+
await viewModel.load()
7882

7983
// When
84+
viewModel.$userDefaultsSections
85+
.dropFirst()
86+
.sink { sections in
87+
XCTAssertTrue(sections.isEmpty)
88+
expectation.fulfill()
89+
}
90+
.store(in: &cancellables)
8091
viewModel.searchQuery = "noMatch"
8192

8293
// Then
83-
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
94+
await fulfillment(of: [expectation], timeout: 1)
8495
}
8596

86-
func testUserDefaultsSections_WithFilteredOutNonBooleanEntries() {
97+
func testUserDefaultsSections_WithFilteredOutNonBooleanEntries() async {
8798
// Given
8899
mockPersistentRepository.set(["entry1": "NotBoolean"], forKey: "section1")
89100
mockPersistentRepository.set(["entry2": false], forKey: "section1")
90101

91102
// When
92-
viewModel.load()
103+
await viewModel.load()
93104

94105
// Then
95106
XCTAssertTrue(viewModel.userDefaultsSections.count == 1)
@@ -100,32 +111,32 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
100111
XCTAssertTrue(viewModel.userDefaultsSections.first?.rows.first?.value == false)
101112
}
102113

103-
func testUserDefaultsSections_WithFilteredOutGutenbergItems() {
114+
func testUserDefaultsSections_WithFilteredOutGutenbergItems() async {
104115
// Given
105116
mockPersistentRepository.set(true, forKey: "com.wordpress.gutenberg-entry")
106117

107118
// When
108-
viewModel.load()
119+
await viewModel.load()
109120

110121
// Then
111122
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
112123
}
113124

114-
func testUserDefaultsSections_WithFilteredOutFeatureFlagSection() {
125+
func testUserDefaultsSections_WithFilteredOutFeatureFlagSection() async {
115126
// Given
116127
mockPersistentRepository.set(["entry1": true], forKey: "FeatureFlagStoreCache")
117128

118129
// When
119-
viewModel.load()
130+
await viewModel.load()
120131

121132
// Then
122133
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
123134
}
124135

125-
func testUpdateUserDefault_OtherSection() {
136+
func testUpdateUserDefault_OtherSection() async {
126137
// Given
127138
mockPersistentRepository.set(true, forKey: "entry1")
128-
viewModel.load()
139+
await viewModel.load()
129140

130141
// When
131142
let section = viewModel.userDefaultsSections[0]
@@ -141,10 +152,10 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
141152
XCTAssertTrue(viewModel.userDefaultsSections.first?.rows.first?.value == false)
142153
}
143154

144-
func testUpdateUserDefault_GivenSection() {
155+
func testUpdateUserDefault_GivenSection() async {
145156
// Given
146157
mockPersistentRepository.set(["entry1": true], forKey: "section1")
147-
viewModel.load()
158+
await viewModel.load()
148159

149160
// When
150161
let section = viewModel.userDefaultsSections[0]

0 commit comments

Comments
 (0)