Files
letro-ios/UnitTests/Sources/EmojiProviderTests.swift
Stefan Ceriu 7eda122d20 Fix various flakey unit tests (#1783)
* Fix flakey emoji provider tests

* Fix flakey RoomScreenViewModel tests

* Fix flakey HomeScreenViewModel tests

* Fix flakey RoomMemberListScreen tests, problem with bindings getting overriden and deferFulfillment cancellable not getting stored

* Fix flakey RoomNotificationSettingsScreen tests and crashes

* Fix flakey RoomMemberDetailsScreen tests

* Deprecate old `deferFulfillment` and `nextViewState` methods

* Convert more files to the new `deferFulfillment`

* Converted the rest of the tests to the new deferFulfillment

* Removed now unused `nextViewState` and `deferFulfillment`

* Remove automatic retries from unit tests

* Reset analytics flag after running unit tests

* Address PR comments

* Introduce a new `deferFulfillment(publisher, keyPath, transitionValues)` method and use it where appropiate
2023-09-26 13:28:29 +03:00

105 lines
5.1 KiB
Swift

//
// Copyright 2022 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
@testable import ElementX
@MainActor
final class EmojiProviderTests: XCTestCase {
func testWhenEmojisLoadedCategoriesAreLoadedFromLoader() async throws {
let item = EmojiItem(label: "test", unicode: "test", keywords: ["1", "2"], shortcodes: ["1", "2"], skins: ["🙂"])
let category = EmojiCategory(id: "test", emojis: [item])
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = [category]
let emojiProvider = EmojiProvider(loader: emojiLoaderMock)
let categories = await emojiProvider.categories()
XCTAssertEqual(emojiLoaderMock.categories, categories)
}
func testWhenEmojisLoadedAndSearchStringEmptyAllCategoriesReturned() async throws {
let item = EmojiItem(label: "test", unicode: "test", keywords: ["1", "2"], shortcodes: ["1", "2"], skins: ["🙂"])
let category = EmojiCategory(id: "test", emojis: [item])
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = [category]
let emojiProvider = EmojiProvider(loader: emojiLoaderMock)
let categories = await emojiProvider.categories(searchString: "")
XCTAssertEqual(emojiLoaderMock.categories, categories)
}
func testWhenEmojisLoadedSecondTimeCachedValuesAreUsed() async throws {
let item = EmojiItem(label: "test", unicode: "test", keywords: ["1", "2"], shortcodes: ["1", "2"], skins: ["🙂"])
let item2 = EmojiItem(label: "test2", unicode: "test2", keywords: ["3", "4"], shortcodes: ["3", "4"], skins: ["🙂"])
let categoriesForFirstLoad = [EmojiCategory(id: "test",
emojis: [item])]
let categoriesForSecondLoad = [EmojiCategory(id: "test2",
emojis: [item2])]
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = categoriesForFirstLoad
let emojiProvider = EmojiProvider(loader: emojiLoaderMock)
_ = await emojiProvider.categories()
emojiLoaderMock.categories = categoriesForSecondLoad
let categories = await emojiProvider.categories()
XCTAssertEqual(categories, categoriesForFirstLoad)
}
func testWhenEmojisSearchedCorrectNumberOfCategoriesReturned() async throws {
let searchString = "smile"
var categories = [EmojiCategory]()
let item0WithSearchString = EmojiItem(label: "emoji0", unicode: "\(searchString)_123", keywords: ["key1", "key1"], shortcodes: ["key1", "key1"], skins: ["🙂"])
let item1WithSearchString = EmojiItem(label: searchString, unicode: "emoji1", keywords: ["key1", "key1"], shortcodes: ["key1", "key1"], skins: ["🙂"])
let item2WithSearchString = EmojiItem(label: "emoji_2", unicode: "emoji_2", keywords: ["key1", "\(searchString)_123"], shortcodes: ["key1", "key2"], skins: ["🙂"])
let item3WithSearchString = EmojiItem(label: "emoji_2", unicode: "emoji_2", keywords: ["key1", "key1"], shortcodes: ["key1", "\(searchString)_123"], skins: ["🙂"])
let item4WithoutSearchString = EmojiItem(label: "emoji_3", unicode: "emoji_3", keywords: ["key1", "key1"], shortcodes: ["key1", "key1"], skins: ["🙂"])
let item5WithSearchString = EmojiItem(label: "emoji0", unicode: "\(searchString)_123", keywords: ["key1", "key1"], shortcodes: ["key1", "key1"], skins: ["🙂"])
categories.append(EmojiCategory(id: "test",
emojis: [item0WithSearchString,
item1WithSearchString,
item2WithSearchString,
item3WithSearchString,
item4WithoutSearchString]))
categories.append(EmojiCategory(id: "test",
emojis: [item5WithSearchString]))
let emojiLoaderMock = EmojiLoaderMock()
emojiLoaderMock.categories = categories
let emojiProvider = EmojiProvider(loader: emojiLoaderMock)
_ = await emojiProvider.categories()
let result = await emojiProvider.categories(searchString: searchString)
XCTAssertEqual(result.count, 2)
XCTAssertEqual(result.first?.emojis.count, 4)
}
}
private class EmojiLoaderMock: EmojiLoaderProtocol {
var categories = [ElementX.EmojiCategory]()
func load() async -> [ElementX.EmojiCategory] {
categories
}
}