Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdchr committed May 6, 2024
1 parent fb0bda8 commit 59be791
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ReaderInterestsDataSource {
}

/// Fetches the interests from the topic service
public func reload() {
public func reload(completion: (() -> Void)? = nil) {
interestsService.fetchInterests(success: { [weak self] interests in
guard let self = self else {
return
Expand All @@ -88,13 +88,15 @@ class ReaderInterestsDataSource {
})
}
.map { ReaderInterestViewModel(interest: $0) }
completion?()
}

}) { [weak self] (error: Error) in
DDLogError("Error: Could not retrieve reader interests: \(String(describing: error))")

Task { @MainActor [weak self] in
self?.interests = []
completion?()
}
}
}
Expand Down
52 changes: 35 additions & 17 deletions WordPress/WordPressTest/ReaderInterestsDataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,16 @@ class ReaderInterestsDataSourceTests: XCTestCase {
let dataSource = ReaderInterestsDataSource(topics: [], service: service)

let successExpectation = expectation(description: "Fetching of interests succeeds")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = true
service.fetchSuccessExpectation = successExpectation

dataSource.reload()

wait(for: [successExpectation], timeout: 4)
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [successExpectation, reloadExpectation], timeout: 4)
XCTAssertEqual(dataSource.count, 1)
}

Expand All @@ -79,43 +81,50 @@ class ReaderInterestsDataSourceTests: XCTestCase {
let dataSource = ReaderInterestsDataSource(topics: [], service: service)

let failureExpectation = expectation(description: "Fetching of interests fails")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = false
service.fetchFailureExpectation = failureExpectation

dataSource.reload()

wait(for: [failureExpectation], timeout: 4)
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [failureExpectation, reloadExpectation], timeout: 4)
XCTAssertEqual(dataSource.count, 0)
}

func testInterestsDataSourceDelegateIsCalled() {
let reloadExpectation = expectation(description: "Reload succeeds")
let delegateExpectation = expectation(description: "DataSource delegate is called sucessfully")
let delegate = MockInterestsDelegate(delegateExpectation)

let service = MockInterestsService()
let dataSource = ReaderInterestsDataSource(topics: [], service: service)
dataSource.delegate = delegate

dataSource.reload()

wait(for: [delegateExpectation], timeout: 4)
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [delegateExpectation, reloadExpectation], timeout: 4)
XCTAssertEqual(dataSource.count, 1)
}

func testDataSourceInterestInterestFor() throws {
let service = MockInterestsService()
let dataSource = ReaderInterestsDataSource(topics: [], service: service)
let successExpectation = expectation(description: "Fetching of interests succeeds")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = true
service.fetchSuccessExpectation = successExpectation

dataSource.reload()
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [successExpectation], timeout: 4)
wait(for: [successExpectation, reloadExpectation], timeout: 4)

let interest = try XCTUnwrap(dataSource.interest(for: 0), "Expected interest at index 0 to exist")
XCTAssertEqual(interest.title, Constants.testTitle)
Expand All @@ -127,13 +136,16 @@ class ReaderInterestsDataSourceTests: XCTestCase {
let service = MockInterestsService()
let dataSource = ReaderInterestsDataSource(topics: [], service: service)
let successExpectation = expectation(description: "Fetching of interests succeeds")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = true
service.fetchSuccessExpectation = successExpectation

dataSource.reload()
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [successExpectation], timeout: 4)
wait(for: [successExpectation, reloadExpectation], timeout: 4)

// Queries for invalid index should return nil instead of crashing.
XCTAssertNil(dataSource.interest(for: 1))
Expand All @@ -143,13 +155,16 @@ class ReaderInterestsDataSourceTests: XCTestCase {
let service = MockInterestsService()
let dataSource = ReaderInterestsDataSource(topics: [], service: service)
let successExpectation = expectation(description: "Fetching of interests succeeds")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = true
service.fetchSuccessExpectation = successExpectation

dataSource.reload()
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [successExpectation], timeout: 4)
wait(for: [successExpectation, reloadExpectation], timeout: 4)

// Toggle on
let interest = try XCTUnwrap(dataSource.interest(for: 0), "Expected interest at index 0 to exist")
Expand All @@ -169,13 +184,16 @@ class ReaderInterestsDataSourceTests: XCTestCase {
let service = MockInterestsService()
let dataSource = ReaderInterestsDataSource(topics: [], service: service)
let successExpectation = expectation(description: "Fetching of interests succeeds")
let reloadExpectation = expectation(description: "Reload succeeds")

service.success = true
service.fetchSuccessExpectation = successExpectation

dataSource.reload()
dataSource.reload {
reloadExpectation.fulfill()
}

wait(for: [successExpectation], timeout: 4)
wait(for: [successExpectation, reloadExpectation], timeout: 4)

// Toggle on
let interest = try XCTUnwrap(dataSource.interest(for: 0), "Expected interest at index 0 to exist")
Expand Down

0 comments on commit 59be791

Please sign in to comment.