Skip to content

Commit

Permalink
Update Reader cards fetch call to use async method
Browse files Browse the repository at this point in the history
  • Loading branch information
wargcm committed Mar 11, 2024
1 parent c007e00 commit 8b8d209
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 45 deletions.
24 changes: 6 additions & 18 deletions WordPressKit/ReaderPostServiceRemote+Cards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,12 @@ extension ReaderPostServiceRemote {
private func fetch(_ endpoint: String,
success: @escaping ([RemoteReaderCard], String?) -> Void,
failure: @escaping (Error) -> Void) {
wordPressComRestApi.GET(endpoint,
parameters: nil,
success: { response, _ in
do {
let decoder = JSONDecoder()
let data = try JSONSerialization.data(withJSONObject: response, options: [])
let envelope = try decoder.decode(ReaderCardEnvelope.self, from: data)

success(envelope.cards, envelope.nextPageHandle)
} catch {
WPKitLogError("Error parsing the reader cards response: \(error)")
failure(error)
}
}, failure: { error, _ in
WPKitLogError("Error fetching reader cards: \(error)")

failure(error)
})
Task { @MainActor [wordPressComRestApi] in
await wordPressComRestApi.perform(.get, URLString: endpoint, type: ReaderCardEnvelope.self)
.map { ($0.body.cards, $0.body.nextPageHandle) }
.mapError { error -> Error in error.asNSError() }
.execute(onSuccess: success, onFailure: failure)
}
}

private func cardsEndpoint(with path: String,
Expand Down
15 changes: 15 additions & 0 deletions WordPressKitTests/MockWordPressComRestApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class MockWordPressComRestApi: WordPressComRestApi {
@objc var successBlockPassedIn: ((AnyObject, HTTPURLResponse?) -> Void)?
@objc var failureBlockPassedIn: ((NSError, HTTPURLResponse?) -> Void)?

var performMethodCall: HTTPRequestBuilder.Method?

override func GET(_ URLString: String?, parameters: [String: AnyObject]?, success: @escaping ((AnyObject, HTTPURLResponse?) -> Void), failure: @escaping ((NSError, HTTPURLResponse?) -> Void)) -> Progress? {
getMethodCalled = true
URLStringPassedIn = URLString
Expand Down Expand Up @@ -44,6 +46,19 @@ class MockWordPressComRestApi: WordPressComRestApi {
return Progress()
}

override func perform<T: Decodable>(
_ method: HTTPRequestBuilder.Method,
URLString: String,
parameters: [String: AnyObject]? = nil,
fulfilling progress: Progress? = nil,
jsonDecoder: JSONDecoder? = nil,
type: T.Type = T.self
) async -> APIResult<T> {
performMethodCall = method
URLStringPassedIn = URLString
return .failure(.connection(.init(.cancelled)))
}

@objc func methodCalled() -> String {

var method = "Unknown"
Expand Down
85 changes: 58 additions & 27 deletions WordPressKitTests/ReaderPostServiceRemote+CardsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,51 +86,66 @@ class ReaderPostServiceRemoteCardTests: RemoteTestCase, RESTTestable {
waitForExpectations(timeout: timeout, handler: nil)
}

func testHTTPMethod() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchCards(for: ["dogs"], success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertEqual(mockRemoteApi.performMethodCall, .get)
}

// Calls the API with the given page handle
//
func testCallAPIWithTheGivenPageHandle() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchCards(for: ["dogs"], page: "foobar", success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchCards(for: ["dogs"], page: "foobar", success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("&page_handle=foobar") ?? false)
}

// Calls the API with .popularity as the given sorting option
//
func testCallAPIWithPopularityAsTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchCards(for: [], sortingOption: .popularity, success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchCards(for: [], sortingOption: .popularity, success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("sort=popularity") ?? false)
}

// Calls the API with .date as the given sorting option
//
func testCallAPIWithDateAsTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchCards(for: [], sortingOption: .date, success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchCards(for: [], sortingOption: .date, success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("sort=date") ?? false)
}

// Calls the API without the given sorting option
//
func testCallAPIWithoutTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchCards(for: [], success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchCards(for: [], success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertFalse(mockRemoteApi.URLStringPassedIn?.contains("sort=") ?? true)
}

Expand Down Expand Up @@ -221,54 +236,70 @@ class ReaderPostServiceRemoteCardTests: RemoteTestCase, RESTTestable {
waitForExpectations(timeout: timeout, handler: nil)
}

func testStreamsCallAPIWithTheGivenPageHandle() {
func testStreamsHTTPMethod() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchStreamCards(for: ["dogs"], page: "foobar", success: { _, _ in }, failure: { _ in })
readerPostServiceRemote.fetchStreamCards(for: ["dogs"], success: { _, _ in }, failure: failure)

XCTAssertTrue(mockRemoteApi.getMethodCalled)
waitForExpectations(timeout: timeout)
XCTAssertEqual(mockRemoteApi.performMethodCall, .get)
}

func testStreamsCallAPIWithTheGivenPageHandle() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchStreamCards(for: ["dogs"], page: "foobar", success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("&page_handle=foobar") ?? false)
}

func testStreamsCallAPIWithPopularityAsTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchStreamCards(for: [], sortingOption: .popularity, success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchStreamCards(for: [], sortingOption: .popularity, success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("sort=popularity") ?? false)
}

func testStreamsCallAPIWithDateAsTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchStreamCards(for: [], sortingOption: .date, success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchStreamCards(for: [], sortingOption: .date, success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("sort=date") ?? false)
}

func testStreamsCallAPIWithoutTheGivenSortingOption() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)

readerPostServiceRemote.fetchStreamCards(for: [], success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchStreamCards(for: [], success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertFalse(mockRemoteApi.URLStringPassedIn?.contains("sort=") ?? true)
}

func testStreamsCallAPIWithCountValue() {
let expect = expectation(description: "Executes fetch call")
let failure: (Error) -> Void = { _ in expect.fulfill() }
let readerPostServiceRemote = ReaderPostServiceRemote(wordPressComRestApi: mockRemoteApi)
let expectedCount = 5

readerPostServiceRemote.fetchStreamCards(for: ["dogs"], count: expectedCount, success: { _, _ in }, failure: { _ in })

XCTAssertTrue(mockRemoteApi.getMethodCalled)
readerPostServiceRemote.fetchStreamCards(for: ["dogs"], count: expectedCount, success: { _, _ in }, failure: failure)

waitForExpectations(timeout: timeout)
XCTAssertTrue(mockRemoteApi.URLStringPassedIn?.contains("&count=\(expectedCount)") ?? false)
}

Expand Down

0 comments on commit 8b8d209

Please sign in to comment.