-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tests to remove reliance on UserDefaults (#156)
### Description - Remove all usages of UserDefaults from MapboxSearch and MapboxSearchUI - Replaces this with plain dependency injection - Add LocalhostMockServiceProvider for integration tests to provide a mocked customBaseURL - Add FeedbackIntegrationTestCase back to UI test suite - Split MockServerTestCase into UI and Integration variants because it had a namespace collision - Utilize customBaseURL for ServiceProvider and LocalhostMockServiceProvider - Stop reading from "MapboxAPIBaseURL" from NSUserDefaults in `ServiceProvider.createEngine`. - Providing a value in Info.plist is still supported. - Update integration tests to use LocalhostMockServiceProvider - This fixes a bug in the Demo app where you building and running the app or tests could use the wrong baseApiURL based on the current value in user defaults. - Update and correct tests for iOS 17 using all mocked data for unit and UI tests. - Rename `SearchEngine.reverseGeocoding` function to `SearchEngine.reverse`. ### Checklist - [x] Update `CHANGELOG` ### Screenshots | Before | After | | -- | -- | | <img width="355" alt="Screenshot 2024-02-06 at 17 47 49" src="https://github.com/mapbox/mapbox-search-ios/assets/384288/f615ad45-efeb-4687-9373-f89d0037c982"> | <img width="356" alt="Screenshot 2024-02-06 at 17 28 38" src="https://github.com/mapbox/mapbox-search-ios/assets/384288/801d518e-c8c2-4403-93f8-1ef2336338c7"> |
- Loading branch information
Showing
30 changed files
with
861 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
Tests/MapboxSearchIntegrationTests/LocalhostMockServiceProvider.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
@testable import MapboxSearch | ||
import XCTest | ||
|
||
class LocalhostMockServiceProvider: ServiceProviderProtocol { | ||
public static var customBaseURL: String? | ||
|
||
/// LocalDataProvider for favorites records | ||
public let localFavoritesProvider = FavoritesProvider() | ||
/// LocalDataProvider for history records | ||
public let localHistoryProvider = HistoryProvider() | ||
/// MapboxMobileEvents manager for analytics usage | ||
public let eventsManager = EventsManager() | ||
|
||
/// Responsible for sending feedback related events. | ||
public private(set) lazy var feedbackManager = FeedbackManager(eventsManager: eventsManager) | ||
|
||
/// Shared instance of ServiceProvider | ||
public static let shared = LocalhostMockServiceProvider() | ||
|
||
var dataLayerProviders: [IndexableDataProvider] { [localHistoryProvider, localFavoritesProvider] } | ||
} | ||
|
||
extension LocalhostMockServiceProvider: EngineProviderProtocol { | ||
func getStoredAccessToken() -> String? { | ||
Bundle.main.object(forInfoDictionaryKey: accessTokenPlistKey) as? String | ||
} | ||
|
||
func createEngine( | ||
apiType: CoreSearchEngine.ApiType, | ||
accessToken: String, | ||
locationProvider: CoreLocationProvider? | ||
) -> CoreSearchEngineProtocol { | ||
MapboxOptions.accessToken = accessToken | ||
|
||
let engineOptions = CoreSearchEngine.Options( | ||
baseUrl: Self.customBaseURL, | ||
apiType: NSNumber(value: apiType.rawValue), | ||
sdkInformation: SdkInformation.defaultInfo, | ||
eventsUrl: nil | ||
) | ||
|
||
return CoreSearchEngine( | ||
options: engineOptions, | ||
location: locationProvider | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
Tests/MapboxSearchIntegrationTests/SearchEngineGeocodingIntegrationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import CoreLocation | ||
@testable import MapboxSearch | ||
import XCTest | ||
|
||
class SearchEngineGeocodingIntegrationTests: MockServerIntegrationTestCase { | ||
let delegate = SearchEngineDelegateStub() | ||
var searchEngine: SearchEngine! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
|
||
searchEngine = SearchEngine( | ||
accessToken: "access-token", | ||
serviceProvider: LocalhostMockServiceProvider.shared, | ||
locationProvider: DefaultLocationProvider(), | ||
apiType: .geocoding | ||
) | ||
|
||
searchEngine.delegate = delegate | ||
} | ||
|
||
func testReverseGeocodingSearch() throws { | ||
try server.setResponse(.reverseGeocoding) | ||
|
||
let expectation = XCTestExpectation() | ||
let options = ReverseGeocodingOptions(point: CLLocationCoordinate2D(latitude: 12.0, longitude: 12.0)) | ||
|
||
// Search engine is a SBS type! | ||
searchEngine.reverse(options: options) { result in | ||
if case .success(let reverseGeocodingResults) = result { | ||
XCTAssertFalse(reverseGeocodingResults.isEmpty) | ||
} else { | ||
XCTFail("No resolved result") | ||
} | ||
expectation.fulfill() | ||
} | ||
|
||
wait(for: [expectation], timeout: 10) | ||
} | ||
|
||
func testReverseGeocodingSearchFailed() throws { | ||
try server.setResponse(.reverseGeocoding, statusCode: 500) | ||
|
||
let expectation = XCTestExpectation() | ||
let options = ReverseGeocodingOptions(point: CLLocationCoordinate2D(latitude: 12.0, longitude: 12.0)) | ||
searchEngine.reverse(options: options) { result in | ||
if case .failure(.reverseGeocodingFailed(let reasonError as NSError, _)) = result { | ||
XCTAssert(reasonError.code == 500) | ||
expectation.fulfill() | ||
} | ||
} | ||
wait(for: [expectation], timeout: 10) | ||
} | ||
} |
Oops, something went wrong.