Last active
April 13, 2020 23:14
-
-
Save avegancafe/8cb711db485bdb5d14d9934d7c34737d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*: | |
| # A Case of the Missing Unit Test */ | |
| /*: | |
| ### Type Defs | |
| */ | |
| struct Payload { | |
| let url: String | |
| } | |
| struct FetchListingsAction { | |
| let type: String | |
| let payload: Payload | |
| } | |
| struct Coordinates { | |
| let lat: Float | |
| let lng: Float | |
| } | |
| typealias Dispatcher = ( | |
| fetchListings: (Int, Bool, String, Coordinates) -> Void, | |
| fetchBuildingSearchResults: () -> Void | |
| ) | |
| struct Actions { | |
| let fetchListings: (Int, Bool, String, Coordinates) -> FetchListingsAction | |
| } | |
| struct Routes { | |
| let listing: (Int, Bool, String, Coordinates) -> String | |
| } | |
| /*: | |
| ### App Code | |
| */ | |
| let routes = Routes( | |
| listing: { (pageNumber: Int, neighborhoods: Bool, sort: String, mapCoordinates: Coordinates) -> String in | |
| return "/api/listings?pageNumber=\(pageNumber)&neighborhoods=\(neighborhoods)&sort=\(sort)&coordinates=\(mapCoordinates)" | |
| } | |
| ) | |
| let actions = Actions( | |
| fetchListings: {(pageNumber, neighborhoods, sort, mapCoordinates) -> FetchListingsAction in | |
| return FetchListingsAction( | |
| type: "[Fetch] listings", | |
| payload: Payload( | |
| url: routes.listing(pageNumber, neighborhoods, sort, mapCoordinates) | |
| ) | |
| ) | |
| } | |
| ) | |
| let mapDispatchToProps = {(dispatch: @escaping (FetchListingsAction) -> Void) -> Dispatcher in | |
| return ( | |
| fetchListings: {(pageNumber, neighborhoods, sort, mapCoordinates) -> Void in | |
| dispatch(actions.fetchListings( | |
| pageNumber, | |
| neighborhoods, | |
| sort, | |
| mapCoordinates | |
| )) | |
| }, | |
| fetchBuildingSearchResults: {} | |
| ) | |
| } | |
| mapDispatchToProps({(action) in return}).fetchListings(0, true, "", Coordinates(lat: 0.0, lng: 0.0)) | |
| /*: | |
| ### Simplified Example | |
| */ | |
| typealias Fn1 = (String, Int, Bool) -> Void | |
| typealias Fn2 = (String, Int, Bool) -> Void | |
| typealias Fn3 = (String, Int, Bool) -> Void | |
| let fn3: Fn3 = { (name, age, developer) in | |
| print("name: \(name), age: \(age), developer: \(developer)") | |
| } | |
| let fn2: Fn2 = { (name, age, developer) in | |
| return fn3(name, age, developer) | |
| } | |
| let fn1: Fn1 = { (name, age, developer) in | |
| return fn2(name, age, developer) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment