-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend] setStackDataInSessionStorage created with tests (#3167)
- Loading branch information
1 parent
8ea9547
commit 0daa9c9
Showing
3 changed files
with
96 additions
and
16 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
71 changes: 71 additions & 0 deletions
71
...rc/utils/sessionStorage/setStackDataInSessionStorage/setStackDataInSessionStorage.test.ts
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,71 @@ | ||
import { describe, expect, it, beforeEach, afterEach } from 'vitest'; | ||
import setStackDataInSessionStorage from './setStackDataInSessionStorage'; | ||
|
||
describe('Session Storage', () => { | ||
describe('setStackDataInSessionStorage', () => { | ||
const testKey = 'testKey'; | ||
const stackValue = 2; | ||
|
||
afterEach(() => { | ||
sessionStorage.removeItem('testKey'); | ||
}); | ||
|
||
describe('When I set a data in session storage', () => { | ||
beforeEach(() => { | ||
setStackDataInSessionStorage({ | ||
valueToStore: { test: 'value1' }, | ||
key: testKey, | ||
stackValue, | ||
}); | ||
}); | ||
|
||
describe('If I get datas with my key from session storage', () => { | ||
it('contains the my value', () => { | ||
const storedData = sessionStorage.getItem('testKey'); | ||
expect(storedData).to.not.equal(null); | ||
|
||
if (storedData) { | ||
const parsedStoredData = JSON.parse(storedData); | ||
expect(parsedStoredData[0].test).to.equal('value1'); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('When I set three data in session storage with a stack value of 2', () => { | ||
beforeEach(() => { | ||
Array.from(Array(3).keys()).forEach((_, i) => { | ||
setStackDataInSessionStorage({ | ||
valueToStore: { test: `value${i}` }, | ||
key: testKey, | ||
stackValue, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('If I get datas with my key from session storage', () => { | ||
it('does not contains my first value', () => { | ||
const storedData = sessionStorage.getItem('testKey'); | ||
expect(storedData).to.not.equal(null); | ||
|
||
if (storedData) { | ||
const parsedStoredData = JSON.parse(storedData); | ||
expect(parsedStoredData[2]).to.equal(undefined); | ||
} | ||
}); | ||
|
||
it('contains my two last values', () => { | ||
const storedData = sessionStorage.getItem('testKey'); | ||
expect(storedData).to.not.equal(null); | ||
|
||
if (storedData) { | ||
const parsedStoredData = JSON.parse(storedData); | ||
expect(parsedStoredData[0].test).to.equal('value2'); | ||
expect(parsedStoredData[1].test).to.equal('value1'); | ||
expect(parsedStoredData[2]).to.equal(undefined); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
17 changes: 17 additions & 0 deletions
17
...ont/src/utils/sessionStorage/setStackDataInSessionStorage/setStackDataInSessionStorage.ts
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,17 @@ | ||
type SetDataOnSessionStorageParams = { | ||
valueToStore: unknown; | ||
key: string; | ||
stackValue: number; | ||
}; | ||
const setStackDataInSessionStorage = ({ valueToStore, key, stackValue }: SetDataOnSessionStorageParams): void => { | ||
const storedStackData = sessionStorage.getItem(key); | ||
const currentStoredStackData = storedStackData ? JSON.parse(storedStackData) : []; | ||
|
||
currentStoredStackData.unshift(valueToStore); | ||
|
||
if (currentStoredStackData.length > stackValue) currentStoredStackData.pop(); | ||
|
||
sessionStorage.setItem(key, JSON.stringify(currentStoredStackData)); | ||
}; | ||
|
||
export default setStackDataInSessionStorage; |