|
| 1 | +import XCTest |
| 2 | +@testable import WordPress |
| 3 | + |
| 4 | +class BooleanUserDefaultsDebugViewModelTests: CoreDataTestCase { |
| 5 | + |
| 6 | + var viewModel: BooleanUserDefaultsDebugViewModel! |
| 7 | + var mockPersistentRepository: InMemoryUserDefaults! |
| 8 | + |
| 9 | + override func setUp() { |
| 10 | + super.setUp() |
| 11 | + mockPersistentRepository = InMemoryUserDefaults() |
| 12 | + viewModel = BooleanUserDefaultsDebugViewModel(coreDataStack: contextManager, |
| 13 | + persistentRepository: mockPersistentRepository) |
| 14 | + } |
| 15 | + |
| 16 | + override func tearDown() { |
| 17 | + viewModel = nil |
| 18 | + mockPersistentRepository = nil |
| 19 | + super.tearDown() |
| 20 | + } |
| 21 | + |
| 22 | + func testLoadUserDefaults_WithOtherSection() { |
| 23 | + // Given |
| 24 | + mockPersistentRepository.set(true, forKey: "entry1") |
| 25 | + |
| 26 | + // When |
| 27 | + viewModel.load() |
| 28 | + |
| 29 | + // Then |
| 30 | + XCTAssertTrue(viewModel.userDefaultsSections["Other"]?["entry1"]?.value ?? false) |
| 31 | + } |
| 32 | + |
| 33 | + func testLoadUserDefaults_WithoutOtherSection() { |
| 34 | + // Given |
| 35 | + mockPersistentRepository.set(["entry1": true], forKey: "section1") |
| 36 | + |
| 37 | + // When |
| 38 | + viewModel.load() |
| 39 | + |
| 40 | + // Then |
| 41 | + XCTAssertNil(viewModel.userDefaultsSections["Other"]) |
| 42 | + } |
| 43 | + |
| 44 | + func testUserDefaultsSections_MatchingQuery() { |
| 45 | + // Given |
| 46 | + mockPersistentRepository.set(["match": true], forKey: "section1") |
| 47 | + viewModel.load() |
| 48 | + |
| 49 | + // When |
| 50 | + viewModel.searchQuery = "mat" |
| 51 | + |
| 52 | + // Then |
| 53 | + XCTAssertNotNil(viewModel.userDefaultsSections["section1"]) |
| 54 | + XCTAssertTrue(viewModel.userDefaultsSections["section1"]?["match"]?.value ?? false) |
| 55 | + } |
| 56 | + |
| 57 | + func testUserDefaultsSections_NotMatchingQuery() { |
| 58 | + // Given |
| 59 | + mockPersistentRepository.set(["entry1": true], forKey: "section1") |
| 60 | + viewModel.load() |
| 61 | + |
| 62 | + // When |
| 63 | + viewModel.searchQuery = "noMatch" |
| 64 | + |
| 65 | + // Then |
| 66 | + XCTAssertTrue(viewModel.userDefaultsSections.isEmpty) |
| 67 | + } |
| 68 | + |
| 69 | + func testUserDefaultsSections_WithFilteredOutNonBooleanEntries() { |
| 70 | + // Given |
| 71 | + mockPersistentRepository.set(["entry1": "NotBoolean"], forKey: "section1") |
| 72 | + mockPersistentRepository.set(["entry2": false], forKey: "section1") |
| 73 | + |
| 74 | + // When |
| 75 | + viewModel.load() |
| 76 | + |
| 77 | + // Then |
| 78 | + XCTAssertTrue(viewModel.userDefaultsSections["section1"]?.count == 1) |
| 79 | + XCTAssertFalse(viewModel.userDefaultsSections["section1"]?["entry2"]?.value ?? true) |
| 80 | + } |
| 81 | + |
| 82 | + func testUserDefaultsSections_WithFilteredOutGutenbergItems() { |
| 83 | + // Given |
| 84 | + mockPersistentRepository.set(true, forKey: "com.wordpress.gutenberg-entry") |
| 85 | + |
| 86 | + // When |
| 87 | + viewModel.load() |
| 88 | + |
| 89 | + // Then |
| 90 | + XCTAssertTrue(viewModel.userDefaultsSections.isEmpty) |
| 91 | + } |
| 92 | + |
| 93 | + func testUserDefaultsSections_WithFilteredOutFeatureFlagSection() { |
| 94 | + // Given |
| 95 | + mockPersistentRepository.set(["entry1": true], forKey: "FeatureFlagStoreCache") |
| 96 | + |
| 97 | + // When |
| 98 | + viewModel.load() |
| 99 | + |
| 100 | + // Then |
| 101 | + XCTAssertTrue(viewModel.userDefaultsSections.isEmpty) |
| 102 | + } |
| 103 | + |
| 104 | + func testUpdateUserDefault_OtherSection() { |
| 105 | + // Given |
| 106 | + mockPersistentRepository.set(true, forKey: "entry1") |
| 107 | + viewModel.load() |
| 108 | + |
| 109 | + // When |
| 110 | + viewModel.updateUserDefault(false, forSection: "Other", forUserDefault: "entry1") |
| 111 | + |
| 112 | + // Then |
| 113 | + XCTAssertEqual(viewModel.userDefaultsSections["Other"]?["entry1"]?.value, false) |
| 114 | + } |
| 115 | + |
| 116 | + func testUpdateUserDefault_GivenSection() { |
| 117 | + // Given |
| 118 | + mockPersistentRepository.set(["entry1": true], forKey: "section1") |
| 119 | + viewModel.load() |
| 120 | + |
| 121 | + // When |
| 122 | + viewModel.updateUserDefault(false, forSection: "section1", forUserDefault: "entry1") |
| 123 | + |
| 124 | + // Then |
| 125 | + XCTAssertEqual(viewModel.userDefaultsSections["section1"]?["entry1"]?.value, false) |
| 126 | + } |
| 127 | +} |
0 commit comments