|
| 1 | +import XCTest |
| 2 | +import Yosemite |
| 3 | +@testable import WooCommerce |
| 4 | + |
| 5 | +final class CardPresentConfigurationLoaderTests: XCTestCase { |
| 6 | + /// Mock Storage: InMemory |
| 7 | + /// |
| 8 | + private var storageManager: MockStorageManager! |
| 9 | + |
| 10 | + /// Mock Stores |
| 11 | + /// |
| 12 | + private var stores: MockStoresManager! |
| 13 | + |
| 14 | + /// Dummy Site ID |
| 15 | + /// |
| 16 | + private let sampleSiteID: Int64 = 1234 |
| 17 | + |
| 18 | + override func setUpWithError() throws { |
| 19 | + try super.setUpWithError() |
| 20 | + storageManager = MockStorageManager() |
| 21 | + stores = MockStoresManager(sessionManager: .makeForTesting(authenticated: true)) |
| 22 | + stores.whenReceivingAction(ofType: AppSettingsAction.self) { action in |
| 23 | + switch action { |
| 24 | + case .loadStripeInPersonPaymentsSwitchState(let completion): |
| 25 | + completion(.success(true)) |
| 26 | + case .loadCanadaInPersonPaymentsSwitchState(let completion): |
| 27 | + completion(.success(true)) |
| 28 | + default: |
| 29 | + break |
| 30 | + } |
| 31 | + } |
| 32 | + stores.sessionManager.setStoreId(sampleSiteID) |
| 33 | + ServiceLocator.setSelectedSiteSettings(SelectedSiteSettings(stores: stores, storageManager: storageManager)) |
| 34 | + } |
| 35 | + |
| 36 | + override func tearDownWithError() throws { |
| 37 | + ServiceLocator.setSelectedSiteSettings(SelectedSiteSettings()) |
| 38 | + storageManager.reset() |
| 39 | + storageManager = nil |
| 40 | + stores = nil |
| 41 | + try super.tearDownWithError() |
| 42 | + } |
| 43 | + |
| 44 | + func test_configuration_for_US_with_stripe_enabled_and_canada_enabled() { |
| 45 | + // Given |
| 46 | + setupFeatures(stripe: true, canada: true) |
| 47 | + setupCountry(country: .us) |
| 48 | + |
| 49 | + // When |
| 50 | + let loader = CardPresentConfigurationLoader(stores: stores) |
| 51 | + let configuration = loader.configuration |
| 52 | + |
| 53 | + // Then |
| 54 | + XCTAssertTrue(configuration.isSupportedCountry) |
| 55 | + } |
| 56 | + |
| 57 | + func test_configuration_for_Canada_with_stripe_enabled_and_canada_enabled() { |
| 58 | + // Given |
| 59 | + setupFeatures(stripe: true, canada: true) |
| 60 | + setupCountry(country: .ca) |
| 61 | + |
| 62 | + // When |
| 63 | + let loader = CardPresentConfigurationLoader(stores: stores) |
| 64 | + let configuration = loader.configuration |
| 65 | + |
| 66 | + // Then |
| 67 | + XCTAssertTrue(configuration.isSupportedCountry) |
| 68 | + } |
| 69 | + |
| 70 | + func test_configuration_for_Spain_with_stripe_enabled_and_canada_enabled() { |
| 71 | + // Given |
| 72 | + setupFeatures(stripe: true, canada: true) |
| 73 | + setupCountry(country: .es) |
| 74 | + |
| 75 | + // When |
| 76 | + let loader = CardPresentConfigurationLoader(stores: stores) |
| 77 | + let configuration = loader.configuration |
| 78 | + |
| 79 | + // Then |
| 80 | + XCTAssertFalse(configuration.isSupportedCountry) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private extension CardPresentConfigurationLoaderTests { |
| 85 | + func setupCountry(country: Country) { |
| 86 | + let setting = SiteSetting.fake() |
| 87 | + .copy( |
| 88 | + siteID: sampleSiteID, |
| 89 | + settingID: "woocommerce_default_country", |
| 90 | + value: country.rawValue, |
| 91 | + settingGroupKey: SiteSettingGroup.general.rawValue |
| 92 | + ) |
| 93 | + storageManager.insertSampleSiteSetting(readOnlySiteSetting: setting) |
| 94 | + ServiceLocator.selectedSiteSettings.refresh() |
| 95 | + } |
| 96 | + |
| 97 | + enum Country: String { |
| 98 | + case us = "US:CA" |
| 99 | + case ca = "CA:NS" |
| 100 | + case es = "ES" |
| 101 | + } |
| 102 | + |
| 103 | + func setupFeatures(stripe: Bool, canada: Bool) { |
| 104 | + stores.whenReceivingAction(ofType: AppSettingsAction.self) { action in |
| 105 | + switch action { |
| 106 | + case .loadStripeInPersonPaymentsSwitchState(onCompletion: let completion): |
| 107 | + completion(.success(stripe)) |
| 108 | + case .loadCanadaInPersonPaymentsSwitchState(onCompletion: let completion): |
| 109 | + completion(.success(canada)) |
| 110 | + default: |
| 111 | + break |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments