Skip to content

Commit

Permalink
Add test for backForwardButtonStateChanged
Browse files Browse the repository at this point in the history
  • Loading branch information
thatswinnie committed Nov 14, 2024
1 parent 0bc5bc5 commit bd2a214
Showing 1 changed file with 95 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
override func setUp() {
super.setUp()
DependencyHelperMock().bootstrapDependencies()
setupTestingStore()
}

override func tearDown() {
Expand All @@ -25,6 +24,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func tests_initialState_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()

XCTAssertEqual(initialState.windowUUID, windowUUID)
Expand All @@ -46,6 +46,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_didLoadToolbarsAction_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand Down Expand Up @@ -83,6 +84,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_numberOfTabsChangedAction_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand All @@ -104,6 +106,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_readerModeStateChangedAction_onHomepage_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand All @@ -122,6 +125,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_readerModeStateChangedAction_onWebsite_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand All @@ -144,6 +148,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_websiteLoadingStateDidChangeAction_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand All @@ -164,6 +169,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func test_urlDidChangeAction_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

Expand All @@ -181,6 +187,50 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
XCTAssertEqual(newState.browserActions[2].actionType, .menu)
}

func test_backForwardButtonStateChangedAction_withNavigationToolbar_returnsExpectedState() {
setupTestingStore()
let initialState = createSubject()
let reducer = addressBarReducer()

let urlDidChangeState = loadWebsiteAction(state: initialState, reducer: reducer)
let newState = reducer(
urlDidChangeState,
ToolbarAction(
canGoBack: true,
canGoForward: false,
windowUUID: windowUUID,
actionType: ToolbarActionType.backForwardButtonStateChanged
)
)

XCTAssertEqual(newState.windowUUID, windowUUID)
XCTAssertEqual(newState.navigationActions.count, 0)
}

func test_backForwardButtonStateChangedAction_withoutNavigationToolbar_returnsExpectedState() {
setupTestingStore(with: initialToolbarState(isShowingNavigationToolbar: false))
let initialState = createSubject()
let reducer = addressBarReducer()

let urlDidChangeState = loadWebsiteAction(state: initialState, reducer: reducer)
let newState = reducer(
urlDidChangeState,
ToolbarAction(
canGoBack: true,
canGoForward: false,
windowUUID: windowUUID,
actionType: ToolbarActionType.backForwardButtonStateChanged
)
)

XCTAssertEqual(newState.windowUUID, windowUUID)
XCTAssertEqual(newState.navigationActions.count, 2)
XCTAssertEqual(newState.navigationActions[0].actionType, .back)
XCTAssertEqual(newState.navigationActions[0].isEnabled, true)
XCTAssertEqual(newState.navigationActions[1].actionType, .forward)
XCTAssertEqual(newState.navigationActions[1].isEnabled, false)
}

func test_clearSearchAction_returnsExpectedState() {
let initialState = createSubject()
let reducer = addressBarReducer()
Expand Down Expand Up @@ -285,6 +335,48 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
)
}

// MARK: Helper
func setupAppState(with initialToolbarState: ToolbarState) -> AppState {
return AppState(
activeScreens: ActiveScreensState(
screens: [
.browserViewController(
BrowserViewControllerState(
windowUUID: windowUUID
)
),
.toolbar(initialToolbarState)
]
)
)
}

func setupTestingStore(with initialToolbarState: ToolbarState) {
StoreTestUtilityHelper.setupTestingStore(
with: setupAppState(with: initialToolbarState),
middlewares: [ToolbarMiddleware().toolbarProvider]
)
}

func initialToolbarState(isShowingNavigationToolbar: Bool) -> ToolbarState {
let toolbarState = ToolbarState(windowUUID: windowUUID)
return ToolbarState(
windowUUID: windowUUID,
toolbarPosition: toolbarState.toolbarPosition,
isPrivateMode: toolbarState.isPrivateMode,
addressToolbar: toolbarState.addressToolbar,
navigationToolbar: toolbarState.navigationToolbar,
isShowingNavigationToolbar: isShowingNavigationToolbar,
isShowingTopTabs: toolbarState.isShowingTopTabs,
canGoBack: toolbarState.canGoBack,
canGoForward: toolbarState.canGoForward,
numberOfTabs: toolbarState.numberOfTabs,
showMenuWarningBadge: toolbarState.showMenuWarningBadge,
isNewTabFeatureEnabled: toolbarState.isNewTabFeatureEnabled,
canShowDataClearanceAction: toolbarState.canShowDataClearanceAction,
canShowNavigationHint: toolbarState.canShowNavigationHint)
}

// MARK: StoreTestUtility
func setupAppState() -> AppState {
return AppState(
Expand All @@ -306,7 +398,7 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
}

func setupTestingStore() {
storeUtilityHelper.setupTestingStore(
StoreTestUtilityHelper.setupTestingStore(
with: setupAppState(),
middlewares: [ToolbarMiddleware().toolbarProvider]
)
Expand All @@ -315,6 +407,6 @@ final class AddressBarStateTests: XCTestCase, StoreTestUtility {
// In order to avoid flaky tests, we should reset the store
// similar to production
func resetTestingStore() {
storeUtilityHelper.resetTestingStore()
StoreTestUtilityHelper.resetTestingStore()
}
}

0 comments on commit bd2a214

Please sign in to comment.