2020-07-30 01:50:30 +02:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
import Combine
|
|
|
|
import CombineExpectations
|
|
|
|
@testable import Metatext
|
|
|
|
|
|
|
|
class AddIdentityViewModelTests: XCTestCase {
|
|
|
|
func testAddIdentity() throws {
|
2020-08-03 00:23:01 +02:00
|
|
|
let environment = AppEnvironment.fresh()
|
2020-08-09 07:37:04 +02:00
|
|
|
let sut = AddIdentityViewModel(authenticationService: AuthenticationService(environment: environment))
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIDRecorder = sut.addedIdentityID.record()
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "https://mastodon.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentityID = try wait(for: addedIDRecorder.next(), timeout: 1)
|
2020-08-03 00:23:01 +02:00
|
|
|
let identityRecorder = environment.identityDatabase.identityObservation(id: addedIdentityID).record()
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentity = try wait(for: identityRecorder.next(), timeout: 1)
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
XCTAssertEqual(addedIdentity.url, URL(string: "https://mastodon.social")!)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddIdentityWithoutScheme() throws {
|
2020-08-03 00:23:01 +02:00
|
|
|
let environment = AppEnvironment.fresh()
|
2020-08-09 07:37:04 +02:00
|
|
|
let sut = AddIdentityViewModel(authenticationService: AuthenticationService(environment: environment))
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIDRecorder = sut.addedIdentityID.record()
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "mastodon.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentityID = try wait(for: addedIDRecorder.next(), timeout: 1)
|
2020-08-03 00:23:01 +02:00
|
|
|
let identityRecorder = environment.identityDatabase.identityObservation(id: addedIdentityID).record()
|
2020-08-03 17:20:51 +02:00
|
|
|
let addedIdentity = try wait(for: identityRecorder.next(), timeout: 1)
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
XCTAssertEqual(addedIdentity.url, URL(string: "https://mastodon.social")!)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testInvalidURL() throws {
|
2020-08-09 07:37:04 +02:00
|
|
|
let sut = AddIdentityViewModel(authenticationService: AuthenticationService(environment: .fresh()))
|
2020-08-03 00:23:01 +02:00
|
|
|
let recorder = sut.$alertItem.record()
|
|
|
|
|
|
|
|
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
|
2020-07-30 01:50:30 +02:00
|
|
|
|
|
|
|
sut.urlFieldText = "🐘.social"
|
|
|
|
sut.goTapped()
|
|
|
|
|
|
|
|
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 {
|
2020-08-09 03:37:46 +02:00
|
|
|
let environment = AppEnvironment.fresh(webAuthSessionType: CanceledLoginMockWebAuthSession.self)
|
2020-08-09 07:37:04 +02:00
|
|
|
let sut = AddIdentityViewModel(authenticationService: AuthenticationService(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.goTapped()
|
|
|
|
|
|
|
|
try wait(for: recorder.next().inverted, timeout: 1)
|
|
|
|
}
|
2020-07-30 01:50:30 +02:00
|
|
|
}
|