forked from nalexn/clean-architecture-swiftui
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for user permissions interactor and push token repo
- Loading branch information
Alexey Naumov
committed
Apr 26, 2020
1 parent
82044c2
commit 79c6771
Showing
7 changed files
with
178 additions
and
7 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
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
109 changes: 109 additions & 0 deletions
109
UnitTests/Interactors/UserPermissionsInteractorTests.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,109 @@ | ||
// | ||
// UserPermissionsInteractorTests.swift | ||
// UnitTests | ||
// | ||
// Created by Alexey Naumov on 26.04.2020. | ||
// Copyright © 2020 Alexey Naumov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
@testable import CountriesSwiftUI | ||
|
||
class UserPermissionsInteractorTests: XCTestCase { | ||
|
||
var state = Store<AppState>(AppState()) | ||
var sut: RealUserPermissionsInteractor! | ||
|
||
override func setUp() { | ||
state.bulkUpdate { $0 = AppState() } | ||
} | ||
|
||
func delay(_ closure: @escaping () -> Void) { | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: closure) | ||
} | ||
|
||
func test_noSideEffectOnInit() { | ||
let exp = XCTestExpectation(description: #function) | ||
sut = RealUserPermissionsInteractor(appState: state) { | ||
XCTFail() | ||
} | ||
delay { | ||
XCTAssertEqual(self.state.value, AppState()) | ||
exp.fulfill() | ||
} | ||
wait(for: [exp], timeout: 0.3) | ||
} | ||
|
||
// MARK: - Push | ||
|
||
func test_pushFirstResolveStatus() { | ||
XCTAssertEqual(AppState().permissions.push, .unknown) | ||
let exp = XCTestExpectation(description: #function) | ||
sut = RealUserPermissionsInteractor(appState: state) { | ||
XCTFail() | ||
} | ||
sut.resolveStatus(for: .pushNotifications) | ||
delay { | ||
XCTAssertNotEqual(self.state.value.permissions.push, .unknown) | ||
exp.fulfill() | ||
} | ||
wait(for: [exp], timeout: 0.3) | ||
} | ||
|
||
func test_pushSecondResolveStatus() { | ||
XCTAssertEqual(AppState().permissions.push, .unknown) | ||
let exp = XCTestExpectation(description: #function) | ||
sut = RealUserPermissionsInteractor(appState: state) { | ||
XCTFail() | ||
} | ||
sut.resolveStatus(for: .pushNotifications) | ||
delay { | ||
self.sut.resolveStatus(for: .pushNotifications) | ||
XCTAssertNotEqual(self.state.value.permissions.push, .unknown) | ||
exp.fulfill() | ||
} | ||
wait(for: [exp], timeout: 0.3) | ||
} | ||
|
||
func test_pushRequestPermissionNotDetermined() { | ||
state[\.permissions.push] = .notRequested | ||
let exp = XCTestExpectation(description: #function) | ||
sut = RealUserPermissionsInteractor(appState: state) { | ||
XCTFail() | ||
} | ||
sut.request(permission: .pushNotifications) | ||
delay { | ||
XCTAssertNotEqual(self.state.value.permissions.push, .unknown) | ||
exp.fulfill() | ||
} | ||
wait(for: [exp], timeout: 0.3) | ||
} | ||
|
||
func test_pushRequestPermissionDenied() { | ||
state[\.permissions.push] = .denied | ||
let exp = XCTestExpectation(description: #function) | ||
sut = RealUserPermissionsInteractor(appState: state) { | ||
XCTAssertEqual(self.state.value.permissions.push, .denied) | ||
exp.fulfill() | ||
} | ||
sut.request(permission: .pushNotifications) | ||
wait(for: [exp], timeout: 0.3) | ||
} | ||
|
||
func test_authorizationStatusMapping() { | ||
XCTAssertEqual(UNAuthorizationStatus.notDetermined.map, .notRequested) | ||
XCTAssertEqual(UNAuthorizationStatus.provisional.map, .notRequested) | ||
XCTAssertEqual(UNAuthorizationStatus.denied.map, .denied) | ||
XCTAssertEqual(UNAuthorizationStatus.authorized.map, .granted) | ||
XCTAssertEqual(UNAuthorizationStatus(rawValue: 10)?.map, .notRequested) | ||
} | ||
|
||
// MARK: - Stub | ||
|
||
func test_stubUserPermissionsInteractor() { | ||
let sut = StubUserPermissionsInteractor() | ||
sut.request(permission: .pushNotifications) | ||
sut.resolveStatus(for: .pushNotifications) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// PushTokenWebRepositoryTests.swift | ||
// UnitTests | ||
// | ||
// Created by Alexey Naumov on 26.04.2020. | ||
// Copyright © 2020 Alexey Naumov. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import Combine | ||
@testable import CountriesSwiftUI | ||
|
||
class PushTokenWebRepositoryTests: XCTestCase { | ||
|
||
private var sut: RealPushTokenWebRepository! | ||
private var cancelBag = CancelBag() | ||
|
||
override func setUp() { | ||
sut = RealPushTokenWebRepository(session: .mockedResponsesOnly, | ||
baseURL: "https://test.com") | ||
} | ||
|
||
override func tearDown() { | ||
cancelBag = CancelBag() | ||
} | ||
|
||
func test_register() { | ||
let exp = XCTestExpectation(description: #function) | ||
sut.register(devicePushToken: Data()) | ||
.sinkToResult { result in | ||
result.assertSuccess() | ||
exp.fulfill() | ||
} | ||
.store(in: cancelBag) | ||
wait(for: [exp], timeout: 0.1) | ||
} | ||
} |
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