Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle conversion of test mock list of objects into selection set data #2591

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Sources/ApolloTestSupport/TestMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public class Mock<O: MockObject>: AnyMock, Hashable {
if let mock = $0 as? AnyMock {
return mock._selectionSetMockData
}
if let mockArray = $0 as? [AnyMock?] {
return mockArray.map(\.?._selectionSetMockData)
}
return $0
}
}
Expand Down
34 changes: 34 additions & 0 deletions Tests/ApolloTests/TestMockTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,40 @@ class TestMockTests: XCTestCase {
expect(heightDict?["yards"] as? Int).to(equal(3))
}

func test___selectionSetMockData__givenListOfObjectsFieldSet__convertsObjectsToDict() throws {
// given
let mock = Mock<Dog>()
let cat = Mock<Cat>()

// when
cat.id = "10"
mock.listOfObjects = [cat]

let actual = mock._selectionSetMockData
let listDict = actual["listOfObjects"] as? [JSONObject]

// then
expect(listDict).toNot(beNil())
expect(listDict?[0]["id"] as? String).to(equal("10"))
}

func test___selectionSetMockData__givenOptionalListOfObjectsFieldSet__convertsObjectsToDict() throws {
// given
let mock = Mock<Dog>()
let cat = Mock<Cat>()

// when
cat.id = "10"
mock.listOfOptionalObjects = [cat, nil]

let actual = mock._selectionSetMockData
let listDict = actual["listOfOptionalObjects"] as? [JSONObject?]

// then
expect(listDict).toNot(beNil())
expect(listDict?[0]?["id"] as? String).to(equal("10"))
}

func test___selectionSetMockData__givenCustomScalarField__convertsObjectToDictWithCustomScalarIntact() throws {
// given
let mock = Mock<Dog>()
Expand Down