mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-02 12:07:55 +00:00
7f91852291
* Screens template * Loading and parsing * Gridview * New json * Search method * Unit tests * Added emoji provider state * Removed containsIgnoringCase * Added plus icon, added double tap gesture, dismiss sheet after selecting emoji * Renamed EmojisProvider to EmojiProvider * Renamed EmojiPickerScreenScreen * Extra padding * Renamed EmojisLoaderProtocol * Category names * Moved method into public section of the class * Return all categories when search string is empty * Added cancel button and tittle * Removed empty lines * Removed swiftlint warning
105 lines
5.3 KiB
Swift
105 lines
5.3 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
|
|
|
|
final class EmojiProviderTests: XCTestCase {
|
|
var sut: EmojiProvider!
|
|
private var emojiLoaderMock: EmojiLoaderMock!
|
|
|
|
@MainActor override func setUp() {
|
|
emojiLoaderMock = EmojiLoaderMock()
|
|
sut = EmojiProvider(loader: emojiLoaderMock)
|
|
}
|
|
|
|
func test_whenEmojisLoaded_categoriesAreLoadedFromLoader() async throws {
|
|
let item = EmojiItem(id: "test", name: "test", keywords: ["1", "2"], skins: [try slightlySmilingFaceEmoji()])
|
|
let category = EmojiCategory(id: "test", emojis: [item])
|
|
emojiLoaderMock.categories = [category]
|
|
let categories = await sut.getCategories()
|
|
XCTAssertEqual(emojiLoaderMock.categories, categories)
|
|
}
|
|
|
|
func test_whenEmojisLoadedAndSearchStringEmpty_allCategoriesReturned() async throws {
|
|
let item = EmojiItem(id: "test", name: "test", keywords: ["1", "2"], skins: [try slightlySmilingFaceEmoji()])
|
|
let category = EmojiCategory(id: "test", emojis: [item])
|
|
emojiLoaderMock.categories = [category]
|
|
let categories = await sut.getCategories(searchString: "")
|
|
XCTAssertEqual(emojiLoaderMock.categories, categories)
|
|
}
|
|
|
|
func test_whenEmojisLoadedSecondTime_cachedValuesAreUsed() async throws {
|
|
let categoriesForFirstLoad = [EmojiCategory(id: "test",
|
|
emojis: [EmojiItem(id: "test", name: "test", keywords: ["1", "2"], skins: [try slightlySmilingFaceEmoji()])])]
|
|
let categoriesForSecondLoad = [EmojiCategory(id: "test2",
|
|
emojis: [EmojiItem(id: "test2", name: "test2", keywords: ["3", "4"], skins: [try meltingFaceEmoji()])])]
|
|
emojiLoaderMock.categories = categoriesForFirstLoad
|
|
_ = await sut.getCategories()
|
|
emojiLoaderMock.categories = categoriesForSecondLoad
|
|
let categories = await sut.getCategories()
|
|
XCTAssertEqual(categories, categoriesForFirstLoad)
|
|
}
|
|
|
|
func test_whenEmojisSearched_correctNumberOfCategoriesReturned() async throws {
|
|
let searchString = "smile"
|
|
var categories = [EmojiCategory]()
|
|
categories.append(EmojiCategory(id: "test",
|
|
emojis: [EmojiItem(id: "\(searchString)_123",
|
|
name: "emoji0",
|
|
keywords: ["key1", "key1"],
|
|
skins: [try slightlySmilingFaceEmoji()]),
|
|
EmojiItem(id: "emoji_1",
|
|
name: searchString,
|
|
keywords: ["key1", "key1"],
|
|
skins: [try slightlySmilingFaceEmoji()]),
|
|
EmojiItem(id: "emoji_2",
|
|
name: "emoji2",
|
|
keywords: ["key1", "\(searchString)_123"],
|
|
skins: [try slightlySmilingFaceEmoji()]),
|
|
EmojiItem(id: "emoji_3",
|
|
name: "emoji_3",
|
|
keywords: ["key1", "key1"],
|
|
skins: [try slightlySmilingFaceEmoji()])]))
|
|
categories.append(EmojiCategory(id: "test",
|
|
emojis: [EmojiItem(id: "\(searchString)_123",
|
|
name: "emoji0",
|
|
keywords: ["key1", "key1"],
|
|
skins: [try slightlySmilingFaceEmoji()])]))
|
|
emojiLoaderMock.categories = categories
|
|
_ = await sut.getCategories()
|
|
let result = await sut.getCategories(searchString: searchString)
|
|
XCTAssertEqual(result.count, 2)
|
|
XCTAssertEqual(result.first?.emojis.count, 3)
|
|
}
|
|
|
|
private func slightlySmilingFaceEmoji() throws -> EmojiItemSkin {
|
|
try XCTUnwrap(EmojiItemSkin(from: EmojiMartEmojiSkin(unified: "1f642", native: "🙂")))
|
|
}
|
|
|
|
private func meltingFaceEmoji() throws -> EmojiItemSkin {
|
|
try XCTUnwrap(EmojiItemSkin(from: EmojiMartEmojiSkin(unified: "1fae0", native: "🫠")))
|
|
}
|
|
}
|
|
|
|
private class EmojiLoaderMock: EmojiLoaderProtocol {
|
|
var categories = [ElementX.EmojiCategory]()
|
|
func load() async -> [ElementX.EmojiCategory] {
|
|
categories
|
|
}
|
|
}
|