|
| 1 | +import Testing |
| 2 | +import UserNotifications |
| 3 | +@testable import WooCommerce |
| 4 | +@testable import Yosemite |
| 5 | + |
| 6 | +struct POSNotificationSchedulerTests { |
| 7 | + private let mockFeatureFlagService: MockFeatureFlagService |
| 8 | + private let mockPushNotesManager: MockPushNotificationsManager |
| 9 | + |
| 10 | + init() { |
| 11 | + mockFeatureFlagService = MockFeatureFlagService() |
| 12 | + mockPushNotesManager = MockPushNotificationsManager() |
| 13 | + } |
| 14 | + |
| 15 | + @Test func scheduleLocalNotificationIfEligible_when_featureFlag_is_disabled_then_no_notification_scheduled() async throws { |
| 16 | + // Given |
| 17 | + let siteSettings = sampleSiteSettings(countryCode: "US") |
| 18 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = false |
| 19 | + |
| 20 | + let scheduler = POSNotificationScheduler( |
| 21 | + siteSettings: siteSettings, |
| 22 | + featureFlagService: mockFeatureFlagService, |
| 23 | + pushNotificationsManager: mockPushNotesManager |
| 24 | + ) |
| 25 | + |
| 26 | + // When |
| 27 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 28 | + |
| 29 | + // Then |
| 30 | + #expect(mockPushNotesManager.requestedLocalNotifications.isEmpty) |
| 31 | + } |
| 32 | + |
| 33 | + @Test func scheduleLocalNotificationIfEligible_when_country_is_US_and_featureFlag_is_enabled_then_notification_is_scheduled() async throws { |
| 34 | + // Given |
| 35 | + let siteSettings = sampleSiteSettings(countryCode: "US") |
| 36 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 37 | + |
| 38 | + let scheduler = POSNotificationScheduler( |
| 39 | + siteSettings: siteSettings, |
| 40 | + featureFlagService: mockFeatureFlagService, |
| 41 | + pushNotificationsManager: mockPushNotesManager |
| 42 | + ) |
| 43 | + |
| 44 | + // When/Then |
| 45 | + await confirmation() { confirmation in |
| 46 | + mockPushNotesManager.onRequestLocalNotificationCalled = { |
| 47 | + confirmation() |
| 48 | + } |
| 49 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 50 | + } |
| 51 | + |
| 52 | + #expect(mockPushNotesManager.requestedLocalNotifications.count == 1) |
| 53 | + } |
| 54 | + |
| 55 | + @Test func scheduleLocalNotificationIfEligible_when_country_is_GB_and_featureFlag_is_enabled_then_notification_is_scheduled() async throws { |
| 56 | + // Given |
| 57 | + let siteSettings = sampleSiteSettings(countryCode: "GB") |
| 58 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 59 | + |
| 60 | + let scheduler = POSNotificationScheduler( |
| 61 | + siteSettings: siteSettings, |
| 62 | + featureFlagService: mockFeatureFlagService, |
| 63 | + pushNotificationsManager: mockPushNotesManager |
| 64 | + ) |
| 65 | + |
| 66 | + // When/Then |
| 67 | + await confirmation() { confirmation in |
| 68 | + mockPushNotesManager.onRequestLocalNotificationCalled = { |
| 69 | + confirmation() |
| 70 | + } |
| 71 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 72 | + } |
| 73 | + |
| 74 | + #expect(mockPushNotesManager.requestedLocalNotifications.count == 1) |
| 75 | + } |
| 76 | + |
| 77 | + @Test func scheduleLocalNotificationIfEligible_when_country_is_not_eligible_then_no_notification_scheduled() async throws { |
| 78 | + // Given |
| 79 | + let siteSettings = sampleSiteSettings(countryCode: "FR") |
| 80 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 81 | + |
| 82 | + let scheduler = POSNotificationScheduler( |
| 83 | + siteSettings: siteSettings, |
| 84 | + featureFlagService: mockFeatureFlagService, |
| 85 | + pushNotificationsManager: mockPushNotesManager |
| 86 | + ) |
| 87 | + |
| 88 | + // When |
| 89 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 90 | + |
| 91 | + // Then |
| 92 | + #expect(mockPushNotesManager.requestedLocalNotifications.isEmpty) |
| 93 | + } |
| 94 | + |
| 95 | + @Test func scheduleLocalNotificationIfEligible_when_potentialMerchant_case_then_uses_correct_scenario() async throws { |
| 96 | + // Given |
| 97 | + let siteSettings = sampleSiteSettings(countryCode: "US") |
| 98 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 99 | + |
| 100 | + let scheduler = POSNotificationScheduler( |
| 101 | + siteSettings: siteSettings, |
| 102 | + featureFlagService: mockFeatureFlagService, |
| 103 | + pushNotificationsManager: mockPushNotesManager |
| 104 | + ) |
| 105 | + |
| 106 | + // When/Then |
| 107 | + await confirmation() { confirmation in |
| 108 | + mockPushNotesManager.onRequestLocalNotificationCalled = { |
| 109 | + confirmation() |
| 110 | + } |
| 111 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 112 | + } |
| 113 | + |
| 114 | + let notification = try #require(mockPushNotesManager.requestedLocalNotifications.first) |
| 115 | + let trigger = try #require(mockPushNotesManager.triggersForRequestedLocalNotifications.first as? UNTimeIntervalNotificationTrigger) |
| 116 | + |
| 117 | + #expect(notification.scenario == .pointOfSalePotentialMerchant) |
| 118 | + #expect(notification.userInfo[LocalNotification.UserInfoKey.surveyURL] as? String == |
| 119 | + LocalNotification.SurveyURL.pointOfSalePotentialMerchant) |
| 120 | + #expect(trigger.timeInterval == 60) |
| 121 | + #expect(trigger.repeats == false) |
| 122 | + } |
| 123 | + |
| 124 | + @Test func scheduleLocalNotificationIfEligible_when_currentMerchant_case_then_uses_correct_scenario() async throws { |
| 125 | + // Given |
| 126 | + let siteSettings = sampleSiteSettings(countryCode: "US") |
| 127 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 128 | + |
| 129 | + let scheduler = POSNotificationScheduler( |
| 130 | + siteSettings: siteSettings, |
| 131 | + featureFlagService: mockFeatureFlagService, |
| 132 | + pushNotificationsManager: mockPushNotesManager |
| 133 | + ) |
| 134 | + |
| 135 | + // When/Then |
| 136 | + await confirmation() { confirmation in |
| 137 | + mockPushNotesManager.onRequestLocalNotificationCalled = { |
| 138 | + confirmation() |
| 139 | + } |
| 140 | + await scheduler.scheduleLocalNotificationIfEligible(for: .currentMerchant) |
| 141 | + } |
| 142 | + |
| 143 | + let notification = try #require(mockPushNotesManager.requestedLocalNotifications.first) |
| 144 | + let trigger = try #require(mockPushNotesManager.triggersForRequestedLocalNotifications.first as? UNTimeIntervalNotificationTrigger) |
| 145 | + |
| 146 | + #expect(notification.scenario == .pointOfSaleCurrentMerchant) |
| 147 | + #expect(notification.userInfo[LocalNotification.UserInfoKey.surveyURL] as? String == |
| 148 | + LocalNotification.SurveyURL.pointOfSaleCurrentMerchant) |
| 149 | + #expect(trigger.timeInterval == 300) |
| 150 | + #expect(trigger.repeats == false) |
| 151 | + } |
| 152 | + |
| 153 | + @Test func scheduleLocalNotificationIfEligible_when_eligible_then_notification_manager_receives_correct_notification() async throws { |
| 154 | + // Given |
| 155 | + let siteSettings = sampleSiteSettings(countryCode: "GB") |
| 156 | + mockFeatureFlagService.isFeatureFlagEnabledReturnValue[.pointOfSaleSurveys] = true |
| 157 | + |
| 158 | + let scheduler = POSNotificationScheduler( |
| 159 | + siteSettings: siteSettings, |
| 160 | + featureFlagService: mockFeatureFlagService, |
| 161 | + pushNotificationsManager: mockPushNotesManager |
| 162 | + ) |
| 163 | + |
| 164 | + // When/Then |
| 165 | + await confirmation() { confirmation in |
| 166 | + mockPushNotesManager.onRequestLocalNotificationCalled = { |
| 167 | + confirmation() |
| 168 | + } |
| 169 | + await scheduler.scheduleLocalNotificationIfEligible(for: .potentialMerchant) |
| 170 | + } |
| 171 | + |
| 172 | + #expect(mockPushNotesManager.requestedLocalNotifications.count == 1) |
| 173 | + #expect(mockPushNotesManager.triggersForRequestedLocalNotifications.count == 1) |
| 174 | + |
| 175 | + let notification = try #require(mockPushNotesManager.requestedLocalNotifications.first) |
| 176 | + #expect(notification.scenario == .pointOfSalePotentialMerchant) |
| 177 | + #expect(notification.userInfo.keys.contains(LocalNotification.UserInfoKey.surveyURL)) |
| 178 | + } |
| 179 | + |
| 180 | + private func sampleSiteSettings(countryCode: String) -> [SiteSetting] { |
| 181 | + [ |
| 182 | + SiteSetting.fake().copy( |
| 183 | + siteID: 123, |
| 184 | + settingID: "woocommerce_default_country", |
| 185 | + value: countryCode, |
| 186 | + settingGroupKey: SiteSettingGroup.general.rawValue |
| 187 | + ) |
| 188 | + ] |
| 189 | + } |
| 190 | +} |
0 commit comments