|
| 1 | +import XCTest |
| 2 | +import Combine |
| 3 | +@testable import PublishedObject |
| 4 | + |
| 5 | +class Outer: ObservableObject { |
| 6 | + @PublishedObject var innerPublishedObject: Inner |
| 7 | + @Published var innerPublished: Inner |
| 8 | + |
| 9 | + init(_ value: Int) { |
| 10 | + self.innerPublishedObject = Inner(value) |
| 11 | + self.innerPublished = Inner(value) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +class Inner: ObservableObject { |
| 16 | + @Published var value: Int |
| 17 | + |
| 18 | + init(_ int: Int) { |
| 19 | + self.value = int |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +final class PublishedObjectTests: XCTestCase { |
| 24 | + var cancellables: Set<AnyCancellable> = [] |
| 25 | + |
| 26 | + func testObjectWillChange() throws { |
| 27 | + let outer = Outer(1) |
| 28 | + |
| 29 | + // Setting property on Outer (This will send an update with either @Published or @PublishedObject) |
| 30 | + |
| 31 | + let exp1 = XCTestExpectation(description: "outer.objectWillChange will be called") |
| 32 | + outer.objectWillChange.first().sink { exp1.fulfill() } .store(in: &cancellables) |
| 33 | + outer.innerPublishedObject = Inner(2) |
| 34 | + wait(for: [exp1], timeout: 0.1) |
| 35 | + |
| 36 | + let exp2 = XCTestExpectation(description: "outer.objectWillChange will be called") |
| 37 | + outer.objectWillChange.first().sink { exp2.fulfill() } .store(in: &cancellables) |
| 38 | + outer.innerPublished = Inner(2) |
| 39 | + wait(for: [exp2], timeout: 0.1) |
| 40 | + |
| 41 | + // Setting property on Inner (This will only send an update when using @PublishedObject) |
| 42 | + |
| 43 | + let exp3 = XCTestExpectation(description: "outer.objectWillChange will be called") |
| 44 | + outer.objectWillChange.first().sink { exp3.fulfill() } .store(in: &cancellables) |
| 45 | + outer.innerPublishedObject.value = 3 |
| 46 | + wait(for: [exp3], timeout: 0.1) |
| 47 | + |
| 48 | + let exp4 = XCTestExpectation(description: "outer.objectWillChange will NOT be called") |
| 49 | + exp4.isInverted = true |
| 50 | + outer.objectWillChange.first().sink { exp4.fulfill() } .store(in: &cancellables) |
| 51 | + outer.innerPublished.value = 3 |
| 52 | + wait(for: [exp4], timeout: 0.1) |
| 53 | + } |
| 54 | + |
| 55 | + func testProjectedValue() throws { |
| 56 | + let outer = Outer(1) |
| 57 | + |
| 58 | + // Initial value |
| 59 | + |
| 60 | + let exp0 = XCTestExpectation(description: "projectedValue will be called") |
| 61 | + outer.$innerPublishedObject.first().sink { inner in |
| 62 | + if inner.value == 1 { |
| 63 | + exp0.fulfill() |
| 64 | + } |
| 65 | + }.store(in: &cancellables) |
| 66 | + wait(for: [exp0], timeout: 0.1) |
| 67 | + |
| 68 | + let exp1 = XCTestExpectation(description: "projectedValue will be called") |
| 69 | + outer.$innerPublished.first().sink { inner in |
| 70 | + if inner.value == 1 { |
| 71 | + exp1.fulfill() |
| 72 | + } |
| 73 | + }.store(in: &cancellables) |
| 74 | + wait(for: [exp1], timeout: 0.1) |
| 75 | + |
| 76 | + |
| 77 | + // Setting property on Outer (This will send an update with either @Published or @PublishedObject) |
| 78 | + |
| 79 | + let exp2 = XCTestExpectation(description: "projectedValue will be called") |
| 80 | + outer.$innerPublishedObject.dropFirst().first().sink { inner in |
| 81 | + if inner.value == 2 { |
| 82 | + exp2.fulfill() |
| 83 | + } |
| 84 | + }.store(in: &cancellables) |
| 85 | + outer.innerPublishedObject = Inner(2) |
| 86 | + wait(for: [exp2], timeout: 0.1) |
| 87 | + |
| 88 | + let exp3 = XCTestExpectation(description: "projectedValue will be called") |
| 89 | + outer.$innerPublished.dropFirst().first().sink { inner in |
| 90 | + if inner.value == 2 { |
| 91 | + exp3.fulfill() |
| 92 | + } |
| 93 | + }.store(in: &cancellables) |
| 94 | + outer.innerPublished = Inner(2) |
| 95 | + wait(for: [exp3], timeout: 0.1) |
| 96 | + |
| 97 | + // Setting property on Inner (This will only send an update when using @PublishedObject) |
| 98 | + |
| 99 | + let exp4 = XCTestExpectation(description: "projectedValue will be called") |
| 100 | + outer.$innerPublishedObject.dropFirst().first().sink { inner in |
| 101 | + if inner.value == 3 { |
| 102 | + exp4.fulfill() |
| 103 | + } |
| 104 | + }.store(in: &cancellables) |
| 105 | + outer.innerPublishedObject.value = 3 |
| 106 | + wait(for: [exp4], timeout: 0.1) |
| 107 | + |
| 108 | + let exp5 = XCTestExpectation(description: "projectedValue will NOT be called") |
| 109 | + exp5.isInverted = true |
| 110 | + outer.$innerPublished.dropFirst().first().sink { inner in |
| 111 | + if inner.value == 3 { |
| 112 | + exp5.fulfill() |
| 113 | + } |
| 114 | + }.store(in: &cancellables) |
| 115 | + outer.innerPublished.value = 3 |
| 116 | + wait(for: [exp5], timeout: 0.1) |
| 117 | + } |
| 118 | + |
| 119 | + static var allTests = [ |
| 120 | + ("testObjectWillChange", testObjectWillChange), |
| 121 | + ("testProjectedValue", testProjectedValue), |
| 122 | + ] |
| 123 | +} |
0 commit comments