|  | 
|  | 1 | +// | 
|  | 2 | +// This file is part of Canvas. | 
|  | 3 | +// Copyright (C) 2025-present  Instructure, Inc. | 
|  | 4 | +// | 
|  | 5 | +// This program is free software: you can redistribute it and/or modify | 
|  | 6 | +// it under the terms of the GNU Affero General Public License as | 
|  | 7 | +// published by the Free Software Foundation, either version 3 of the | 
|  | 8 | +// License, or (at your option) any later version. | 
|  | 9 | +// | 
|  | 10 | +// This program is distributed in the hope that it will be useful, | 
|  | 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | +// GNU Affero General Public License for more details. | 
|  | 14 | +// | 
|  | 15 | +// You should have received a copy of the GNU Affero General Public License | 
|  | 16 | +// along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
|  | 17 | +// | 
|  | 18 | + | 
|  | 19 | +@testable import Core | 
|  | 20 | +import Combine | 
|  | 21 | +import XCTest | 
|  | 22 | + | 
|  | 23 | +class SubjectExtensionsTests: XCTestCase { | 
|  | 24 | + | 
|  | 25 | +    func test_binding_returnsCurrentValue() { | 
|  | 26 | +        let subject = CurrentValueSubject<Int, Never>(42) | 
|  | 27 | + | 
|  | 28 | +        // WHEN | 
|  | 29 | +        let binding = subject.binding | 
|  | 30 | + | 
|  | 31 | +        // THEN | 
|  | 32 | +        XCTAssertEqual(binding.wrappedValue, 42) | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    func test_binding_updatesSubjectValue() { | 
|  | 36 | +        let subject = CurrentValueSubject<String, Never>("initial") | 
|  | 37 | +        let binding = subject.binding | 
|  | 38 | + | 
|  | 39 | +        // WHEN | 
|  | 40 | +        binding.wrappedValue = "updated" | 
|  | 41 | + | 
|  | 42 | +        // THEN | 
|  | 43 | +        XCTAssertEqual(subject.value, "updated") | 
|  | 44 | +    } | 
|  | 45 | + | 
|  | 46 | +    func test_binding_worksWithComplexType() { | 
|  | 47 | +        struct TestModel { | 
|  | 48 | +            var name: String | 
|  | 49 | +            var count: Int | 
|  | 50 | +        } | 
|  | 51 | + | 
|  | 52 | +        let initialModel = TestModel(name: "test", count: 5) | 
|  | 53 | +        let subject = CurrentValueSubject<TestModel, Never>(initialModel) | 
|  | 54 | +        let binding = subject.binding | 
|  | 55 | + | 
|  | 56 | +        XCTAssertEqual(binding.wrappedValue.name, "test") | 
|  | 57 | +        XCTAssertEqual(binding.wrappedValue.count, 5) | 
|  | 58 | + | 
|  | 59 | +        // WHEN | 
|  | 60 | +        let updatedModel = TestModel(name: "updated", count: 10) | 
|  | 61 | +        binding.wrappedValue = updatedModel | 
|  | 62 | + | 
|  | 63 | +        // THEN | 
|  | 64 | +        XCTAssertEqual(subject.value.name, "updated") | 
|  | 65 | +        XCTAssertEqual(subject.value.count, 10) | 
|  | 66 | +    } | 
|  | 67 | + | 
|  | 68 | +    func test_binding_worksWithBooleanValue() { | 
|  | 69 | +        let subject = CurrentValueSubject<Bool, Never>(false) | 
|  | 70 | +        let binding = subject.binding | 
|  | 71 | + | 
|  | 72 | +        XCTAssertFalse(binding.wrappedValue) | 
|  | 73 | + | 
|  | 74 | +        // WHEN | 
|  | 75 | +        binding.wrappedValue = true | 
|  | 76 | + | 
|  | 77 | +        // THEN | 
|  | 78 | +        XCTAssertTrue(subject.value) | 
|  | 79 | +    } | 
|  | 80 | + | 
|  | 81 | +    func test_binding_worksWithOptionalValue() { | 
|  | 82 | +        let subject = CurrentValueSubject<Int?, Never>(nil) | 
|  | 83 | +        let binding = subject.binding | 
|  | 84 | + | 
|  | 85 | +        XCTAssertNil(binding.wrappedValue) | 
|  | 86 | + | 
|  | 87 | +        // WHEN | 
|  | 88 | +        binding.wrappedValue = 100 | 
|  | 89 | + | 
|  | 90 | +        // THEN | 
|  | 91 | +        XCTAssertEqual(subject.value, 100) | 
|  | 92 | + | 
|  | 93 | +        // WHEN | 
|  | 94 | +        binding.wrappedValue = nil | 
|  | 95 | + | 
|  | 96 | +        // THEN | 
|  | 97 | +        XCTAssertNil(subject.value) | 
|  | 98 | +    } | 
|  | 99 | +} | 
0 commit comments