// // LiveLikeGiphyChallengeTests.swift // LiveLikeGiphyChallengeTests // // Created by Irina Smokvarska on 1/30/22. // import XCTest @testable import LiveLikeGiphyChallenge class LiveLikeGiphyChallengeTests: XCTestCase { var urlSession: URLSession! override func setUpWithError() throws { // Set url session for mock networking let configuration = URLSessionConfiguration.ephemeral configuration.protocolClasses = [MockURLProtocol.self] urlSession = URLSession(configuration: configuration) } func testFetchGifsByString_retrieveSearchedGif_successfullyReturnsSearchedGifData() async throws { let searchString = "Adventure%20Time%20Morning" let chunk = 1 //The problem with the API here is that it doesn't always return this GIF with the exact same search string let expectedJSON = """ [ { "type": "gif", "id": "KFOqUTRI8H26s", "url": "https://giphy.com/gifs/KFOqUTRI8H26s", "title": "Adventure Time Morning GIF" } ] """ let mockData = Data(expectedJSON.utf8) MockURLProtocol.requestHandler = { request in (HTTPURLResponse(), mockData) } let service = FetchGiphyAPI() service.urlSession = urlSession let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk) XCTAssertEqual(1, gif.count, "The search result should show 1 GIF") } func testFetchGifsByString_errorWhileRetrievingSearchedGif_failedSearchedGifData() async throws { let searchString = "Adventure%20Time%20Morning" let chunk = -1 let expectedJSON = """ [ { "type": "gif", "id": "KFOqUTRI8H26s", "url": "https://giphy.com/gifs/KFOqUTRI8H26s", "title": "Adventure Time Morning GIF" } ] """ let mockData = Data(expectedJSON.utf8) MockURLProtocol.requestHandler = { request in (HTTPURLResponse(), mockData) } let service = FetchGiphyAPI() service.urlSession = urlSession let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk) XCTAssertEqual([], gif, "The search result should be empty") } func testFetchTrendingGifs_successfullyRetrievesTreningGifs() async throws { let service = FetchGiphyAPI() service.urlSession = urlSession let gifs = try await service.fetchTrendingGifs(chunk: 20) XCTAssertFalse(gifs.isEmpty, "We should get gifs") } }