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

[GWL-74] CocoaCombine bind(to:) 추가 #103

Merged
merged 3 commits into from
Nov 23, 2023
Merged
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
6 changes: 5 additions & 1 deletion iOS/Projects/Shared/CombineCocoa/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ import ProjectDescriptionHelpers

let project = Project.makeModule(
name: "CombineCocoa",
targets: .custom(name: "CombineCocoa", product: .framework)
targets: .custom(
name: "CombineCocoa",
product: .framework,
testingOptions: [.unitTest]
)
)
17 changes: 17 additions & 0 deletions iOS/Projects/Shared/CombineCocoa/Sources/Publisher+bind.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Publisher+bind.swift
// CombineCocoa
//
// Created by 홍승현 on 11/23/23.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Combine

public extension Publisher where Failure == Never {
func bind<S>(to subject: S) -> AnyCancellable where S: Subject, S.Output == Output, S.Failure == Failure {
return sink { value in
subject.send(value)
}
}
}
68 changes: 68 additions & 0 deletions iOS/Projects/Shared/CombineCocoa/Tests/Publisher+bindTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// Publisher+bindTests.swift
// ProjectDescriptionHelpers
//
// Created by 홍승현 on 11/23/23.
//

@testable import CombineCocoa

import Combine
import XCTest

final class Publisher_bindTests: XCTestCase {
private var subscriptions: Set<AnyCancellable> = []
private let publisher: PassthroughSubject<Void, Never> = .init()
private var sut: AnyPublisher<Void, Never>?

override func tearDown() {
for subscription in subscriptions {
subscription.cancel()
}
subscriptions.removeAll()
}

func testBindToPassthroughSubject() {
// assign
let expectation = XCTestExpectation(description: "PassthroughSubject should receive value")
let passthroughSubject = PassthroughSubject<String, Never>()
let testValue = "Test"

// act & assert

passthroughSubject
.sink { receivedValue in
XCTAssertEqual(receivedValue, testValue)
expectation.fulfill()
}
.store(in: &subscriptions)

Just(testValue)
.bind(to: passthroughSubject)
.store(in: &subscriptions)

wait(for: [expectation], timeout: 1.0)
}

func testBindToCurrentValueSubject() {
// assign
let expectation = XCTestExpectation(description: "CurrentValueSubject should receive value")
let currentValueSubject = CurrentValueSubject<String, Never>("Initial Value")
let testValue = "Test"

// act
Just(testValue)
.bind(to: currentValueSubject)
.store(in: &subscriptions)

// assert
currentValueSubject
.sink { receivedValue in
XCTAssertEqual(receivedValue, testValue)
expectation.fulfill()
}
.store(in: &subscriptions)

wait(for: [expectation], timeout: 1.0)
}
}
Loading