Skip to content

[v2] [8/X] Unit Test Overhaul - Part 2 #644

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

Draft
wants to merge 1 commit into
base: QueryWatcher-tasklocal-contextid
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions Tests/ApolloInternalTestHelpers/MockOperation.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ApolloAPI
import Foundation

open class MockOperation<SelectionSet: RootSelectionSet>: GraphQLOperation, @unchecked Sendable {
public typealias Data = SelectionSet
Expand All @@ -23,6 +24,14 @@ open class MockQuery<SelectionSet: RootSelectionSet>: MockOperation<SelectionSet
public static func mock() -> MockQuery<MockSelectionSet> where SelectionSet == MockSelectionSet {
MockQuery<MockSelectionSet>()
}

public var defaultResponseData: Foundation.Data {
return """
{
"data": {}
}
""".data(using: .utf8)!
}
}

open class MockMutation<SelectionSet: RootSelectionSet>: MockOperation<SelectionSet>, GraphQLMutation, @unchecked Sendable {
Expand Down
23 changes: 20 additions & 3 deletions Tests/ApolloInternalTestHelpers/MockURLProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public final class MockURLProtocol<RequestProvider: MockResponseProvider>: URLPr
override public func startLoading() {
self.asyncTask = Task {
guard
let url = self.request.url,
let handler = await RequestProvider.requestHandler(for: url)
let handler = await requestHandler(for: request.url)
else {
self.client?.urlProtocol(self, didFailWithError: MockURLError.requestNotHandled)
return
Expand Down Expand Up @@ -52,7 +51,25 @@ public final class MockURLProtocol<RequestProvider: MockResponseProvider>: URLPr
}
}
}


private func requestHandler(for url: URL?) async -> MockResponseProvider.MultiResponseHandler? {
guard let url = url else { return nil }

if let handler = await RequestProvider.requestHandler(for: url) {
return handler
}

var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false)
urlComponents?.query = nil

if let urlWithoutQueryString = urlComponents?.string,
let urlWithoutQuery = URL(string: urlWithoutQueryString) {
return await RequestProvider.requestHandler(for: urlWithoutQuery)
}

return nil
}

override public func stopLoading() {
self.asyncTask?.cancel()
}
Expand Down
Loading
Loading