|
| 1 | +@testable import ApexyLoader |
| 2 | +import Apexy |
| 3 | +import XCTest |
| 4 | + |
| 5 | +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 6 | +final class WebLoaderTests: XCTestCase { |
| 7 | + |
| 8 | + private var mockClient: MockClient! |
| 9 | + private var webLoader: WebLoader<String>! |
| 10 | + |
| 11 | + override func setUp() { |
| 12 | + super.setUp() |
| 13 | + mockClient = MockClient() |
| 14 | + webLoader = WebLoader(apiClient: mockClient) |
| 15 | + } |
| 16 | + |
| 17 | + override func tearDown() { |
| 18 | + webLoader = nil |
| 19 | + mockClient = nil |
| 20 | + super.tearDown() |
| 21 | + } |
| 22 | + |
| 23 | + // MARK: - Async Request Tests |
| 24 | + |
| 25 | + func testAsyncRequestSuccess() async { |
| 26 | + // Given |
| 27 | + let expectedContent = "Test Content" |
| 28 | + mockClient.mockResult = .success(expectedContent) |
| 29 | + let endpoint = MockEndpoint() |
| 30 | + |
| 31 | + // When |
| 32 | + guard webLoader.startLoading() else { |
| 33 | + XCTFail("Should be able to start loading") |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + await webLoader.request(endpoint) |
| 38 | + |
| 39 | + // Then |
| 40 | + switch webLoader.state { |
| 41 | + case .success(let content): |
| 42 | + XCTAssertEqual(content, expectedContent) |
| 43 | + default: |
| 44 | + XCTFail("Expected success state, got \(webLoader.state)") |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + func testAsyncRequestFailure() async { |
| 49 | + // Given |
| 50 | + let expectedError = URLError(.networkConnectionLost) |
| 51 | + mockClient.mockResult = .failure(expectedError) |
| 52 | + let endpoint = MockEndpoint() |
| 53 | + |
| 54 | + // When |
| 55 | + guard webLoader.startLoading() else { |
| 56 | + XCTFail("Should be able to start loading") |
| 57 | + return |
| 58 | + } |
| 59 | + |
| 60 | + await webLoader.request(endpoint) |
| 61 | + |
| 62 | + // Then |
| 63 | + switch webLoader.state { |
| 64 | + case .failure(let error, let cache): |
| 65 | + XCTAssertEqual(error as? URLError, expectedError) |
| 66 | + XCTAssertNil(cache) |
| 67 | + default: |
| 68 | + XCTFail("Expected failure state, got \(webLoader.state)") |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + func testAsyncRequestWithTransformation() async { |
| 73 | + // Given |
| 74 | + let rawContent = "Raw Content" |
| 75 | + let transformedContent = "Transformed: Raw Content" |
| 76 | + mockClient.mockResult = .success(rawContent) |
| 77 | + let endpoint = MockEndpoint() |
| 78 | + |
| 79 | + // When |
| 80 | + guard webLoader.startLoading() else { |
| 81 | + XCTFail("Should be able to start loading") |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + await webLoader.request(endpoint) { raw in |
| 86 | + return "Transformed: \(raw)" |
| 87 | + } |
| 88 | + |
| 89 | + // Then |
| 90 | + switch webLoader.state { |
| 91 | + case .success(let content): |
| 92 | + XCTAssertEqual(content, transformedContent) |
| 93 | + default: |
| 94 | + XCTFail("Expected success state, got \(webLoader.state)") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + func testAsyncRequestWithCompletion() async { |
| 99 | + // Given |
| 100 | + let expectedContent = "Test Content" |
| 101 | + mockClient.mockResult = .success(expectedContent) |
| 102 | + let endpoint = MockEndpoint() |
| 103 | + var completionCalled = false |
| 104 | + var receivedResult: Result<String, Error>? |
| 105 | + |
| 106 | + // When |
| 107 | + guard webLoader.startLoading() else { |
| 108 | + XCTFail("Should be able to start loading") |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + await webLoader.request(endpoint) { result in |
| 113 | + completionCalled = true |
| 114 | + receivedResult = result |
| 115 | + } |
| 116 | + |
| 117 | + // Then |
| 118 | + XCTAssertTrue(completionCalled) |
| 119 | + switch receivedResult { |
| 120 | + case .success(let content): |
| 121 | + XCTAssertEqual(content, expectedContent) |
| 122 | + case .failure: |
| 123 | + XCTFail("Expected success result") |
| 124 | + case .none: |
| 125 | + XCTFail("Expected result to be set") |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + func testAsyncRequestWithoutStartLoading() async { |
| 130 | + // Given |
| 131 | + let endpoint = MockEndpoint() |
| 132 | + |
| 133 | + // When |
| 134 | + await webLoader.request(endpoint) |
| 135 | + |
| 136 | + // Then |
| 137 | + // The request method should still work even without startLoading |
| 138 | + // but it will call finishLoading which changes the state |
| 139 | + switch webLoader.state { |
| 140 | + case .success(let content): |
| 141 | + XCTAssertEqual(content, "Mock Content") |
| 142 | + default: |
| 143 | + XCTFail("Expected success state, got \(webLoader.state)") |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + func testAsyncLoadMethod() async { |
| 148 | + // Given |
| 149 | + let expectedContent = "Test Content" |
| 150 | + mockClient.mockResult = .success(expectedContent) |
| 151 | + let endpoint = MockEndpoint() |
| 152 | + |
| 153 | + // Create a custom loader that has a custom load method |
| 154 | + let customLoader = CustomWebLoader(apiClient: mockClient, endpoint: endpoint) |
| 155 | + |
| 156 | + // When |
| 157 | + await customLoader.customLoad() |
| 158 | + |
| 159 | + // Then |
| 160 | + switch customLoader.state { |
| 161 | + case .success(let content): |
| 162 | + XCTAssertEqual(content, expectedContent) |
| 163 | + default: |
| 164 | + XCTFail("Expected success state, got \(customLoader.state)") |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 172 | +private final class CustomWebLoader: WebLoader<String> { |
| 173 | + private let testEndpoint: MockEndpoint |
| 174 | + |
| 175 | + init(apiClient: Client, endpoint: MockEndpoint) { |
| 176 | + self.testEndpoint = endpoint |
| 177 | + super.init(apiClient: apiClient) |
| 178 | + } |
| 179 | + |
| 180 | + func customLoad() async { |
| 181 | + guard startLoading() else { return } |
| 182 | + await request(testEndpoint) |
| 183 | + } |
| 184 | +} |
0 commit comments