diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index b7d2bfa..de4dd4a 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -6,7 +6,7 @@ on: pull_request: branches: [ main ] env: - DEVELOPER_DIR: /Applications/Xcode_13.2.1.app/Contents/Developer + DEVELOPER_DIR: /Applications/Xcode_14.2.app/Contents/Developer jobs: build: diff --git a/Package.swift b/Package.swift index b9ee55d..e9e40c1 100644 --- a/Package.swift +++ b/Package.swift @@ -5,12 +5,15 @@ import PackageDescription let package = Package( name: "AsyncWebSocketClient", - platforms: [.iOS(.v13), .macOS(.v10_15), .watchOS(.v6), .tvOS(.v13)], + platforms: [.iOS(.v13), .macOS(.v12), .watchOS(.v6), .tvOS(.v13)], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( name: "AsyncWebSocketClient", targets: ["AsyncWebSocketClient"]), + .library( + name: "AsyncWebSocketClientMocks", + targets: ["AsyncWebSocketClientMocks"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. @@ -24,6 +27,11 @@ let package = Package( dependencies: [ "AsyncTimeSequences", ]), + .target( + name: "AsyncWebSocketClientMocks", + dependencies: [ + "AsyncWebSocketClient", + ]), .testTarget( name: "AsyncWebSocketClientTests", dependencies: [ diff --git a/README.md b/README.md index 07ef09d..b3e8dd5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,3 @@ -(WORK IN PROGRESS) - # AsyncWebSocketClient ![badge-platforms][] [![badge-spm][]][spm] @@ -38,7 +36,7 @@ try await client.sendJSONData(objectToEncode) ## How to test -(Coming soon) +AsyncWebSocketClient conforms to AsyncWebSocketClientProtocol and it is recommended to use dependency injection to make testing easier. For convenience, this library also includes a mock target `AsyncWebSocketClientMocks` which you can use if you just need a simple mock. You could optionally add your own mock class conforming to AsyncWebSocketClientProtocol. ## Installation diff --git a/Sources/AsyncWebSocketClientMocks/AsyncWebSocketClientMock.swift b/Sources/AsyncWebSocketClientMocks/AsyncWebSocketClientMock.swift new file mode 100644 index 0000000..377f4b5 --- /dev/null +++ b/Sources/AsyncWebSocketClientMocks/AsyncWebSocketClientMock.swift @@ -0,0 +1,51 @@ +// +// AsyncWebSocketClientMock.swift +// +// +// Created by Henry Javier Serrano Echeverria on 27/1/24. +// + +import AsyncWebSocketClient +import Foundation + +open class AsyncWebSocketClientMock: AsyncWebSocketClientProtocol { + public var connectWasCalledCount = 0 + public func connect() async throws { + connectWasCalledCount += 1 + } + + public var disconnectWasCalledCount = 0 + public func disconnect() async throws { + disconnectWasCalledCount += 1 + } + + public var sendWasCalledStack = [AsyncWebSocketData]() + public func send(_ data: AsyncWebSocketData) async throws { + sendWasCalledStack.append(data) + } + + @Published public var streamSocketEvent = AsyncWebSocketEvent.socketOpened + public var listenStreamWasCalledCount = 0 + public func listenStream() async -> AsyncStream { + listenStreamWasCalledCount += 1 + + let stream = $streamSocketEvent.values + return AsyncStream { continuation in + let cancellableTask = Task { + for await event in stream { + try Task.checkCancellation() + continuation.yield(event) + } + } + // Cancel the task that is listening to the stream socket event. + continuation.onTermination = { _ in + cancellableTask.cancel() + } + } + } + + public init() { + + } + +}