Skip to content

Commit

Permalink
Correctly chain up async calls on emoji store loading to avoid race c…
Browse files Browse the repository at this point in the history
…onditions and intermittent test failures.
  • Loading branch information
stefanceriu committed Feb 8, 2022
1 parent b35407b commit c9d0a4f
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions RiotTests/EmojiStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ import XCTest

class EmojiStoreTests: XCTestCase {

private var emojiStore: EmojiStore!

override func setUp() {
emojiStore = loadStore()
}

// MARK: - Tests

func testFinds💯WhenSearchingForHundred() {
find("hundred", expect: "💯")
}
Expand All @@ -43,34 +35,26 @@ class EmojiStoreTests: XCTestCase {
func testFinds2️⃣WhenSearchingFor2() {
find("2", expect: "2️⃣")
}

// MARK: - Helpers

private func loadStore() -> EmojiStore {
let store = EmojiStore()
let emojiService = EmojiMartService()
let expectation = self.expectation(description: "The wai-ai-ting is the hardest part")

emojiService.getEmojiCategories { response in

// MARK: - Private

private func find(_ searchText: String, expect emoji: String) {
loadEmojiStore { emojiStore in
let emojis = emojiStore.findEmojiItemsSortedByCategory(with: searchText).flatMap { $0.emojis.map { $0.value } }
XCTAssert(emojis.contains(emoji), "Search text \"\(searchText)\" should find \"\(emoji)\" but only found \(emojis)")
}
}

private func loadEmojiStore(_ completion: @escaping (EmojiStore) -> Void) {
EmojiMartService().getEmojiCategories { response in
switch response {
case .success(let categories):
let store = EmojiStore()
store.set(categories)
expectation.fulfill()
completion(store)
case .failure(let error):
XCTFail("Failed to load emojis: \(error)")
}
}

waitForExpectations(timeout: 2) { error in
XCTAssertNil(error)
}

return store
}

private func find(_ searchText: String, expect emoji: String) {
let emojis = emojiStore.findEmojiItemsSortedByCategory(with: searchText).flatMap { $0.emojis.map { $0.value } }
XCTAssert(emojis.contains(emoji), "Search text \"\(searchText)\" should find \"\(emoji)\" but only found \(emojis)")
}

}

0 comments on commit c9d0a4f

Please sign in to comment.