forked from All-Hands-AI/OpenHands
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issue All-Hands-AI#4941: [Bug]: Browser tab does not reset after …
…starting a new session (All-Hands-AI#4945) Co-authored-by: amanape <[email protected]>
- Loading branch information
1 parent
50e7da9
commit c555611
Showing
2 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { describe, it, expect, beforeEach, vi } from "vitest"; | ||
import { clearSession } from "../src/utils/clear-session"; | ||
import store from "../src/store"; | ||
import { initialState as browserInitialState } from "../src/state/browserSlice"; | ||
|
||
describe("clearSession", () => { | ||
beforeEach(() => { | ||
// Mock localStorage | ||
const localStorageMock = { | ||
getItem: vi.fn(), | ||
setItem: vi.fn(), | ||
removeItem: vi.fn(), | ||
clear: vi.fn(), | ||
}; | ||
vi.stubGlobal("localStorage", localStorageMock); | ||
|
||
// Set initial browser state to non-default values | ||
store.dispatch({ | ||
type: "browser/setUrl", | ||
payload: "https://example.com", | ||
}); | ||
store.dispatch({ | ||
type: "browser/setScreenshotSrc", | ||
payload: "base64screenshot", | ||
}); | ||
}); | ||
|
||
it("should clear localStorage and reset browser state", () => { | ||
clearSession(); | ||
|
||
// Verify localStorage items were removed | ||
expect(localStorage.removeItem).toHaveBeenCalledWith("token"); | ||
expect(localStorage.removeItem).toHaveBeenCalledWith("repo"); | ||
|
||
// Verify browser state was reset | ||
const state = store.getState(); | ||
expect(state.browser.url).toBe(browserInitialState.url); | ||
expect(state.browser.screenshotSrc).toBe(browserInitialState.screenshotSrc); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
import store from "#/store"; | ||
import { initialState as browserInitialState } from "#/state/browserSlice"; | ||
|
||
/** | ||
* Clear the session data from the local storage. This will remove the token and repo | ||
* Clear the session data from the local storage and reset relevant Redux state | ||
*/ | ||
export const clearSession = () => { | ||
// Clear local storage | ||
localStorage.removeItem("token"); | ||
localStorage.removeItem("repo"); | ||
|
||
// Reset browser state to initial values | ||
store.dispatch({ | ||
type: "browser/setUrl", | ||
payload: browserInitialState.url, | ||
}); | ||
store.dispatch({ | ||
type: "browser/setScreenshotSrc", | ||
payload: browserInitialState.screenshotSrc, | ||
}); | ||
}; |