Skip to content

Commit

Permalink
[frontend] setStackDataInSessionStorage created with tests (#3167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinBouzinFiligran committed Apr 8, 2024
1 parent 8ea9547 commit 0daa9c9
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import InvestigationExpandForm from './InvestigationExpandForm';
import inject18n from '../../../../components/i18n';
import { commitMutation, fetchQuery, MESSAGING$ } from '../../../../relay/environment';
import { hexToRGB } from '../../../../utils/Colors';
import setStackDataInSessionStorage from '../../../../utils/sessionStorage/setStackDataInSessionStorage/setStackDataInSessionStorage';
import {
applyFilters,
buildGraphData,
Expand Down Expand Up @@ -1836,7 +1837,13 @@ class InvestigationGraphComponent extends Component {
this.fetchObjectRelCounts(newElements);
}
if (newElementsIds.length > 0) {
this.setPreExpansionStateOnSessionStorage(newElementsIds);
setStackDataInSessionStorage({ valueToStore: {
dateTime: new Date().getTime(),
investigatedEntitiesIdsList: newElementsIds,
},
key: 'preExpansionStateList',
stackValue: 10 });

this.graphData = buildGraphData(
this.graphObjects,
decodeGraphData(this.props.workspace.graph_data),
Expand Down Expand Up @@ -1881,21 +1888,6 @@ class InvestigationGraphComponent extends Component {
this.handleToggleDisplayProgress();
}

// eslint-disable-next-line class-methods-use-this
setPreExpansionStateOnSessionStorage(investigatedEntitiesIdsList) {
const storedPreExpansion = sessionStorage.getItem('preExpansionStateList');
const currentStoredPreExpansion = storedPreExpansion ? JSON.parse(storedPreExpansion) : [];

currentStoredPreExpansion.unshift({
dateTime: new Date().getTime(),
investigatedEntitiesIdsList,
});

if (currentStoredPreExpansion.length > 10) currentStoredPreExpansion.pop();

sessionStorage.setItem('preExpansionStateList', JSON.stringify(currentStoredPreExpansion));
}

handleRollBackToPreExpansionState() {
const storedPreExpansion = sessionStorage.getItem('preExpansionStateList');

Expand Down
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);
}
});
});
});
});
});
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;

0 comments on commit 0daa9c9

Please sign in to comment.