element-ios/RiotSwiftUI/Modules/Template/TemplateAdvancedRoomsExample/TemplateRoomChat/Test/Unit/TemplateRoomChatViewModelTests.swift

63 lines
2.4 KiB
Swift
Raw Normal View History

2021-09-12 13:57:45 +00:00
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import XCTest
import Combine
@testable import RiotSwiftUI
@available(iOS 14.0, *)
class TemplateRoomChatViewModelTests: XCTestCase {
2021-09-20 14:56:53 +00:00
2021-09-12 13:57:45 +00:00
var service: MockTemplateRoomChatService!
var viewModel: TemplateRoomChatViewModel!
2021-09-20 14:56:53 +00:00
var context: TemplateRoomChatViewModel.Context!
2021-09-12 13:57:45 +00:00
var cancellables = Set<AnyCancellable>()
2021-09-20 14:56:53 +00:00
2021-09-12 13:57:45 +00:00
override func setUpWithError() throws {
2021-09-20 14:56:53 +00:00
service = MockTemplateRoomChatService()
service.simulateUpdate(initializationStatus: .initialized)
2021-09-12 13:57:45 +00:00
viewModel = TemplateRoomChatViewModel(templateRoomChatService: service)
2021-09-20 14:56:53 +00:00
context = viewModel.context
2021-09-12 13:57:45 +00:00
}
func testInitialState() {
2021-09-20 14:56:53 +00:00
XCTAssertEqual(context.viewState.bubbles.count, 3)
XCTAssertEqual(context.viewState.sendButtonEnabled, false)
XCTAssertEqual(context.viewState.roomInitializationStatus, .initialized)
2021-09-12 13:57:45 +00:00
}
2021-09-20 14:56:53 +00:00
func testSendMessageUpdatesReceived() throws {
let bubblesPublisher: AnyPublisher<[[TemplateRoomChatBubble]], Never> = context.$viewState.map(\.bubbles).removeDuplicates().collect(2).first().eraseToAnyPublisher()
let awaitDeferred = xcAwaitDeferred(bubblesPublisher)
let newMessage: String = "Let's Go"
service.send(textMessage: newMessage)
let result: [[TemplateRoomChatBubble]]? = try awaitDeferred()
// Test that the update to the messages in turn updates the view's
// the last bubble by appending another text item, asserting the body.
guard let item:TemplateRoomChatBubbleItem = result?.last?.last?.items.last,
case TemplateRoomChatBubbleItemContent.message(let message) = item.content,
case let TemplateRoomChatMessageContent.text(text) = message else {
XCTFail()
return
}
XCTAssertEqual(text.body, newMessage)
2021-09-12 13:57:45 +00:00
}
}