Skip to content

Commit

Permalink
Refactor unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alfogrillo committed Feb 2, 2023
1 parent e5e4035 commit a2b1177
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 48 deletions.
45 changes: 27 additions & 18 deletions Riot/Managers/PushRulesUpdater/PushRulesUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ final class PushRulesUpdater {
private var cancellables: Set<AnyCancellable> = .init()
private var rules: [NotificationPushRuleType] = []
private let notificationSettingsService: NotificationSettingsServiceType
private let didCompleteUpdateSubject: PassthroughSubject<Void, Never> = .init()

var didCompleteUpdate: AnyPublisher<Void, Never> {
didCompleteUpdateSubject.eraseToAnyPublisher()
}

init(notificationSettingsService: NotificationSettingsServiceType, needsCheck: AnyPublisher<Void, Never>) {
self.notificationSettingsService = notificationSettingsService
Expand All @@ -39,31 +44,35 @@ final class PushRulesUpdater {

private extension PushRulesUpdater {
func syncRulesIfNeeded() {
for rule in rules {
syncRelatedRulesIfNeeded(for: rule)
}
}

func syncRelatedRulesIfNeeded(for rule: NotificationPushRuleType) {
guard let ruleId = rule.pushRuleId else {
return
}
let dispatchGroup: DispatchGroup = .init()

let relatedRules = ruleId.syncedRules(in: rules)

for relatedRule in relatedRules {
guard rule.hasSameContentOf(relatedRule) == false else {
for rule in rules {
guard let ruleId = rule.pushRuleId else {
continue
}

sync(relatedRuleId: relatedRule.ruleId, with: rule)
let relatedRules = ruleId.syncedRules(in: rules)

for relatedRule in relatedRules {
guard rule.hasSameContentOf(relatedRule) == false else {
continue
}

dispatchGroup.enter()
Task {
try? await sync(relatedRuleId: relatedRule.ruleId, with: rule)
dispatchGroup.leave()
}
}
}

dispatchGroup.notify(queue: .main) { [weak self] in
self?.didCompleteUpdateSubject.send(())
}
}

func sync(relatedRuleId: String, with rule: NotificationPushRuleType) {
Task {
try? await notificationSettingsService.updatePushRuleActions(for: relatedRuleId, enabled: rule.enabled, actions: rule.ruleActions)
}
func sync(relatedRuleId: String, with rule: NotificationPushRuleType) async throws {
try await notificationSettingsService.updatePushRuleActions(for: relatedRuleId, enabled: rule.enabled, actions: rule.ruleActions)
}
}

Expand Down
74 changes: 44 additions & 30 deletions RiotTests/PushRulesUpdaterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ final class PushRulesUpdaterTests: XCTestCase {
private var notificationService: MockNotificationSettingsService!
private var pushRulesUpdater: PushRulesUpdater!
private var needsCheckPublisher: PassthroughSubject<Void, Never> = .init()
private var subscriptions: Set<AnyCancellable> = .init()

override func setUpWithError() throws {
notificationService = .init()
notificationService.rules = [MockNotificationPushRule].default
pushRulesUpdater = .init(notificationSettingsService: notificationService, needsCheck: needsCheckPublisher.eraseOutput())
}

override func tearDownWithError() throws {
subscriptions.removeAll()
}

func testNoRuleIsUpdated() throws {
needsCheckPublisher.send()
Expand All @@ -42,11 +47,14 @@ final class PushRulesUpdaterTests: XCTestCase {

needsCheckPublisher.send(())

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
XCTAssertEqual(self.notificationService.rules[targetRuleIndex].ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
XCTAssertTrue(self.notificationService.rules[targetRuleIndex].enabled)
expectation.fulfill()
}
pushRulesUpdater
.didCompleteUpdate
.sink { _ in
XCTAssertEqual(self.notificationService.rules[targetRuleIndex].ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
XCTAssertTrue(self.notificationService.rules[targetRuleIndex].enabled)
expectation.fulfill()
}
.store(in: &subscriptions)

waitForExpectations(timeout: 2.0)
}
Expand All @@ -60,21 +68,24 @@ final class PushRulesUpdaterTests: XCTestCase {

needsCheckPublisher.send(())

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
for rule in self.notificationService.rules {
guard let id = rule.pushRuleId else {
continue
}

if affectedRules.contains(id) {
XCTAssertEqual(rule.ruleActions, targetActions)
} else {
XCTAssertEqual(rule.ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
pushRulesUpdater
.didCompleteUpdate
.sink { _ in
for rule in self.notificationService.rules {
guard let id = rule.pushRuleId else {
continue
}

if affectedRules.contains(id) {
XCTAssertEqual(rule.ruleActions, targetActions)
} else {
XCTAssertEqual(rule.ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
}
}
expectation.fulfill()
}
expectation.fulfill()
}

.store(in: &subscriptions)

waitForExpectations(timeout: 2.0)
}

Expand All @@ -87,20 +98,23 @@ final class PushRulesUpdaterTests: XCTestCase {

needsCheckPublisher.send(())

DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
for rule in self.notificationService.rules {
guard let id = rule.pushRuleId else {
continue
}

if affectedRules.contains(id) {
XCTAssertEqual(rule.ruleActions, targetActions)
} else {
XCTAssertEqual(rule.ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
pushRulesUpdater
.didCompleteUpdate
.sink { _ in
for rule in self.notificationService.rules {
guard let id = rule.pushRuleId else {
continue
}

if affectedRules.contains(id) {
XCTAssertEqual(rule.ruleActions, targetActions)
} else {
XCTAssertEqual(rule.ruleActions, NotificationStandardActions.notifyDefaultSound.actions)
}
}
expectation.fulfill()
}
expectation.fulfill()
}
.store(in: &subscriptions)

waitForExpectations(timeout: 2.0)
}
Expand Down

0 comments on commit a2b1177

Please sign in to comment.