@@ -26,6 +26,17 @@ class SuggestedEventsIndexerTests: XCTestCase {
2626 var eventProcessor : EventProcessing ? = TestEventProcessor ( )
2727 var indexer : SuggestedEventsIndexer ! // swiftlint:disable:this implicitly_unwrapped_optional
2828
29+ enum Keys {
30+ static let productionEvents = " production_events "
31+ static let predictionEvents = " eligible_for_prediction_events "
32+ static let setting = " suggestedEventsSetting "
33+ }
34+
35+ enum Values {
36+ static let productionEvents = [ " foo " , " bar " , " baz " ]
37+ static let predictionEvents = productionEvents. map { return $0 + " 1 " }
38+ }
39+
2940 override func setUp( ) {
3041 super. setUp ( )
3142
@@ -129,4 +140,82 @@ class SuggestedEventsIndexerTests: XCTestCase {
129140 " Enabling should load a server configuration "
130141 )
131142 }
143+
144+ func testCompletingEnablingWithErrorOnly( ) {
145+ indexer. enable ( )
146+
147+ TestServerConfigurationProvider . capturedCompletionBlock ? ( nil , SampleError ( ) )
148+
149+ XCTAssertTrue (
150+ indexer. optInEvents. isEmpty,
151+ " Should not set events if there is an error fetching the server configuration "
152+ )
153+ XCTAssertTrue (
154+ indexer. unconfirmedEvents. isEmpty,
155+ " Should not set events if there is an error fetching the server configuration "
156+ )
157+ }
158+
159+ func testCompletingEnablingWithEmptySuggestedEventsSetting( ) {
160+ indexer. enable ( )
161+
162+ TestServerConfigurationProvider . capturedCompletionBlock ? (
163+ ServerConfigurationFixtures . defaultConfig ( ) ,
164+ nil
165+ )
166+
167+ XCTAssertTrue (
168+ indexer. optInEvents. isEmpty,
169+ " Should not set events if there is no suggested events setting in the server configuration "
170+ )
171+ XCTAssertTrue (
172+ indexer. unconfirmedEvents. isEmpty,
173+ " Should not set events if there is no suggested events setting in the server configuration "
174+ )
175+ }
176+
177+ func testCompletingEnablingWithSuggestedEventsSettingAndError( ) {
178+ indexer. enable ( )
179+
180+ TestServerConfigurationProvider . capturedCompletionBlock ? (
181+ ServerConfigurationFixtures . config ( with: validSetting) ,
182+ SampleError ( )
183+ )
184+
185+ XCTAssertTrue (
186+ indexer. optInEvents. isEmpty,
187+ " Should not set events if there is an error fetching the server configuration "
188+ )
189+ XCTAssertTrue (
190+ indexer. unconfirmedEvents. isEmpty,
191+ " Should not set events if there is an error fetching the server configuration "
192+ )
193+ }
194+
195+ func testCompletingEnablingWithNonRepeatingSuggestedEventsSetting( ) {
196+ indexer. enable ( )
197+
198+ TestServerConfigurationProvider . capturedCompletionBlock ? (
199+ ServerConfigurationFixtures . config ( with: validSetting) ,
200+ nil
201+ )
202+
203+ XCTAssertEqual (
204+ indexer. optInEvents,
205+ Set ( Values . productionEvents) ,
206+ " Should set up suggested events successfully "
207+ )
208+ XCTAssertEqual (
209+ indexer. unconfirmedEvents,
210+ Set ( Values . predictionEvents) ,
211+ " Should set up suggested events successfully "
212+ )
213+ }
214+
215+ let validSetting : [ String : Any ] = [
216+ Keys . setting: [
217+ Keys . productionEvents: Values . productionEvents,
218+ Keys . predictionEvents: Values . predictionEvents
219+ ]
220+ ]
132221}
0 commit comments