-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Union test mock generator (#2298)
* change: Update version for next alpha release * docs: Update Roadmap to point to releases and not specific alpha version * docs: Update Changelog for next alpha release * change: Add target dependency for test mocks package * fix: Generate test mock extensions for Union types * chore: Update AnimalKingdomAPI generated files * ci: Add Union mock generated file to TestMocks expected paths test
- Loading branch information
1 parent
a4dbe35
commit 11bc60d
Showing
10 changed files
with
224 additions
and
6 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
9 changes: 9 additions & 0 deletions
9
Sources/AnimalKingdomAPI/AnimalKingdomAPI/TestMocks/ClassroomPet+Mock.swift
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,9 @@ | ||
// @generated | ||
// This file was automatically generated and should not be edited. | ||
|
||
import ApolloTestSupport | ||
import AnimalKingdomAPI | ||
|
||
extension ClassroomPet: MockFieldValue { | ||
public typealias MockValueCollectionType = Array<AnyMock> | ||
} |
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
23 changes: 23 additions & 0 deletions
23
Sources/ApolloCodegenLib/FileGenerators/MockUnionFileGenerator.swift
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,23 @@ | ||
import Foundation | ||
import ApolloUtils | ||
|
||
/// Generates a file providing the ability to mock a GraphQLUnionType for testing purposes. | ||
struct MockUnionFileGenerator: FileGenerator { | ||
/// Source GraphQL union. | ||
let graphqlUnion: GraphQLUnionType | ||
|
||
let ir: IR | ||
|
||
let config: ReferenceWrapped<ApolloCodegenConfiguration> | ||
|
||
var template: TemplateRenderer { | ||
MockUnionTemplate( | ||
graphqlUnion: graphqlUnion, | ||
config: config, | ||
ir: ir | ||
) | ||
} | ||
|
||
var target: FileTarget { .testMock } | ||
var fileName: String { "\(graphqlUnion.name)+Mock.swift" } | ||
} |
24 changes: 24 additions & 0 deletions
24
Sources/ApolloCodegenLib/Templates/MockUnionTemplate.swift
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,24 @@ | ||
import Foundation | ||
import ApolloUtils | ||
|
||
struct MockUnionTemplate: TemplateRenderer { | ||
/// IR representation of source [GraphQL Union](https://spec.graphql.org/draft/#sec-Unions). | ||
let graphqlUnion: GraphQLUnionType | ||
|
||
/// Shared codegen configuration. | ||
let config: ReferenceWrapped<ApolloCodegenConfiguration> | ||
|
||
let ir: IR | ||
|
||
let target: TemplateTarget = .testMockFile | ||
|
||
var template: TemplateString { | ||
TemplateString(""" | ||
extension \ | ||
\(if: !config.output.schemaTypes.isInModule, "\(ir.schema.name.firstUppercased).")\ | ||
\(graphqlUnion.name.firstUppercased): MockFieldValue { | ||
public typealias MockValueCollectionType = Array<AnyMock> | ||
} | ||
""") | ||
} | ||
} |
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
52 changes: 52 additions & 0 deletions
52
Tests/ApolloCodegenTests/CodeGeneration/FileGenerators/MockUnionFileGeneratorTests.swift
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,52 @@ | ||
import XCTest | ||
import Nimble | ||
@testable import ApolloCodegenLib | ||
import ApolloUtils | ||
|
||
class MockUnionFileGeneratorTests: XCTestCase { | ||
let graphqlUnion = GraphQLUnionType.mock("MockUnion", types: []) | ||
|
||
var subject: MockUnionFileGenerator! | ||
|
||
override func tearDown() { | ||
subject = nil | ||
} | ||
|
||
// MARK: Test Helpers | ||
|
||
private func buildSubject() { | ||
subject = MockUnionFileGenerator( | ||
graphqlUnion: graphqlUnion, | ||
ir: .mock(compilationResult: .mock()), | ||
config: ReferenceWrapped(value: .mock(.other)) | ||
) | ||
} | ||
|
||
// MARK: Property Tests | ||
|
||
func test__properties__shouldReturnTargetType_testMock() { | ||
// given | ||
buildSubject() | ||
|
||
// then | ||
expect(self.subject.target).to(equal(.testMock)) | ||
} | ||
|
||
func test__properties__givenGraphQLUnion_shouldReturnFileName_matchingUnionName() { | ||
// given | ||
buildSubject() | ||
|
||
let expected = "\(graphqlUnion.name)+Mock.swift" | ||
|
||
// then | ||
expect(self.subject.fileName).to(equal(expected)) | ||
} | ||
|
||
func test__properties__givenGraphQLUnion_shouldOverwrite() { | ||
// given | ||
buildSubject() | ||
|
||
// then | ||
expect(self.subject.overwrite).to(beTrue()) | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
Tests/ApolloCodegenTests/CodeGeneration/Templates/MockUnionTemplateTests.swift
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,77 @@ | ||
import XCTest | ||
import Nimble | ||
@testable import ApolloCodegenLib | ||
import ApolloCodegenInternalTestHelpers | ||
import ApolloUtils | ||
|
||
class MockUnionTemplateTests: XCTestCase { | ||
var ir: IR! | ||
var subject: MockUnionTemplate! | ||
|
||
override func tearDown() { | ||
subject = nil | ||
|
||
super.tearDown() | ||
} | ||
|
||
// MARK: Helpers | ||
|
||
private func buildSubject( | ||
name: String = "Pet", | ||
types: [GraphQLObjectType] = [], | ||
moduleType: ApolloCodegenConfiguration.SchemaTypesFileOutput.ModuleType = .swiftPackageManager | ||
) { | ||
let config = ApolloCodegenConfiguration.mock(moduleType) | ||
ir = IR.mock(compilationResult: .mock()) | ||
|
||
subject = MockUnionTemplate( | ||
graphqlUnion: GraphQLUnionType.mock(name, types: types), | ||
config: ReferenceWrapped(value: config), | ||
ir: ir | ||
) | ||
} | ||
|
||
private func renderSubject() -> String { | ||
subject.template.description | ||
} | ||
|
||
// MARK: Boilerplate tests | ||
|
||
func test__target__isTestMockFile() { | ||
buildSubject() | ||
|
||
expect(self.subject.target).to(equal(.testMockFile)) | ||
} | ||
|
||
func test_render_givenSchemaType_generatesExtensionWithTypealias() { | ||
// given | ||
buildSubject(name: "Pet") | ||
|
||
let expected = """ | ||
extension Pet: MockFieldValue { | ||
public typealias MockValueCollectionType = Array<AnyMock> | ||
} | ||
""" | ||
|
||
// when | ||
let actual = renderSubject() | ||
|
||
// then | ||
expect(actual).to(equalLineByLine(expected)) | ||
} | ||
|
||
func test_render_givenConfig_SchemaTypeOutputNone_generatesExtensionWithSchemaNamespace() { | ||
// given | ||
buildSubject(name: "Pet", moduleType: .embeddedInTarget(name: "MockApplication")) | ||
|
||
let expected = """ | ||
extension TestSchema.Pet: MockFieldValue { | ||
""" | ||
|
||
// when | ||
let actual = renderSubject() | ||
|
||
// then | ||
expect(actual).to(equalLineByLine(expected, ignoringExtraLines: true)) | ||
} | ||
} |
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