metatext-app-ios-iphone-ipad/ViewModels/Tests/ViewModelsTests/AddIdentityViewModelTests.s...

76 lines
2.8 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import CombineExpectations
2020-08-31 03:40:58 +02:00
import HTTP
2020-08-31 02:18:19 +02:00
import Mastodon
2020-09-04 02:54:05 +02:00
import MockKeychain
2020-08-31 20:57:02 +02:00
import ServiceLayer
2020-09-01 09:33:49 +02:00
import ServiceLayerMocks
@testable import ViewModels
2020-09-05 04:31:43 +02:00
import XCTest
2020-08-31 12:21:01 +02:00
2020-11-09 07:22:20 +01:00
final class AddIdentityViewModelTests: XCTestCase {
func testAddIdentity() throws {
2020-09-13 02:50:22 +02:00
let uuid = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!
let environment = AppEnvironment.mock(uuid: { uuid })
let allIdentitiesService = try AllIdentitiesService(environment: environment)
2020-09-09 14:05:43 +02:00
let sut = AddIdentityViewModel(
2020-09-13 02:50:22 +02:00
allIdentitiesService: allIdentitiesService,
2020-09-10 06:51:31 +02:00
instanceURLService: InstanceURLService(environment: environment))
2020-10-06 00:50:05 +02:00
let addedIdRecorder = allIdentitiesService.identitiesCreated.record()
sut.urlFieldText = "https://mastodon.social"
sut.logInTapped()
2020-10-06 00:50:05 +02:00
_ = try wait(for: addedIdRecorder.next(), timeout: 1)
}
func testAddIdentityWithoutScheme() throws {
2020-09-13 02:50:22 +02:00
let uuid = UUID(uuidString: "E621E1F8-C36C-495A-93FC-0C247A3E6E5F")!
let environment = AppEnvironment.mock(uuid: { uuid })
let allIdentitiesService = try AllIdentitiesService(environment: environment)
2020-09-09 14:05:43 +02:00
let sut = AddIdentityViewModel(
2020-09-13 02:50:22 +02:00
allIdentitiesService: allIdentitiesService,
2020-09-10 06:51:31 +02:00
instanceURLService: InstanceURLService(environment: environment))
2020-10-06 00:50:05 +02:00
let addedIdRecorder = allIdentitiesService.identitiesCreated.record()
sut.urlFieldText = "mastodon.social"
sut.logInTapped()
2020-10-06 00:50:05 +02:00
_ = try wait(for: addedIdRecorder.next(), timeout: 1)
}
func testInvalidURL() throws {
2020-09-09 14:05:43 +02:00
let environment = AppEnvironment.mock()
let sut = AddIdentityViewModel(
allIdentitiesService: try AllIdentitiesService(environment: environment),
2020-09-10 06:51:31 +02:00
instanceURLService: InstanceURLService(environment: environment))
2020-08-03 00:23:01 +02:00
let recorder = sut.$alertItem.record()
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
sut.urlFieldText = "🐘.social"
sut.logInTapped()
let alertItem = try wait(for: recorder.next(), timeout: 1)
2020-09-10 06:51:31 +02:00
XCTAssertEqual((alertItem?.error as? AddIdentityError), AddIdentityError.unableToConnectToInstance)
}
2020-08-03 00:23:01 +02:00
func testDoesNotAlertCanceledLogin() throws {
2020-09-09 14:05:43 +02:00
let environment = AppEnvironment.mock(webAuthSessionType: CanceledLoginMockWebAuthSession.self)
let sut = AddIdentityViewModel(
allIdentitiesService: try AllIdentitiesService(environment: environment),
2020-09-10 06:51:31 +02:00
instanceURLService: InstanceURLService(environment: environment))
2020-08-03 00:23:01 +02:00
let recorder = sut.$alertItem.record()
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
sut.urlFieldText = "https://mastodon.social"
sut.logInTapped()
2020-08-03 00:23:01 +02:00
try wait(for: recorder.next().inverted, timeout: 1)
}
}