Skip to content

Commit

Permalink
Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alfogrillo committed Jan 30, 2023
1 parent 8b39dbc commit 731b343
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class MockNotificationSettingsService: NotificationSettingsServiceType, Observab

func updatePushRuleActions(for ruleId: String, enabled: Bool, actions: NotificationActions?, completion: ((Result<Void, Error>) -> Void)?) {
guard let ruleIndex = rules.firstIndex(where: { $0.ruleId == ruleId }) else {
completion?(.failure(NSError(domain: "fake", code: 0)))
completion?(.success(()))
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ final class NotificationSettingsViewModelTests: XCTestCase {
let expectation = expectation(description: #function)
setupWithPollRules()

viewModel.update(ruleID: .oneToOneRoom, isChecked: false)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
viewModel.update(ruleID: .oneToOneRoom, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOneRoom], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.oneToOnePollStart], false)
Expand All @@ -68,9 +71,12 @@ final class NotificationSettingsViewModelTests: XCTestCase {
let expectation = expectation(description: #function)
setupWithPollRules()

viewModel.update(ruleID: .allOtherMessages, isChecked: false)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
viewModel.update(ruleID: .allOtherMessages, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

XCTAssertEqual(self.viewModel.viewState.selectionState.count, 8)
XCTAssertEqual(self.viewModel.viewState.selectionState[.allOtherMessages], false)
XCTAssertEqual(self.viewModel.viewState.selectionState[.pollStart], false)
Expand All @@ -91,9 +97,12 @@ final class NotificationSettingsViewModelTests: XCTestCase {
let expectation = expectation(description: #function)
setupWithPollRules()

viewModel.update(ruleID: .allOtherMessages, isChecked: false)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
viewModel.update(ruleID: .allOtherMessages, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

// simulating a "mismatch" on the poll started rule
self.viewModel.update(ruleID: .pollStart, isChecked: true)

Expand All @@ -112,9 +121,12 @@ final class NotificationSettingsViewModelTests: XCTestCase {
let expectation = expectation(description: #function)
setupWithPollRules()

viewModel.update(ruleID: .oneToOneRoom, isChecked: false)

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
viewModel.update(ruleID: .oneToOneRoom, isChecked: false) { result in
guard case .success = result else {
XCTFail()
return
}

// simulating a "mismatch" on the one to one poll started rule
self.viewModel.update(ruleID: .oneToOnePollStart, isChecked: true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ final class NotificationSettingsViewModel: NotificationSettingsViewModelType, Ob

// MARK: - Public

func update(ruleID: NotificationPushRuleId, isChecked: Bool) {
func update(ruleID: NotificationPushRuleId, isChecked: Bool, completion: ((Result<Void, Error>) -> Void)? = nil) {
let index = NotificationIndex.index(when: isChecked)
let standardActions = ruleID.standardActions(for: index)
let enabled = standardActions != .disabled
Expand All @@ -119,15 +119,16 @@ final class NotificationSettingsViewModel: NotificationSettingsViewModelType, Ob
id: ruleID,
enabled: enabled,
standardActions: standardActions,
then: ruleID.syncedRules
then: ruleID.syncedRules,
completion: completion
)

default:
notificationSettingsService.updatePushRuleActions(
for: ruleID.rawValue,
enabled: enabled,
actions: standardActions.actions,
completion: nil
completion: completion
)
}
}
Expand Down Expand Up @@ -171,8 +172,8 @@ private extension NotificationSettingsViewModel {
func updatePushAction(id: NotificationPushRuleId,
enabled: Bool,
standardActions: NotificationStandardActions,
then rules: [NotificationPushRuleId]) {

then rules: [NotificationPushRuleId],
completion: ((Result<Void, Error>) -> Void)?) {
viewState.saving = true

Task {
Expand All @@ -187,18 +188,19 @@ private extension NotificationSettingsViewModel {
}

try await group.waitForAll()
await completeUpdate(error: nil)
await completeUpdate(completion: completion, result: .success(()))
}
} catch {
await completeUpdate(error: error)
await completeUpdate(completion: completion, result: .failure(error))
}
}
}

@MainActor
func completeUpdate(error: Error?) {
func completeUpdate(completion: ((Result<Void, Error>) -> Void)?, result: Result<Void, Error>) {
#warning("Handle error here in the next ticket")
viewState.saving = false
completion?(result)
}

func rulesUpdated(newRules: [NotificationPushRuleType]) {
Expand Down

0 comments on commit 731b343

Please sign in to comment.