Skip to content

Commit

Permalink
Add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nishant-karajgikar committed Aug 24, 2021
1 parent 6eeaa79 commit 40166cb
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import UIKit
import MapboxMaps
import MapKit

@objc(FeatureStateExample)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import Turf
import XCTest
@testable import MapboxMaps

internal class FeatureStateIntegrationTests: MapViewIntegrationTestCase {

internal func testSetFeatureState() {
style?.uri = .streets
let featureStateExpectation = XCTestExpectation(description: "Wait for feature state map to be updated.")

didFinishLoadingStyle = { mapView in

do {
try mapView.mapboxMap.style.addSource(
makeGeoJSONSource(),
id: "test-source")
try mapView.mapboxMap.style.addLayer(
makeLayer())

} catch {
XCTFail("Failed to add geojson source / layer due to error: \(error)")
}
}

didBecomeIdle = { mapView in
mapView.mapboxMap.setFeatureState(sourceId: "test-source", featureId: "0", state: ["testKey": true])

DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak mapView] in
mapView?.mapboxMap.getFeatureState(sourceId: "test-source", featureId: "0") { result in
switch result {
case .success(let map):
XCTAssertEqual(map["testKey"] as? Bool, true)
featureStateExpectation.fulfill()
case .failure(let error):
XCTFail("Could not retrieve feature state: \(error)")
}
}
}
}

wait(for: [featureStateExpectation], timeout: 5.0)
}

internal func testRemoveFeatureState() {
style?.uri = .streets
let featureStateExpectation = XCTestExpectation(description: "Wait for feature state map to be updated.")

didFinishLoadingStyle = { mapView in
do {
try mapView.mapboxMap.style.addSource(
makeGeoJSONSource(),
id: "test-source")
try mapView.mapboxMap.style.addLayer(
makeLayer())
} catch {
XCTFail("Failed to add geojson source / layer due to error: \(error)")
}
}

didBecomeIdle = { mapView in
mapView.mapboxMap.setFeatureState(sourceId: "test-source", featureId: "0", state: ["testKey": true])

DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak mapView] in

mapView?.mapboxMap.removeFeatureState(sourceId: "test-source", featureId: "0")

DispatchQueue.main.asyncAfter(deadline: .now() + 1) { [weak mapView] in
mapView?.mapboxMap.getFeatureState(
sourceId: "test-source",
featureId: "0") { result in

switch result {
case .success(let map):
XCTAssert(map.isEmpty)
featureStateExpectation.fulfill()
case .failure(let error):
XCTFail("Could not retrieve feature state: \(error)")
}
}
}
}
}

wait(for: [featureStateExpectation], timeout: 5.0)
}

// MARK: - Helper

fileprivate func makeGeoJSONSource() -> GeoJSONSource {

let coord = CLLocationCoordinate2D(latitude: 14.765625,
longitude: 26.194876675795218)
let point = Turf.Point(coord)
let feature = Turf.Feature(point)

var geojsonSource = GeoJSONSource()
geojsonSource.generateId = true
geojsonSource.data = .feature(feature)

return geojsonSource
}

fileprivate func makeLayer() -> SymbolLayer {
var symbolLayer = SymbolLayer(id: "test-layer")
symbolLayer.source = "test-source"
symbolLayer.textField = .constant("test")

return symbolLayer
}
}

0 comments on commit 40166cb

Please sign in to comment.