|
1 | | -import { describe, it, expect } from "vitest"; |
| 1 | +import { describe, expect, it } from "vitest"; |
2 | 2 |
|
3 | | -describe("Backend", () => { |
4 | | - it("should pass placeholder test", () => { |
5 | | - expect(true).toBe(true); |
| 3 | +import { calculateAverage, validateFeedback } from "./roti-service.js"; |
| 4 | + |
| 5 | +describe("validateFeedback", () => { |
| 6 | + it("rejects ratings below 1", () => { |
| 7 | + expect(() => validateFeedback({ rating: 0, comment: "ok" })).toThrow( |
| 8 | + "Rating must be an integer between 1 and 5" |
| 9 | + ); |
| 10 | + }); |
| 11 | + |
| 12 | + it("rejects ratings above 5", () => { |
| 13 | + expect(() => validateFeedback({ rating: 6, comment: "ok" })).toThrow( |
| 14 | + "Rating must be an integer between 1 and 5" |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + it("rejects non-integer ratings", () => { |
| 19 | + expect(() => validateFeedback({ rating: 2.5, comment: "ok" })).toThrow( |
| 20 | + "Rating must be an integer between 1 and 5" |
| 21 | + ); |
| 22 | + }); |
| 23 | + |
| 24 | + it("requires a comment for ratings at or below 3", () => { |
| 25 | + expect(() => validateFeedback({ rating: 3, comment: " " })).toThrow( |
| 26 | + "Comment is required when rating is 3 or below" |
| 27 | + ); |
| 28 | + }); |
| 29 | + |
| 30 | + it("allows empty comment for ratings above 3", () => { |
| 31 | + expect(validateFeedback({ rating: 4, comment: " " })).toEqual({ |
| 32 | + rating: 4, |
| 33 | + comment: "", |
| 34 | + }); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("calculateAverage", () => { |
| 39 | + it("returns zero for empty input", () => { |
| 40 | + expect(calculateAverage([])).toBe(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it("calculates average to one decimal", () => { |
| 44 | + expect(calculateAverage([5, 4, 4])).toBe(4.3); |
6 | 45 | }); |
7 | 46 | }); |
0 commit comments