Skip to content

Instantly share code, notes, and snippets.

@mayoff
Created July 19, 2021 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayoff/97c710d9dc61a8d96ef2924e6176f7a0 to your computer and use it in GitHub Desktop.
Save mayoff/97c710d9dc61a8d96ef2924e6176f7a0 to your computer and use it in GitHub Desktop.
for pointfree episode 153
@testable import SwiftUICaseStudies
import XCTest
class RefreshableTests: XCTestCase {
func testVanilla() async {
var k: CheckedContinuation<Void, Never>? = nil
let viewModel = PullToRefreshViewModel(
fetch: {
await withCheckedContinuation {
k = $0
}
return "\($0) is a good number."
}
)
viewModel.incrementButtonTapped()
XCTAssertEqual(viewModel.count, 1)
let task = Task {
await viewModel.getFact()
}
while k == nil {
await Task.yield()
}
XCTAssertEqual(viewModel.isLoading, true)
k!.resume()
await task.value
XCTAssertEqual(viewModel.fact, "1 is a good number.")
XCTAssertEqual(viewModel.isLoading, false)
}
func testVanilla_Cancellation() async {
var k: CheckedContinuation<Void, Never>? = nil
let viewModel = PullToRefreshViewModel(
fetch: {
await withCheckedContinuation {
k = $0
}
return "\($0) is a good number."
}
)
viewModel.incrementButtonTapped()
XCTAssertEqual(viewModel.count, 1)
Task {
await viewModel.getFact()
}
while k == nil {
await Task.yield()
}
XCTAssertEqual(viewModel.isLoading, true)
viewModel.cancelButtonTapped()
XCTAssertNil(viewModel.fact)
XCTAssertEqual(viewModel.isLoading, false)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment