-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding GraphQLResult conversion extension (#139)
- Loading branch information
1 parent
360f068
commit 1a508b1
Showing
3 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import XCTest | ||
import Apollo | ||
import ApolloAPI | ||
import ApolloInternalTestHelpers | ||
import StarWarsAPI | ||
|
||
final class GraphQLResultTests: XCTestCase { | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
try super.tearDownWithError() | ||
} | ||
|
||
func test__result__givenResponseWithData_convertsToJSON() throws { | ||
let jsonObj: [String: AnyHashable] = [ | ||
"hero": [ | ||
"name": "Luke Skywalker", | ||
"__typename": "Human" | ||
] | ||
] | ||
let heroData = try StarWarsAPI.HeroNameQuery.Data(data: jsonObj) | ||
let result = GraphQLResult( | ||
data: heroData, | ||
extensions: nil, | ||
errors: nil, | ||
source: .server, | ||
dependentKeys: nil | ||
) | ||
|
||
let expectedJSON: [String: Any] = [ | ||
"data": [ | ||
"hero": [ | ||
"name": "Luke Skywalker", | ||
"__typename": "Human" | ||
] | ||
] | ||
] | ||
|
||
let convertedJSON = result.asJSONDictionary() | ||
XCTAssertEqual(convertedJSON, expectedJSON) | ||
} | ||
|
||
func test__result__givenResponseWithNullData_convertsToJSON() throws { | ||
let jsonObj: [String: AnyHashable] = [ | ||
"hero": NSNull() | ||
] | ||
let heroData = try StarWarsAPI.HeroNameQuery.Data(data: jsonObj) | ||
let result = GraphQLResult( | ||
data: heroData, | ||
extensions: nil, | ||
errors: nil, | ||
source: .server, | ||
dependentKeys: nil | ||
) | ||
|
||
let expectedJSON: [String: Any] = [ | ||
"data": [ | ||
"hero": NSNull() | ||
] | ||
] | ||
|
||
let convertedJSON = result.asJSONDictionary() | ||
XCTAssertEqual(convertedJSON, expectedJSON) | ||
} | ||
|
||
func test__result__givenResponseWithErrors_convertsToJSON() throws { | ||
let jsonObj: [String: AnyHashable] = [ | ||
"message": "Sample error message", | ||
"locations": [ | ||
"line": 1, | ||
"column": 1 | ||
], | ||
"path": [ | ||
"TestPath" | ||
], | ||
"extensions": [ | ||
"test": "extension" | ||
] | ||
] | ||
|
||
let error = GraphQLError(jsonObj) | ||
let result = GraphQLResult<HeroNameQuery.Data>( | ||
data: nil, | ||
extensions: nil, | ||
errors: [error], | ||
source: .server, | ||
dependentKeys: nil | ||
) | ||
|
||
let expectedJSON: [String: Any] = [ | ||
"errors": [ | ||
[ | ||
"message": "Sample error message", | ||
"locations": [ | ||
"line": 1, | ||
"column": 1 | ||
], | ||
"path": [ | ||
"TestPath" | ||
], | ||
"extensions": [ | ||
"test": "extension" | ||
] | ||
] | ||
] | ||
] | ||
|
||
let convertedJSON = result.asJSONDictionary() | ||
XCTAssertEqual(convertedJSON, expectedJSON) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters