Skip to content

Commit

Permalink
Nuevos test SessionContext
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287747 committed Mar 28, 2024
1 parent 9d55b47 commit c8b2bad
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions webapp/src/test/SessionContext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { SessionProvider, SessionContext } from '../SessionContext';

describe('SessionProvider Component', () => {
test('sets initial session data from localStorage if available', () => {
const storedData = { username: 'testUser' };
localStorage.setItem('sessionData', JSON.stringify(storedData));

const TestComponent = () => {
const { sessionData } = React.useContext(SessionContext);
return <div>{sessionData.username}</div>;
};

const { getByText } = render(
<SessionProvider>
<TestComponent />
</SessionProvider>
);

expect(getByText('testUser')).toBeInTheDocument();
});

test('saves session data to localStorage when updated', () => {
const TestComponent = () => {
const { saveSessionData } = React.useContext(SessionContext);
const handleClick = () => {
saveSessionData({ username: 'newUser' });
};
return <button onClick={handleClick}>Save Data</button>;
};

const { getByText } = render(
<SessionProvider>
<TestComponent />
</SessionProvider>
);

fireEvent.click(getByText('Save Data'));

const storedData = localStorage.getItem('sessionData');
expect(JSON.parse(storedData).username).toEqual('newUser');
});

test('clears session data from localStorage when cleared', () => {
const TestComponent = () => {
const { clearSessionData } = React.useContext(SessionContext);
const handleClick = () => {
clearSessionData();
};
return <button onClick={handleClick}>Clear Data</button>;
};

localStorage.setItem('sessionData', JSON.stringify({ username: 'testUser' }));

const { getByText } = render(
<SessionProvider>
<TestComponent />
</SessionProvider>
);

fireEvent.click(getByText('Clear Data'));

const storedData = localStorage.getItem('sessionData');
expect(storedData).toBeNull();
});
});

0 comments on commit c8b2bad

Please sign in to comment.