-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Looking for Feedback] Add FXIOS-10164 [Homepage] More Pocket Section Tests #22643
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Foundation | ||
|
||
@testable import Client | ||
|
||
final class MockPocketManager: PocketManagerProvider { | ||
var getPocketItemsCalled = 0 | ||
func getPocketItems() async -> [PocketStoryState] { | ||
getPocketItemsCalled += 1 | ||
let stories: [PocketFeedStory] = [ | ||
.make(title: "feed1"), | ||
.make(title: "feed2"), | ||
.make(title: "feed3"), | ||
] | ||
|
||
return stories.compactMap { PocketStoryState(story: PocketStory(pocketFeedStory: $0)) } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import XCTest | ||
|
||
@testable import Client | ||
|
||
final class PocketManagerTests: XCTestCase { | ||
func test_getPocketItems_withSuccess_returnExpectedStories() async { | ||
let subject = createSubject( | ||
with: MockPocketAPI(result: .success(getMockStoriesData())) | ||
) | ||
let stories = await subject.getPocketItems() | ||
XCTAssertEqual(stories.count, 3) | ||
} | ||
|
||
func test_getPocketItems_withSucess_returnEmptyStories() async { | ||
let subject = createSubject( | ||
with: MockPocketAPI(result: .success([])) | ||
) | ||
let stories = await subject.getPocketItems() | ||
XCTAssertEqual(stories.count, 0) | ||
} | ||
|
||
func test_getPocketItems_withFailure_returnEmptyStories() async { | ||
let subject = createSubject( | ||
with: MockPocketAPI(result: .failure(TestError.example)) | ||
) | ||
let stories = await subject.getPocketItems() | ||
XCTAssertEqual(stories.count, 0) | ||
} | ||
|
||
private func createSubject( | ||
with pocketAPI: MockPocketAPI, | ||
file: StaticString = #file, | ||
line: UInt = #line | ||
) -> PocketManager { | ||
let subject = PocketManager(pocketAPI: pocketAPI) | ||
trackForMemoryLeaks(subject, file: file, line: line) | ||
return subject | ||
} | ||
|
||
private func getMockStoriesData() -> [PocketFeedStory] { | ||
return [ | ||
.make(title: "feed1"), | ||
.make(title: "feed2"), | ||
.make(title: "feed3"), | ||
] | ||
} | ||
|
||
enum TestError: Error { | ||
case example | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import Redux | ||
import XCTest | ||
|
||
@testable import Client | ||
|
||
final class PocketMiddlewareTests: XCTestCase, StoreTestUtility { | ||
let storeUtilityHelper = StoreTestUtilityHelper() | ||
let pocketManager = MockPocketManager() | ||
override func setUp() { | ||
super.setUp() | ||
DependencyHelperMock().bootstrapDependencies(injectedPocketManager: pocketManager) | ||
setupTestingStore() | ||
} | ||
|
||
override func tearDown() { | ||
DependencyHelperMock().reset() | ||
resetTestingStore() | ||
super.tearDown() | ||
} | ||
|
||
func test_initializeAction_getPocketData() async throws { | ||
let action = HomepageAction(windowUUID: .XCTestDefaultUUID, actionType: HomepageActionType.initialize) | ||
store.dispatch(action) | ||
try await Task.sleep(nanoseconds: 1_000_000_000) | ||
|
||
let homepageState = store.state.screenState( | ||
HomepageState.self, | ||
for: .homepage, | ||
window: .XCTestDefaultUUID | ||
) | ||
XCTAssertNotNil(homepageState) | ||
XCTAssertEqual(homepageState?.pocketState.pocketData.count, 3) | ||
XCTAssertEqual(pocketManager.getPocketItemsCalled, 1) | ||
} | ||
|
||
func test_enterForegroundAction_getPocketData() async throws { | ||
let action = PocketAction(windowUUID: .XCTestDefaultUUID, actionType: PocketActionType.enteredForeground) | ||
store.dispatch(action) | ||
try await Task.sleep(nanoseconds: 1_000_000_000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure how I feel about these sleeps, however, I am unable to think of another way without a larger refactor. If we could mock our store and monitor the dispatches, then we can use an expectation on that dispatch. In our current case our store is a global variable, so can't be mock properly and the alternative was to make it a var so that it can be overridden in tests. |
||
|
||
let homepageState = store.state.screenState( | ||
HomepageState.self, | ||
for: .homepage, | ||
window: .XCTestDefaultUUID | ||
) | ||
XCTAssertNotNil(homepageState) | ||
XCTAssertEqual(homepageState?.pocketState.pocketData.count, 3) | ||
XCTAssertEqual(pocketManager.getPocketItemsCalled, 1) | ||
} | ||
|
||
// MARK: StoreTestUtility | ||
func setupAppState() -> AppState { | ||
return AppState( | ||
activeScreens: ActiveScreensState( | ||
screens: [ | ||
.homepage( | ||
HomepageState( | ||
windowUUID: .XCTestDefaultUUID | ||
) | ||
), | ||
] | ||
) | ||
) | ||
} | ||
|
||
func setupTestingStore() { | ||
storeUtilityHelper.setupTestingStore( | ||
with: setupAppState(), | ||
middlewares: [PocketMiddleware().pocketSectionProvider] | ||
) | ||
} | ||
|
||
// In order to avoid flaky tests, we should reset the store | ||
// similar to production | ||
func resetTestingStore() { | ||
storeUtilityHelper.resetTestingStore() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to test these middlewares (more of an integration test over unit test); all dependencies will need to be injected through the
DependencyHelper
and this is because of how we are initializing our middlewares. Let me know if there's a better way to write these tests.