metatext-app-ios-iphone-ipad/Tests/View Models/AddIdentityViewModelTests.s...

70 lines
2.5 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import XCTest
import Combine
import CombineExpectations
@testable import Metatext
class AddIdentityViewModelTests: XCTestCase {
func testAddIdentity() throws {
let identityDatabase = IdentityDatabase.fresh()
let sut = AddIdentityViewModel(identitiesService: .fresh(identityDatabase: identityDatabase))
2020-08-12 09:24:39 +02:00
let addedIDAndURLRecorder = sut.addedIdentityIDAndURL.record()
sut.urlFieldText = "https://mastodon.social"
sut.logInTapped()
2020-08-12 09:24:39 +02:00
let addedIdentityIDAndURL = try wait(for: addedIDAndURLRecorder.next(), timeout: 1)
2020-08-12 09:24:39 +02:00
// XCTAssertEqual(addedIdentityIDAndURL.0, addedIdentityID)
XCTAssertEqual(addedIdentityIDAndURL.1, URL(string: "https://mastodon.social")!)
}
func testAddIdentityWithoutScheme() throws {
let identityDatabase = IdentityDatabase.fresh()
let sut = AddIdentityViewModel(identitiesService: .fresh(identityDatabase: identityDatabase))
2020-08-12 09:24:39 +02:00
let addedIDAndURLRecorder = sut.addedIdentityIDAndURL.record()
sut.urlFieldText = "mastodon.social"
sut.logInTapped()
2020-08-12 09:24:39 +02:00
let addedIdentityIDAndURL = try wait(for: addedIDAndURLRecorder.next(), timeout: 1)
2020-08-12 09:24:39 +02:00
// XCTAssertEqual(addedIdentityIDAndURL.0, addedIdentityID)
XCTAssertEqual(addedIdentityIDAndURL.1, URL(string: "https://mastodon.social")!)
}
func testInvalidURL() throws {
let sut = AddIdentityViewModel(identitiesService: .fresh())
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)
XCTAssertEqual((alertItem?.error as? URLError)?.code, URLError.badURL)
}
2020-08-03 00:23:01 +02:00
func testDoesNotAlertCanceledLogin() throws {
let environment = AppEnvironment(
2020-08-12 09:24:39 +02:00
session: Session(configuration: .stubbing),
webAuthSessionType: CanceledLoginMockWebAuthSession.self,
keychainServiceType: MockKeychainService.self)
let identitiesService = IdentitiesService(
identityDatabase: .fresh(),
environment: environment)
let sut = AddIdentityViewModel(identitiesService: identitiesService)
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)
}
}