Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mltbnz committed Jul 2, 2023
1 parent 10715b3 commit a0e7acd
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 103 deletions.
69 changes: 19 additions & 50 deletions CriticalMapsKit/Tests/AppFeatureTests/AppFeatureCoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ final class AppFeatureTests: XCTestCase {
var state = AppFeature.State()
let location = Location(coordinate: .make(), timestamp: 42)
state.mapFeatureState.location = location
state.settingsState.userSettings.rideEventSettings.isEnabled = true
state.settingsState.userSettings.rideEventSettings.eventDistance = .close
state.settingsState.rideEventSettings.isEnabled = true
state.settingsState.rideEventSettings.eventSearchRadius = .close

let store = TestStore(
initialState: state,
Expand All @@ -219,8 +219,8 @@ final class AppFeatureTests: XCTestCase {
[Ride(id: 123, title: "Test", dateTime: Date(timeIntervalSince1970: 0), enabled: true)]
}

await store.send(.settings(.rideevent(.setRideEventRadius(.far)))) {
$0.settingsState.userSettings.rideEventSettings.eventDistance = .far
await store.send(.settings(.rideevent(.set(\.$eventSearchRadius, .far)))) {
$0.settingsState.rideEventSettings.eventSearchRadius = .far
}
await testQueue.advance(by: 2)
await store.receive(.nextRide(.getNextRide(location.coordinate)))
Expand All @@ -237,7 +237,7 @@ final class AppFeatureTests: XCTestCase {
)

var state = AppFeature.State()
state.settingsState.userSettings.rideEventSettings.isEnabled = true
state.settingsState.rideEventSettings.isEnabled = true
state.nextRideState.userLocation = nil
state.mapFeatureState.location = sharedModelLocation

Expand All @@ -246,7 +246,7 @@ final class AppFeatureTests: XCTestCase {
reducer: AppFeature()
)
store.exhaustivity = .off
store.dependencies.date = .init(date)
store.dependencies.date = .init({ @Sendable in self.date() })

let locations: [ComposableCoreLocation.Location] = [location]
await store.send(.map(.locationManager(.didUpdateLocations(locations)))) {
Expand Down Expand Up @@ -289,17 +289,19 @@ final class AppFeatureTests: XCTestCase {
initialState: state,
reducer: AppFeature()
)
store.exhaustivity = .off
store.dependencies.mainQueue = .immediate

let coordinate = Coordinate.make()

await store.send(.map(.focusRideEvent(coordinate))) {
$0.mapFeatureState.eventCenter = CoordinateRegion(center: coordinate.asCLLocationCoordinate)
}
await store.receive(.set(\.$bottomSheetPosition, .relative(0.4))) {
await store.receive(.binding(.set(\.$bottomSheetPosition, .relative(CGFloat(0.4))))) {
$0.bottomSheetPosition = .relative(0.4)
}
await store.receive(.map(.resetRideEventCenter)) {
$0.mapFeatureState.eventCenter = nil
}
}

func test_requestTimerTick_fireUpFetchLocations() async {
Expand Down Expand Up @@ -336,7 +338,7 @@ final class AppFeatureTests: XCTestCase {
var state = AppFeature.State()
let location = Location(coordinate: .make(), timestamp: 42)
state.mapFeatureState.location = location
state.settingsState.userSettings.rideEventSettings.isEnabled = true
state.settingsState.rideEventSettings.isEnabled = true

let store = TestStore(
initialState: state,
Expand All @@ -350,8 +352,8 @@ final class AppFeatureTests: XCTestCase {
[Ride(id: 123, title: "Test", dateTime: Date(timeIntervalSince1970: 0), enabled: true)]
}

await store.send(.settings(.rideevent(.setRideEventsEnabled(true)))) {
$0.settingsState.userSettings.rideEventSettings.isEnabled = true
await store.send(.settings(.rideevent(.set(\.$isEnabled, true)))) {
$0.settingsState.rideEventSettings.isEnabled = true
}
await testQueue.advance(by: 2)
await store.receive(.nextRide(.getNextRide(location.coordinate)))
Expand All @@ -363,9 +365,9 @@ final class AppFeatureTests: XCTestCase {

var state = AppFeature.State()
let location = Location(coordinate: .make(), timestamp: 42)
state.settingsState.userSettings.rideEventSettings.eventDistance = .close
state.settingsState.rideEventSettings.eventSearchRadius = .close
state.mapFeatureState.location = location
state.settingsState.userSettings.rideEventSettings.isEnabled = true
state.settingsState.rideEventSettings.isEnabled = true

let store = TestStore(
initialState: state,
Expand All @@ -378,9 +380,9 @@ final class AppFeatureTests: XCTestCase {
await updatedRaduis.setValue(radius)
return [Ride(id: 123, title: "Test", dateTime: self.date(), enabled: true)]
}

await store.send(.settings(.rideevent(.setRideEventRadius(.far)))) {
$0.settingsState.userSettings.rideEventSettings.eventDistance = .far
await store.send(.settings(.rideevent(.set(\.$eventSearchRadius, .far)))) {
$0.settingsState.rideEventSettings.eventSearchRadius = .far
}
await testQueue.advance(by: 2)
await store.receive(.nextRide(.getNextRide(location.coordinate)))
Expand All @@ -390,34 +392,6 @@ final class AppFeatureTests: XCTestCase {
}
}

func test_didSaveUserSettings() async throws {
let didSaveUserSettings = ActorIsolated(false)
let didSaveObserverPromptSetting = ActorIsolated(false)

let testQueue = DispatchQueue.test

let store = TestStore(
initialState: AppFeature.State(),
reducer: AppFeature()
)
store.dependencies.mainQueue = testQueue.eraseToAnyScheduler()
store.dependencies.fileClient.save = { @Sendable _, _ in
await didSaveUserSettings.setValue(true)
}
store.dependencies.userDefaultsClient.setBool = { @Sendable value, _ in
await didSaveObserverPromptSetting.setValue(value)
}

await store.send(.setObservationMode(false))

await didSaveUserSettings.withValue { val in
XCTAssertTrue(val, "Expected that save is invoked")
}
await didSaveObserverPromptSetting.withValue { val in
XCTAssertTrue(val, "Expected to store that user prompt has been seen")
}
}

func test_viewingModePrompt() async throws {
let didSetDidShowPrompt = ActorIsolated(false)

Expand All @@ -442,18 +416,13 @@ final class AppFeatureTests: XCTestCase {

func test_postLocation_shouldNotPostLocationWhenObserverModeIsEnabled() async {
var state = AppFeature.State()
state.settingsState.userSettings.isObservationModeEnabled = true
state.settingsState.isObservationModeEnabled = true

let store = TestStore(
initialState: state,
reducer: AppFeature()
)
store.dependencies.date = .init({ @Sendable in self.date() })

let location = ComposableCoreLocation.Location(
coordinate: .init(latitude: 11, longitude: 21),
timestamp: Date(timeIntervalSince1970: 2)
)
await store.send(.postLocation)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ final class IdentifiedMessagesTests: XCTestCase {
)!

func test_chatTime_Format() {
let sut = ChatMessage(
identifier: "ID",
device: "DEVICE",
message: "Hello World",
timestamp: date.timeIntervalSince1970
)

var cal = Calendar.current
cal.timeZone = .init(secondsFromGMT: 0)!
let chatTime = date.formatted(Date.FormatStyle.chatTime(cal))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ final class NextRideCoreTests: XCTestCase {
initialState: .init(),
reducer: NextRideFeature()
)
store.dependencies.date = .init(now)
store.dependencies.date = .init({ @Sendable in self.now() })
store.dependencies.userDefaultsClient.dataForKey = { _ in
try? RideEventSettings(
isEnabled: false,
typeSettings: [],
typeSettings: [:],
eventDistance: .near
)
.encoded()
Expand Down Expand Up @@ -116,7 +116,7 @@ final class NextRideCoreTests: XCTestCase {
store.dependencies.userDefaultsClient.dataForKey = { _ in
try? RideEventSettings(
isEnabled: true,
typeSettings: [],
typeSettings: [:],
eventDistance: .near
)
.encoded()
Expand Down Expand Up @@ -144,9 +144,7 @@ final class NextRideCoreTests: XCTestCase {
store.dependencies.userDefaultsClient.dataForKey = { _ in
try? RideEventSettings(
isEnabled: true,
typeSettings: [
RideEventSettings.RideEventTypeSetting(type: Ride.RideType.kidicalMass, isEnabled: true)
],
typeSettings: [.kidicalMass: true],
eventDistance: .near
)
.encoded()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,50 @@ import XCTest
final class RideEventSettingsCoreTests: XCTestCase {
func test_setRideEventsEnabled() {
let store = TestStore(
initialState: RideEventSettings(
isEnabled: true,
typeSettings: .all,
eventDistance: .close
initialState: .init(
settings: RideEventSettings(
isEnabled: true,
typeSettings: .all(),
eventDistance: .close
)
),
reducer: RideEventsSettingsFeature()
)

store.send(.setRideEventsEnabled(false)) {
store.send(.set(\.$isEnabled, false)) {
$0.isEnabled = false
}

store.send(.setRideEventsEnabled(true)) {
store.send(.set(\.$isEnabled, true)) {
$0.isEnabled = true
}
}

func test_setRideEventsTypeEnabled() {
let store = TestStore(
initialState: RideEventSettings(
isEnabled: true,
typeSettings: .all,
eventDistance: .close
),
reducer: RideEventsSettingsFeature()
initialState: RideEventType.State(rideType: .criticalMass, isEnabled: false),
reducer: RideEventType()
)

var updatedType = RideEventSettings.RideEventTypeSetting(type: .kidicalMass, isEnabled: false)
store.send(.setRideEventTypeEnabled(updatedType)) {
var updatedSettings: [RideEventSettings.RideEventTypeSetting] = .all
let index = try XCTUnwrap(updatedSettings.firstIndex(where: { setting in
setting.type == updatedType.type
}))
updatedSettings[index] = updatedType
$0.typeSettings = updatedSettings
}

updatedType.isEnabled = true
store.send(.setRideEventTypeEnabled(updatedType)) {
$0.typeSettings = .all
store.send(.set(\.$isEnabled, true)) {
$0.isEnabled = true
}
}

func test_setRideEventsRadius() {
let store = TestStore(
initialState: RideEventSettings(
isEnabled: true,
typeSettings: .all,
eventDistance: .close
initialState: .init(
settings: RideEventSettings(
isEnabled: true,
typeSettings: .all(),
eventDistance: .close
)
),
reducer: RideEventsSettingsFeature()
)

store.send(.setRideEventRadius(.near)) {
$0.eventDistance = .near
store.send(.set(\.$eventSearchRadius, .near)) {
$0.eventSearchRadius = .near
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ final class RideEventSettingsViewSnapshotTests: XCTestCase {
func test_rideEventSettingsView_light() {
let settingsView = RideEventSettingsView(
store: .init(
initialState: RideEventSettings(
isEnabled: true,
typeSettings: .all,
eventDistance: .close
initialState: .init(
settings: RideEventSettings(
isEnabled: true,
typeSettings: .all(),
eventDistance: .close
)
),
reducer: RideEventsSettingsFeature()
)
Expand All @@ -22,10 +24,12 @@ final class RideEventSettingsViewSnapshotTests: XCTestCase {
func test_rideEventSettingsView_disabled() {
let settingsView = RideEventSettingsView(
store: .init(
initialState: RideEventSettings(
isEnabled: false,
typeSettings: .all,
eventDistance: .close
initialState: .init(
settings: RideEventSettings(
isEnabled: true,
typeSettings: .all(),
eventDistance: .close
)
),
reducer: RideEventsSettingsFeature()
)
Expand Down
Loading

0 comments on commit a0e7acd

Please sign in to comment.