Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion frontend/src/Pages/ProsConsChallenge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ interface Evaluation {

const baseURL = config.baseUrl || 'http://localhost:1313';

// Each argument is scored out of 10 and the final score is their sum
// (see EvaluateProsCons in backend/services/pros_cons.go).
const MAX_SCORE_PER_ARGUMENT = 10;

const ProsConsChallenge: React.FC = () => {
const [token, setToken] = useState<string | null>(null);
const [topic, setTopic] = useState<string | null>(null);
const [pros, setPros] = useState<string[]>(["", "", "", "", ""]);
const [cons, setCons] = useState<string[]>(["", "", "", "", ""]);
const [evaluation, setEvaluation] = useState<Evaluation | null>(null);
// Number of arguments actually submitted; the score maximum is based on this,
// not on how many entries the evaluation response happens to contain.
const [submittedCount, setSubmittedCount] = useState<number>(0);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
const [currentStep, setCurrentStep] = useState<number>(1);
Expand Down Expand Up @@ -96,6 +103,7 @@ const ProsConsChallenge: React.FC = () => {
const data = await response.json();
if (!response.ok) throw new Error(data.error || "Failed to evaluate arguments");
setEvaluation(data);
setSubmittedCount(validPros.length + validCons.length);
setCurrentStep(3);
} catch (err: any) {
setError(err.message);
Expand All @@ -109,6 +117,7 @@ const ProsConsChallenge: React.FC = () => {
setPros(["", "", "", "", ""]);
setCons(["", "", "", "", ""]);
setEvaluation(null);
setSubmittedCount(0);
setCurrentStep(1);
setError(null);
};
Expand Down Expand Up @@ -213,7 +222,9 @@ const ProsConsChallenge: React.FC = () => {
</CardHeader>
<CardContent>
<div className="text-center mb-6">
<h2 className="text-3xl font-bold text-primary">Final Score: {evaluation.score}/100</h2>
<h2 className="text-3xl font-bold text-primary">
Final Score: {evaluation.score}/{submittedCount * MAX_SCORE_PER_ARGUMENT}
</h2>
<p className="text-muted-foreground">Based on {evaluation.pros.length} pros and {evaluation.cons.length} cons submitted</p>
</div>
<div className="grid grid-cols-2 gap-6">
Expand Down
72 changes: 72 additions & 0 deletions frontend/src/Pages/__tests__/ProsConsChallenge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ProsConsChallenge from '../ProsConsChallenge';
import { mockFetch } from '../../test/mockFetch';

const argument = (score: number) => ({ score, feedback: 'Good point.', counter: 'A counterpoint.' });

/**
* Fills `submitted.pros` pros and `submitted.cons` cons, then submits.
* `returned` is what the evaluation endpoint answers with (defaults to one
* entry per submitted argument).
*/
async function submit(
submitted: { pros: number; cons: number },
returned: { pros: number[]; cons: number[] },
) {
mockFetch([
{ match: '/coach/pros-cons/topic', body: { topic: 'Should homework be banned?' } },
{
match: '/coach/pros-cons/submit',
method: 'POST',
body: {
pros: returned.pros.map(argument),
cons: returned.cons.map(argument),
score: [...returned.pros, ...returned.cons].reduce((sum, s) => sum + s, 0),
},
},
]);

const user = userEvent.setup();
render(<ProsConsChallenge />);
await user.click(screen.getByRole('button', { name: 'Get Topic' }));
for (let i = 0; i < submitted.pros; i++) {
await user.type(screen.getByPlaceholderText(`Pro ${i + 1}`), `pro ${i + 1}`);
}
for (let i = 0; i < submitted.cons; i++) {
await user.type(screen.getByPlaceholderText(`Con ${i + 1}`), `con ${i + 1}`);
}
await user.click(screen.getByRole('button', { name: 'Submit Arguments' }));
return user;
}

describe('ProsConsChallenge final score', () => {
beforeEach(() => {
localStorage.setItem('token', 'test-token');
});

it('shows the score out of 10 per submitted argument', async () => {
await submit({ pros: 1, cons: 1 }, { pros: [10], cons: [9] });
expect(await screen.findByRole('heading', { name: 'Final Score: 19/20' })).toBeInTheDocument();
expect(screen.queryByText(/\/100/)).not.toBeInTheDocument();
});

it('shows /100 when all 5 pros and 5 cons are submitted', async () => {
await submit({ pros: 5, cons: 5 }, { pros: [8, 7, 9, 6, 8], cons: [7, 8, 9, 7, 6] });
expect(await screen.findByRole('heading', { name: 'Final Score: 75/100' })).toBeInTheDocument();
});

it('bases the maximum on what was submitted, not on how many entries the response has', async () => {
await submit({ pros: 5, cons: 5 }, { pros: [10], cons: [9] });
expect(await screen.findByRole('heading', { name: 'Final Score: 19/100' })).toBeInTheDocument();
});

it('starts a new challenge without keeping the previous maximum', async () => {
const user = await submit({ pros: 5, cons: 5 }, { pros: [8, 7, 9, 6, 8], cons: [7, 8, 9, 7, 6] });
await screen.findByRole('heading', { name: 'Final Score: 75/100' });
await user.click(screen.getByRole('button', { name: 'Try Another Challenge' }));
expect(screen.getByRole('button', { name: 'Get Topic' })).toBeInTheDocument();
expect(screen.queryByRole('heading', { name: /Final Score/ })).not.toBeInTheDocument();
});
});
Loading