Skip to content

Commit

Permalink
Fix ParseQueryResponseTests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyMDev committed Sep 21, 2021
1 parent 92fc3b2 commit 6f5e31e
Showing 1 changed file with 19 additions and 75 deletions.
94 changes: 19 additions & 75 deletions Tests/ApolloTests/ParseQueryResponseTests.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
import XCTest
@testable import Apollo
import ApolloTestSupport
import StarWarsAPI

class ParseQueryResponseTests: XCTestCase {

func testHeroDetailsWithFragmentQueryDroid() throws {
let query = HeroDetailsWithFragmentQuery()

let response = GraphQLResponse(operation: query, body: [
"data": [
"hero": ["__typename": "Droid", "name": "R2-D2", "primaryFunction": "Astromech"]
]
])

let (result, _) = try response.parseResult()

let droid = try XCTUnwrap(result.data?.hero?.fragments.heroDetails.asDroid,
"Wrong type")

XCTAssertEqual(droid.primaryFunction, "Astromech")
}

func testHeroDetailsWithFragmentQueryHuman() throws {
let query = HeroDetailsWithFragmentQuery()

let response = GraphQLResponse(operation: query, body: [
"data": [
"hero": ["__typename": "Human", "name": "Luke Skywalker", "height": 1.72]
]
])

let (result, _) = try response.parseResult()

let human = try XCTUnwrap(result.data?.hero?.fragments.heroDetails.asHuman,
"Wrong type")

XCTAssertEqual(human.height, 1.72)
}

// MARK: - Response Extensions

func testExtensionsEntryNotNullWhenProvidedInResponseAccompanyingDataEntry() throws {
let query = HumanQuery(id: "9999")
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()],
Expand All @@ -55,7 +20,7 @@ class ParseQueryResponseTests: XCTestCase {
}

func testExtensionsValuesWhenPopulatedInResponse() throws {
let query = HumanQuery(id: "9999")
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()],
Expand All @@ -72,7 +37,7 @@ class ParseQueryResponseTests: XCTestCase {
}

func testExtensionsEntryNullWhenNotProvidedInResponse() throws {
let query = HumanQuery(id: "9999")
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"data": ["human": NSNull()]
Expand All @@ -84,7 +49,7 @@ class ParseQueryResponseTests: XCTestCase {
}

func testExtensionsEntryNotNullWhenDataEntryNotProvidedInResponse() throws {
let query = HumanQuery(id: "9999")
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"extensions": [:]
Expand All @@ -95,50 +60,29 @@ class ParseQueryResponseTests: XCTestCase {
XCTAssertNotNil(result.extensions)
}

// MARK: Mutations

func testCreateReviewForEpisode() throws {
let mutation = CreateReviewForEpisodeMutation(episode: .jedi, review: ReviewInput(stars: 5, commentary: "This is a great movie!"))

let response = GraphQLResponse(operation: mutation, body: [
"data": [
"createReview": [
"__typename": "Review",
"stars": 5,
"commentary": "This is a great movie!"
]
]
])

let (result, _) = try response.parseResult()

XCTAssertEqual(result.data?.createReview?.stars, 5)
XCTAssertEqual(result.data?.createReview?.commentary, "This is a great movie!")
}

// MARK: - Error responses

func testErrorResponseWithoutLocation() throws {
let query = HeroNameQuery()
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"errors": [
[
"message": "Some error",
]
]
])

let (result, _) = try response.parseResult()

XCTAssertNil(result.data)
XCTAssertEqual(result.errors?.first?.message, "Some error")
XCTAssertNil(result.errors?.first?.locations)
}

func testErrorResponseWithLocation() throws {
let query = HeroNameQuery()
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"errors": [
[
Expand All @@ -149,18 +93,18 @@ class ParseQueryResponseTests: XCTestCase {
]
]
])

let (result, _) = try response.parseResult()

XCTAssertNil(result.data)
XCTAssertEqual(result.errors?.first?.message, "Some error")
XCTAssertEqual(result.errors?.first?.locations?.first?.line, 1)
XCTAssertEqual(result.errors?.first?.locations?.first?.column, 2)
}

func testErrorResponseWithCustomError() throws {
let query = HeroNameQuery()
let query = MockQuery.mock()

let response = GraphQLResponse(operation: query, body: [
"errors": [
[
Expand All @@ -169,9 +113,9 @@ class ParseQueryResponseTests: XCTestCase {
]
]
])

let (result, _) = try response.parseResult()

XCTAssertNil(result.data)
XCTAssertEqual(result.errors?.first?.message, "Some error")
XCTAssertEqual(result.errors?.first?["userMessage"] as? String, "Some message")
Expand Down

0 comments on commit 6f5e31e

Please sign in to comment.