Skip to content

Commit

Permalink
Import statements update (w/tests) (#245)
Browse files Browse the repository at this point in the history
Co-authored-by: Hemel Yahya <hemelyahya@Hemels-MacBook-Pro-2.local>
  • Loading branch information
AnthonyMDev and Hemel Yahya committed Jan 25, 2024
1 parent 7238ab5 commit e28f3e2
Show file tree
Hide file tree
Showing 17 changed files with 411 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .package.resolved
Expand Up @@ -59,8 +59,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "a902f1823a7ff3c9ab2fba0f992396b948eda307",
"version" : "1.0.5"
"revision" : "d029d9d39c87bed85b1c50adee7c41795261a192",
"version" : "1.0.6"
}
}
],
Expand Down
Expand Up @@ -61,6 +61,30 @@ class FragmentTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
fragment TestFragment on Query @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndFragment()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: Fragment Definition

func test__render__givenFragment_generatesFragmentDeclarationDefinitionAndBoilerplate() async throws {
Expand Down
Expand Up @@ -63,6 +63,30 @@ class LocalCacheMutationDefinitionTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
query TestOperation @apollo_client_ios_localCacheMutation @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndOperation()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: - Access Level Tests

func test__generate__givenQuery_whenModuleTypeIsSwiftPackageManager_andOperationsInSchemaModule_generatesWithPublicAccess() async throws {
Expand Down
Expand Up @@ -64,7 +64,31 @@ class OperationDefinitionTemplateTests: XCTestCase {
subject.renderBodyTemplate(nonFatalErrorRecorder: .init()).description
}

// MARK: - Operation Definition
// MARK: - Target Configuration Tests

func test__target__givenModuleImports_targetHasModuleImports() async throws {
// given
document = """
query TestOperation @import(module: "ModuleA") {
allAnimals {
species
}
}
"""

// when
try await buildSubjectAndOperation()

guard case let .operationFile(actual) = subject.target else {
fail("expected operationFile target")
return
}

// then
expect(actual).to(equal(["ModuleA"]))
}

// MARK: - Operation Definition Tests

func test__generate__givenQuery_generatesQueryOperation() async throws {
// given
Expand Down
Expand Up @@ -51,7 +51,7 @@ class SelectionSetTemplateTests: XCTestCase {
)
))
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: config
)
Expand Down
Expand Up @@ -41,7 +41,7 @@ class SelectionSetTemplate_ErrorHandling_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand All @@ -64,7 +64,7 @@ class SelectionSetTemplate_ErrorHandling_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down
Expand Up @@ -42,7 +42,7 @@ class SelectionSetTemplate_Initializers_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down Expand Up @@ -70,7 +70,7 @@ class SelectionSetTemplate_Initializers_Tests: XCTestCase {
options: .init()
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: .init(config: config)
)
Expand Down
Expand Up @@ -43,7 +43,7 @@ class SelectionSetTemplate_LocalCacheMutationTests: XCTestCase {
)
)
let mockTemplateRenderer = MockTemplateRenderer(
target: .operationFile,
target: .operationFile(),
template: "",
config: config
)
Expand Down
Expand Up @@ -2,6 +2,7 @@ import XCTest
@testable import ApolloCodegenLib
@testable import ApolloCodegenInternalTestHelpers
import Nimble
import OrderedCollections

class TemplateRenderer_OperationFile_Tests: XCTestCase {

Expand All @@ -21,8 +22,13 @@ class TemplateRenderer_OperationFile_Tests: XCTestCase {
)
}

private func buildSubject(config: ApolloCodegenConfiguration = .mock()) -> MockFileTemplate {
MockFileTemplate(target: .operationFile, config: ApolloCodegen.ConfigurationContext(config: config))
private func buildSubject(
config: ApolloCodegenConfiguration = .mock(),
moduleImports: OrderedSet<String>? = nil
) -> MockFileTemplate {
MockFileTemplate(
target: .operationFile(moduleImports: moduleImports),
config: ApolloCodegen.ConfigurationContext(config: config))
}

// MARK: Render Target .operationFile Tests
Expand Down Expand Up @@ -359,6 +365,32 @@ class TemplateRenderer_OperationFile_Tests: XCTestCase {
expect(actual).to(equalLineByLine(expected, atLine: 6, ignoringExtraLines: true))
}

func test__moduleImports__givenValues_shouldGenerateImportStatements() {
// given
let config = buildConfig(
moduleType:
.embeddedInTarget(
name: "MockApplication",
accessModifier: .public),
operations: .inSchemaModule
)

let moduleImports = OrderedSet(["TestA", "TestB"])

let subject = buildSubject(config: config, moduleImports: moduleImports)

let expected = """
import TestA
import TestB
"""

// when
let (actual, _) = subject.render()

// then
expect(actual).to(equalLineByLine(expected, atLine: 5, ignoringExtraLines: true))
}

func test__casing__givenUppercasedSchemaName_shouldGenerateUppercasedNamespace() {
// given

Expand Down

0 comments on commit e28f3e2

Please sign in to comment.