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

fix: fixes two issues in the generation of mocks. #3120

Merged
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct MockObjectTemplate: TemplateRenderer {
\(if: $0.propertyName.isConflictingTestMockFieldName, """
var \($0.propertyName): \($0.mockType)? {
get { _data["\($0.propertyName)"] as? \($0.mockType) }
set { _set(newValue, for: \\.\($0.propertyName)) }
set { _set\(mockFunctionDescriptor($0.type))(newValue, for: \\.\($0.propertyName)) }
}
""")
""" }, separator: "\n", terminator: "\n")
Expand Down
20 changes: 20 additions & 0 deletions Sources/ApolloTestSupport/TestMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ public class Mock<O: MockObject>: AnyMock, Hashable {
_data[field.key.description] = value?._unsafelyConvertToMockValue()
}

public subscript<T: AnyScalarType & Hashable>(
dynamicMember keyPath: KeyPath<O.MockFields, Field<Array<T>>>
) -> [T]? {
get {
let field = O._mockFields[keyPath: keyPath]
return _data[field.key.description] as? [T]
}
set {
_setList(newValue, for: keyPath)
}
}

public func _setList<T: AnyScalarType & Hashable>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rename this ti setScalarList. The point of defining these methods in the first place was to avoid the issues we were having with trying to make sure the compiler always used the right overload for the functions based on the types. Just to maintain that pattern, they should all have unique names.

_ value: [T]?,
for keyPath: KeyPath<O.MockFields, Field<Array<T>>>
) {
let field = O._mockFields[keyPath: keyPath]
_data[field.key.description] = value?._unsafelyConvertToMockValue()
}

public var _selectionSetMockData: JSONObject {
_data.mapValues {
if let mock = $0 as? AnyMock {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ class MockObjectTemplateTests: XCTestCase {
let expected = """
var hash: String? {
get { _data["hash"] as? String }
set { _set(newValue, for: \\.hash) }
set { _setScalar(newValue, for: \\.hash) }
}

"""
Expand All @@ -487,6 +487,7 @@ class MockObjectTemplateTests: XCTestCase {
subject.graphqlObject.fields = [
"string": .mock("string", type: .nonNull(.string())),
"customScalar": .mock("customScalar", type: .nonNull(.scalar(.mock(name: "CustomScalar")))),
"customScalarList": .mock("customScalarList", type: .list(.nonNull(.scalar(.mock(name: "CustomScalar"))))),
"optionalString": .mock("optionalString", type: .string()),
"object": .mock("object", type: Cat),
"objectList": .mock("objectList", type: .list(.nonNull(Cat))),
Expand Down Expand Up @@ -516,6 +517,7 @@ class MockObjectTemplateTests: XCTestCase {
public extension Mock where O == Dog {
convenience init(
customScalar: TestSchema.CustomScalar? = nil,
customScalarList: [TestSchema.CustomScalar]? = nil,
enumType: GraphQLEnum<TestSchema.EnumType>? = nil,
interface: AnyMock? = nil,
interfaceList: [AnyMock]? = nil,
Expand All @@ -534,6 +536,7 @@ class MockObjectTemplateTests: XCTestCase {
) {
self.init()
_setScalar(customScalar, for: \\.customScalar)
_setList(customScalarList, for: \\.customScalarList)
_setScalar(enumType, for: \\.enumType)
_setEntity(interface, for: \\.interface)
_setList(interfaceList, for: \\.interfaceList)
Expand Down