Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salimbraksa committed Feb 4, 2024
1 parent dc5803f commit a4e6276
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,36 +1,42 @@
import SwiftUI
import Combine

final class BooleanUserDefaultsDebugViewModel: ObservableObject {

private let persistentRepository: UserPersistentRepository
private let coreDataStack: CoreDataStackSwift
private var allUserDefaultsSections = Sections()
private var cancellables = Set<AnyCancellable>()

private var allUserDefaultsSections = Sections() {
didSet {
self.reloadSections()
}
}
// MARK: - State

@Published var searchQuery: String = "" {
didSet {
self.reloadSections()
Task { @MainActor in
self.reloadSections()
}
}
}

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

// MARK: - Init

init(coreDataStack: CoreDataStackSwift = ContextManager.shared,
persistentRepository: UserPersistentRepository = UserPersistentStoreFactory.instance()) {
self.coreDataStack = coreDataStack
self.persistentRepository = persistentRepository
}

// MARK: - Load

func load() {
Task {
await self.load()
}
}

private func load() async {
func load() async {
let allUserDefaults = persistentRepository.dictionaryRepresentation()
var loadedUserDefaultsSections = Sections()
var otherSection = [Row]()
Expand All @@ -55,14 +61,18 @@ final class BooleanUserDefaultsDebugViewModel: ObservableObject {
}

self.allUserDefaultsSections = loadedUserDefaultsSections
}

private func reloadSections() {
Task { @MainActor in
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
await MainActor.run {
self.reloadSections()
}
}

// MARK: - Helpers

@MainActor private func reloadSections() {
self.userDefaultsSections = filterUserDefaults(by: searchQuery)
}

private func filterUserDefaults(by query: String) -> Sections {
guard !query.isEmpty else {
return allUserDefaultsSections
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import XCTest
import Combine
@testable import WordPress

private typealias Section = BooleanUserDefaultsDebugViewModel.Section
private typealias Row = BooleanUserDefaultsDebugViewModel.Row

class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
@MainActor final class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {

var viewModel: BooleanUserDefaultsDebugViewModel!
var mockPersistentRepository: InMemoryUserDefaults!
var cancellables: Set<AnyCancellable>!

override func setUp() {
super.setUp()
cancellables = .init()
mockPersistentRepository = InMemoryUserDefaults()
viewModel = BooleanUserDefaultsDebugViewModel(coreDataStack: contextManager,
persistentRepository: mockPersistentRepository)
Expand All @@ -22,12 +25,12 @@ class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase {
super.tearDown()
}

func testLoadUserDefaults_WithOtherSection() {
func testLoadUserDefaults_WithOtherSection() async {
// Given
mockPersistentRepository.set(true, forKey: "entry1")

// When
viewModel.load()
await viewModel.load()

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

func testLoadUserDefaults_WithoutOtherSection() {
func testLoadUserDefaults_WithoutOtherSection() async {
// Given
mockPersistentRepository.set(["entry1": true], forKey: "section1")

// When
viewModel.load()
await viewModel.load()

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

func testUserDefaultsSections_MatchingQuery() {
func testUserDefaultsSections_MatchingQuery() async {
// Given
mockPersistentRepository.set(["match": true], forKey: "section1")
viewModel.load()
await viewModel.load()

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

func testUserDefaultsSections_NotMatchingQuery() {
func testUserDefaultsSections_NotMatchingQuery() async {
// Given
let expectation = expectation(description: "List should be empty when no matching entries")
mockPersistentRepository.set(["entry1": true], forKey: "section1")
viewModel.load()
await viewModel.load()

// When
viewModel.$userDefaultsSections
.dropFirst()
.sink { sections in
XCTAssertTrue(sections.isEmpty)
expectation.fulfill()
}
.store(in: &cancellables)
viewModel.searchQuery = "noMatch"

// Then
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
await fulfillment(of: [expectation], timeout: 1)
}

func testUserDefaultsSections_WithFilteredOutNonBooleanEntries() {
func testUserDefaultsSections_WithFilteredOutNonBooleanEntries() async {
// Given
mockPersistentRepository.set(["entry1": "NotBoolean"], forKey: "section1")
mockPersistentRepository.set(["entry2": false], forKey: "section1")

// When
viewModel.load()
await viewModel.load()

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

func testUserDefaultsSections_WithFilteredOutGutenbergItems() {
func testUserDefaultsSections_WithFilteredOutGutenbergItems() async {
// Given
mockPersistentRepository.set(true, forKey: "com.wordpress.gutenberg-entry")

// When
viewModel.load()
await viewModel.load()

// Then
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
}

func testUserDefaultsSections_WithFilteredOutFeatureFlagSection() {
func testUserDefaultsSections_WithFilteredOutFeatureFlagSection() async {
// Given
mockPersistentRepository.set(["entry1": true], forKey: "FeatureFlagStoreCache")

// When
viewModel.load()
await viewModel.load()

// Then
XCTAssertTrue(viewModel.userDefaultsSections.isEmpty)
}

func testUpdateUserDefault_OtherSection() {
func testUpdateUserDefault_OtherSection() async {
// Given
mockPersistentRepository.set(true, forKey: "entry1")
viewModel.load()
await viewModel.load()

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

func testUpdateUserDefault_GivenSection() {
func testUpdateUserDefault_GivenSection() async {
// Given
mockPersistentRepository.set(["entry1": true], forKey: "section1")
viewModel.load()
await viewModel.load()

// When
let section = viewModel.userDefaultsSections[0]
Expand Down

0 comments on commit a4e6276

Please sign in to comment.