2024-08-06 15:46:56 +02:00
|
|
|
//
|
2024-09-06 16:34:30 +03:00
|
|
|
// Copyright 2024 New Vector Ltd.
|
2024-08-06 15:46:56 +02:00
|
|
|
//
|
2025-01-06 11:27:37 +01:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
|
|
|
// Please see LICENSE files in the repository root for full details.
|
2024-08-06 15:46:56 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
|
|
|
|
@testable import ElementX
|
|
|
|
|
|
|
|
class PinnedEventsBannerStateTests: XCTestCase {
|
|
|
|
func testEmpty() {
|
|
|
|
var state = PinnedEventsBannerState.loading(numbersOfEvents: 0)
|
|
|
|
XCTAssertTrue(state.isEmpty)
|
|
|
|
|
|
|
|
state = .loaded(state: .init())
|
|
|
|
XCTAssertTrue(state.isEmpty)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoading() {
|
|
|
|
let originalState = PinnedEventsBannerState.loading(numbersOfEvents: 5)
|
|
|
|
|
|
|
|
var state = originalState
|
|
|
|
// This should not affect the state when loading
|
2024-08-07 16:01:01 +02:00
|
|
|
state.previousPin()
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state, originalState)
|
|
|
|
|
|
|
|
XCTAssertTrue(state.isLoading)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertNil(state.selectedPinnedEventID)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, L10n.screenRoomPinnedBannerLoadingDescription)
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 4)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.count, 5)
|
|
|
|
XCTAssertEqual(state.bannerIndicatorDescription.string, L10n.screenRoomPinnedBannerIndicatorDescription(L10n.screenRoomPinnedBannerIndicator(5, 5)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoadingToLoaded() {
|
|
|
|
var state = PinnedEventsBannerState.loading(numbersOfEvents: 2)
|
|
|
|
XCTAssertTrue(state.isLoading)
|
|
|
|
state.setPinnedEventContents(["1": "test1", "2": "test2"])
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state, .loaded(state: .init(pinnedEventContents: ["1": "test1", "2": "test2"], selectedPinnedEventID: "2")))
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertFalse(state.isLoading)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testLoaded() {
|
2024-08-22 17:35:44 +02:00
|
|
|
let state = PinnedEventsBannerState.loaded(state: .init(pinnedEventContents: ["1": "test1", "2": "test2"], selectedPinnedEventID: "2"))
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertFalse(state.isLoading)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "2")
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test2")
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 1)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.count, 2)
|
|
|
|
XCTAssertEqual(state.bannerIndicatorDescription.string, L10n.screenRoomPinnedBannerIndicatorDescription(L10n.screenRoomPinnedBannerIndicator(2, 2)))
|
|
|
|
}
|
|
|
|
|
2024-08-07 16:01:01 +02:00
|
|
|
func testPreviousPin() {
|
2024-08-22 17:35:44 +02:00
|
|
|
var state = PinnedEventsBannerState.loaded(state: .init(pinnedEventContents: ["1": "test1", "2": "test2", "3": "test3"], selectedPinnedEventID: "1"))
|
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "1")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 0)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test1")
|
|
|
|
|
2024-08-07 16:01:01 +02:00
|
|
|
state.previousPin()
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "3")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 2)
|
2024-08-07 16:01:01 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test3")
|
|
|
|
|
|
|
|
state.previousPin()
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "2")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 1)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test2")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testSetContent() {
|
2024-08-22 17:35:44 +02:00
|
|
|
var state = PinnedEventsBannerState.loaded(state: .init(pinnedEventContents: ["1": "test1", "2": "test2", "3": "test3", "4": "test4"], selectedPinnedEventID: "2"))
|
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "2")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 1)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test2")
|
|
|
|
XCTAssertEqual(state.count, 4)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
|
|
|
|
|
|
|
// let's remove the selected item
|
|
|
|
state.setPinnedEventContents(["1": "test1", "3": "test3", "4": "test4"])
|
|
|
|
// new selected item is the new latest
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "4")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 2)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test4")
|
|
|
|
XCTAssertEqual(state.count, 3)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
|
|
|
|
|
|
|
// let's add a new item at the top
|
|
|
|
state.setPinnedEventContents(["0": "test0", "1": "test1", "3": "test3", "4": "test4"])
|
|
|
|
// selected item doesn't change
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "4")
|
2024-08-06 15:46:56 +02:00
|
|
|
// but the index is updated
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 3)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test4")
|
|
|
|
XCTAssertEqual(state.count, 4)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
|
|
|
|
|
|
|
// let's add a new item at the bottom
|
|
|
|
state.setPinnedEventContents(["0": "test0", "1": "test1", "3": "test3", "4": "test4", "5": "test5"])
|
|
|
|
// selected item doesn't change
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "4")
|
2024-08-06 15:46:56 +02:00
|
|
|
// and index stays the same
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 3)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test4")
|
|
|
|
XCTAssertEqual(state.count, 5)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
|
|
|
|
|
|
|
// set to tempty
|
|
|
|
state.setPinnedEventContents([:])
|
|
|
|
XCTAssertTrue(state.isEmpty)
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertNil(state.selectedPinnedEventID)
|
2024-08-06 15:46:56 +02:00
|
|
|
|
|
|
|
// set to one item
|
|
|
|
state.setPinnedEventContents(["6": "test6", "7": "test7"])
|
2024-08-22 17:35:44 +02:00
|
|
|
XCTAssertEqual(state.selectedPinnedEventID, "7")
|
|
|
|
XCTAssertEqual(state.selectedPinnedIndex, 1)
|
2024-08-06 15:46:56 +02:00
|
|
|
XCTAssertEqual(state.displayedMessage.string, "test7")
|
|
|
|
XCTAssertEqual(state.count, 2)
|
|
|
|
XCTAssertFalse(state.isEmpty)
|
|
|
|
}
|
|
|
|
}
|