Skip to content

Commit

Permalink
Fixed: finalizeSession bug in Final component (#689)
Browse files Browse the repository at this point in the history
* fix: Call finalize when Final component has mounted

* chore(deps): Add history package to package.json

* refactor: Add redirect test case to Final Component
  • Loading branch information
drikusroor authored Jan 8, 2024
1 parent 7933da9 commit 9220439
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 12 deletions.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"eslint": "^8.54.0",
"eslint-config-react-app": "^7.0.1",
"eslint-plugin-storybook": "^0.6.15",
"history": "^5.3.0",
"prop-types": "15.8.1",
"storybook": "7.6.6",
"webpack": "5.89.0"
Expand Down
23 changes: 11 additions & 12 deletions frontend/src/components/Final/Final.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import UserFeedback from "../UserFeedback/UserFeedback";

// Final is an experiment view that shows the final scores of the experiment
// It can only be the last view of an experiment
const Final= ({ experiment, participant, session, score, final_text, action_texts, button,
onNext, history, show_participant_link, participant_id_only,
show_profile_link, social, feedback_info, points, rank, logo }) => {
const Final = ({ experiment, participant, session, score, final_text, action_texts, button,
onNext, history, show_participant_link, participant_id_only,
show_profile_link, social, feedback_info, points, rank, logo }) => {
const [showScore, setShowScore] = useState(0);

// Use a ref to prevent doing multiple increments
Expand Down Expand Up @@ -46,17 +46,17 @@ const Final= ({ experiment, participant, session, score, final_text, action_text
};
}, [score, showScore]);

const finalize = () => {
useEffect(() => {
finalizeSession({ session, participant });
}
}, [session, participant]);

return (
<div className="aha__final d-flex flex-column justify-content-center">
{rank && (
<div className="text-center">
<Rank rank={rank} />
<h1 className="total-score title">{showScore} {points}</h1>
</div>
<div className="text-center">
<Rank rank={rank} />
<h1 className="total-score title" data-testid="score">{showScore} {points}</h1>
</div>
)}
<div className="text-center">
<div dangerouslySetInnerHTML={{ __html: final_text }} />
Expand Down Expand Up @@ -85,10 +85,9 @@ const Final= ({ experiment, participant, session, score, final_text, action_text
{action_texts.all_experiments}
</a>
<div
data-testid="profile-link"
className="home text-center"
onClick={() => {
history.push(URLS.profile);
}}
onClick={() => history.push(URLS.profile)}
>
{action_texts.profile}
</div>
Expand Down
116 changes: 116 additions & 0 deletions frontend/src/components/Final/Final.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { BrowserRouter, Router } from 'react-router-dom';
import { createMemoryHistory } from 'history'

import Final from './Final'; // Adjust the import path as necessary


jest.mock('../../API', () => ({
finalizeSession: jest.fn(),
}));

jest.mock('../../config', () => ({
URLS: {
AMLHome: '/aml',
profile: '/profile',
},
}));

describe('Final Component', () => {
it('renders correctly with given props', () => {
render(
<BrowserRouter>
<Final
experiment={{ slug: 'test-experiment' }}
participant="participant-id"
session="session-id"
score={100}
final_text="<p>Final Text</p>"
rank={1}
/>
</BrowserRouter>
);

expect(screen.getByText(/Final Text/i)).toBeInTheDocument();
expect(screen.getByTestId('score')).toBeInTheDocument(); // Adjust based on how you display points
});

it('calls onNext prop when button is clicked', async () => {
const onNextMock = jest.fn();
render(
<BrowserRouter>
<Final
button={{ text: 'Next', link: '' }}
onNext={onNextMock}
/>
</BrowserRouter>
);

fireEvent.click(screen.getByText('Next'));
await waitFor(() => {
expect(onNextMock).toHaveBeenCalled();
});
});

it('does not render rank and social components when props are not provided', () => {
render(
<BrowserRouter>
<Final
experiment={{ slug: 'test-experiment' }}
participant="participant-id"
session="session-id"
score={100}
final_text="<p>Final Text</p>"
/>
</BrowserRouter>
);

expect(screen.queryByText('Rank')).not.toBeInTheDocument();
expect(screen.queryByText('Social')).not.toBeInTheDocument();
});

it('navigates to profile page when profile link is clicked', async () => {

const history = createMemoryHistory();

const mockActionTexts = {
all_experiments: 'All Experiments',
profile: 'Profile',
};

render(
<Router history={history}>
<Final
show_profile_link={true}
action_texts={mockActionTexts}
/>
</Router>
);

const profileLink = screen.getByTestId('profile-link');

expect(profileLink).toBeInTheDocument();

expect(history.location.pathname).toBe('/');

fireEvent.click(profileLink)

expect(history.location.pathname).toBe('/profile');

});

it('calls finalizeSession with correct arguments', () => {
const { finalizeSession } = require('../../API');
render(
<BrowserRouter>
<Final
participant="participant-id"
session="session-id"
/>
</BrowserRouter>
);

expect(finalizeSession).toHaveBeenCalledWith({ session: 'session-id', participant: 'participant-id' });
});
});
19 changes: 19 additions & 0 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2862,6 +2862,15 @@ __metadata:
languageName: node
linkType: hard

"@babel/runtime@npm:^7.7.6":
version: 7.23.7
resolution: "@babel/runtime@npm:7.23.7"
dependencies:
regenerator-runtime: "npm:^0.14.0"
checksum: 3e304133ee55b0750e03e53cb4efb47fb2bdcdb5795f85bbffa10595196c34b9be60eb65bd6d833c87f49fc827f0365f86f95f51d85b188004d3128bb5129c93
languageName: node
linkType: hard

"@babel/template@npm:^7.22.15":
version: 7.22.15
resolution: "@babel/template@npm:7.22.15"
Expand Down Expand Up @@ -7374,6 +7383,7 @@ __metadata:
eslint-config-react-app: "npm:^7.0.1"
eslint-plugin-storybook: "npm:^0.6.15"
file-saver: "npm:^2.0.5"
history: "npm:^5.3.0"
next-share: "npm:0.25.0"
prop-types: "npm:15.8.1"
qs: "npm:^6.10.3"
Expand Down Expand Up @@ -11643,6 +11653,15 @@ __metadata:
languageName: node
linkType: hard

"history@npm:^5.3.0":
version: 5.3.0
resolution: "history@npm:5.3.0"
dependencies:
"@babel/runtime": "npm:^7.7.6"
checksum: 812ec839386222d6437bd78d9f05db32e47d105ada0ad8834b32626919dd2fee7a10001bc489510f93a8069d02f118214bd8d42a82f7cf9daf8e84fbcbbb2016
languageName: node
linkType: hard

"hoist-non-react-statics@npm:^3.1.0, hoist-non-react-statics@npm:^3.3.1, hoist-non-react-statics@npm:^3.3.2":
version: 3.3.2
resolution: "hoist-non-react-statics@npm:3.3.2"
Expand Down

0 comments on commit 9220439

Please sign in to comment.