-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use session storage to persist filter and search (#3114)
* saving urlParams to session storage, testing retrieval and setting * save to session storage on name click, apply to back button * add tests for new session storage utils * typo fix * Add playwright test, fix session storage undefined error * Add undefined return type and return undefined if session storage doesnt exist to fix error * change undefined return to null * update description * mock session storage for playwright test * update to use locator method * nevermind on the e2e for sessionStorage
- Loading branch information
1 parent
2055a30
commit 7c75a98
Showing
5 changed files
with
111 additions
and
5 deletions.
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
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,34 @@ | ||
/** | ||
* Saves a key-value pair to session storage. | ||
* @param key - The key under which the value will be stored. | ||
* @param value - The value to be stored. Can be a string or any serializable object. | ||
*/ | ||
export const saveToSessionStorage = ( | ||
key: string, | ||
value: string | object, | ||
): void => { | ||
const serializedValue = JSON.stringify(value); | ||
|
||
if (typeof window !== "undefined" && window.sessionStorage) { | ||
sessionStorage.setItem(key, serializedValue); | ||
} else { | ||
console.warn("sessionStorage is not available"); | ||
} | ||
}; | ||
|
||
/** | ||
* Retrieves a key-value pair from session storage. | ||
* @param key - The key under which the value was stored. | ||
* @returns string or null - The stored value from session storage or null if it finds nothing | ||
*/ | ||
export const retrieveFromSessionStorage = ( | ||
key: string, | ||
): string | object | null => { | ||
if (typeof window !== "undefined" && window.sessionStorage) { | ||
const storedValue = sessionStorage.getItem(key); | ||
return JSON.parse(<string>storedValue); | ||
} else { | ||
console.warn("sessionStorage is not available"); | ||
return null; | ||
} | ||
}; |
62 changes: 62 additions & 0 deletions
62
containers/ecr-viewer/src/app/tests/components/Utils.test.tsx
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,62 @@ | ||
import { | ||
saveToSessionStorage, | ||
retrieveFromSessionStorage, | ||
} from "../../components/utils.ts"; | ||
|
||
describe("Session Storage saving utils", () => { | ||
beforeEach(() => { | ||
sessionStorage.clear(); | ||
}); | ||
|
||
describe("saveToSessionStorage", () => { | ||
it("should save a string value to session storage", () => { | ||
const key = "Bread"; | ||
const value = "Baguette"; | ||
saveToSessionStorage(key, value); | ||
|
||
expect(sessionStorage.getItem(key)).toBe(JSON.stringify(value)); | ||
}); | ||
|
||
it("should save an object value to session storage", () => { | ||
const key = "testKey"; | ||
const value = { name: "Heironymous", age: 37 }; | ||
saveToSessionStorage(key, value); | ||
|
||
expect(sessionStorage.getItem(key)).toBe(JSON.stringify(value)); | ||
}); | ||
}); | ||
|
||
describe("retrieveFromSessionStorage", () => { | ||
it("should retrieve a string value from session storage", () => { | ||
const key = "fruit"; | ||
const value = "Apples"; | ||
sessionStorage.setItem(key, JSON.stringify(value)); | ||
|
||
const retrievedValue = retrieveFromSessionStorage(key); | ||
expect(retrievedValue).toBe(value); | ||
}); | ||
|
||
it("should retrieve an object value from session storage", () => { | ||
const key = "patient"; | ||
const value = { name: "Arabelle", age: 22 }; | ||
sessionStorage.setItem(key, JSON.stringify(value)); | ||
|
||
const retrievedValue = retrieveFromSessionStorage(key); | ||
expect(retrievedValue).toEqual(value); | ||
}); | ||
|
||
it("should return null if the key does not exist", () => { | ||
const key = "nonExistentKey"; | ||
const retrievedValue = retrieveFromSessionStorage(key); | ||
|
||
expect(retrievedValue).toBeNull(); | ||
}); | ||
|
||
it("should throw an error for invalid JSON in session storage", () => { | ||
const key = "aKey"; | ||
sessionStorage.setItem(key, "invalid-json"); | ||
|
||
expect(() => retrieveFromSessionStorage(key)).toThrow(SyntaxError); | ||
}); | ||
}); | ||
}); |
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
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