LiveLikeGiphyChallengeTests.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // LiveLikeGiphyChallengeTests.swift
  3. // LiveLikeGiphyChallengeTests
  4. //
  5. // Created by Irina Smokvarska on 1/30/22.
  6. //
  7. import XCTest
  8. @testable import LiveLikeGiphyChallenge
  9. class LiveLikeGiphyChallengeTests: XCTestCase {
  10. var urlSession: URLSession!
  11. override func setUpWithError() throws {
  12. // Set url session for mock networking
  13. let configuration = URLSessionConfiguration.ephemeral
  14. configuration.protocolClasses = [MockURLProtocol.self]
  15. urlSession = URLSession(configuration: configuration)
  16. }
  17. func testFetchGifsByString_retrieveSearchedGif_successfullyReturnsSearchedGifData() async throws {
  18. let searchString = "Adventure%20Time%20Morning"
  19. let chunk = 1
  20. //The problem with the API here is that it doesn't always return this GIF with the exact same search string
  21. let expectedJSON = """
  22. [
  23. { "type": "gif",
  24. "id": "KFOqUTRI8H26s",
  25. "url": "https://giphy.com/gifs/KFOqUTRI8H26s",
  26. "title": "Adventure Time Morning GIF"
  27. }
  28. ]
  29. """
  30. let mockData = Data(expectedJSON.utf8)
  31. MockURLProtocol.requestHandler = { request in
  32. (HTTPURLResponse(), mockData)
  33. }
  34. let service = FetchGiphyAPI()
  35. service.urlSession = urlSession
  36. let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk)
  37. XCTAssertEqual(1, gif.count, "The search result should show 1 GIF")
  38. }
  39. func testFetchGifsByString_errorWhileRetrievingSearchedGif_failedSearchedGifData() async throws {
  40. let searchString = "Adventure%20Time%20Morning"
  41. let chunk = -1
  42. let expectedJSON = """
  43. [
  44. { "type": "gif",
  45. "id": "KFOqUTRI8H26s",
  46. "url": "https://giphy.com/gifs/KFOqUTRI8H26s",
  47. "title": "Adventure Time Morning GIF"
  48. }
  49. ]
  50. """
  51. let mockData = Data(expectedJSON.utf8)
  52. MockURLProtocol.requestHandler = { request in
  53. (HTTPURLResponse(), mockData)
  54. }
  55. let service = FetchGiphyAPI()
  56. service.urlSession = urlSession
  57. let gif = try await service.fetchGifsBy(string: searchString, chunk: chunk)
  58. XCTAssertEqual([], gif, "The search result should be empty")
  59. }
  60. func testFetchTrendingGifs_successfullyRetrievesTreningGifs() async throws {
  61. let service = FetchGiphyAPI()
  62. service.urlSession = urlSession
  63. let gifs = try await service.fetchTrendingGifs(chunk: 20)
  64. XCTAssertFalse(gifs.isEmpty, "We should get gifs")
  65. }
  66. }