metatext/ViewModels/Tests/ViewModelsTests/AddIdentityViewModelTests.swift

69 lines
2.3 KiB
Swift
Raw Normal View History

// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import CombineExpectations
2020-08-31 01:40:58 +00:00
import HTTP
2020-08-31 00:18:19 +00:00
import Mastodon
2020-09-04 00:54:05 +00:00
import MockKeychain
2020-08-31 18:57:02 +00:00
import ServiceLayer
2020-09-01 07:33:49 +00:00
import ServiceLayerMocks
@testable import ViewModels
2020-09-05 02:31:43 +00:00
import XCTest
2020-08-31 10:21:01 +00:00
class AddIdentityViewModelTests: XCTestCase {
func testAddIdentity() throws {
2020-09-01 07:33:49 +00:00
let sut = AddIdentityViewModel(allIdentitiesService: try AllIdentitiesService(environment: .mock()))
2020-08-14 01:59:17 +00:00
let addedIDRecorder = sut.addedIdentityID.record()
sut.urlFieldText = "https://mastodon.social"
sut.logInTapped()
2020-08-14 01:59:17 +00:00
_ = try wait(for: addedIDRecorder.next(), timeout: 1)
}
func testAddIdentityWithoutScheme() throws {
2020-09-01 07:33:49 +00:00
let sut = AddIdentityViewModel(allIdentitiesService: try AllIdentitiesService(environment: .mock()))
2020-08-14 01:59:17 +00:00
let addedIDRecorder = sut.addedIdentityID.record()
sut.urlFieldText = "mastodon.social"
sut.logInTapped()
2020-08-14 01:59:17 +00:00
_ = try wait(for: addedIDRecorder.next(), timeout: 1)
}
func testInvalidURL() throws {
2020-09-01 07:33:49 +00:00
let sut = AddIdentityViewModel(allIdentitiesService: try AllIdentitiesService(environment: .mock()))
2020-08-02 22:23:01 +00: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-02 22:23:01 +00:00
func testDoesNotAlertCanceledLogin() throws {
let environment = AppEnvironment(
2020-08-12 07:24:39 +00:00
session: Session(configuration: .stubbing),
webAuthSessionType: CanceledLoginMockWebAuthSession.self,
2020-09-04 00:54:05 +00:00
keychain: MockKeychain.self,
2020-08-26 21:35:06 +00:00
userDefaults: MockUserDefaults(),
2020-09-01 07:33:49 +00:00
userNotificationClient: .mock,
inMemoryContent: true,
identityFixture: nil)
2020-08-31 10:21:01 +00:00
let allIdentitiesService = try AllIdentitiesService(environment: environment)
2020-08-26 21:35:06 +00:00
let sut = AddIdentityViewModel(allIdentitiesService: allIdentitiesService)
2020-08-02 22:23:01 +00:00
let recorder = sut.$alertItem.record()
XCTAssertNil(try wait(for: recorder.next(), timeout: 1))
sut.urlFieldText = "https://mastodon.social"
sut.logInTapped()
2020-08-02 22:23:01 +00:00
try wait(for: recorder.next().inverted, timeout: 1)
}
}