-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from fac30/feature/insight-area-charts
New Insights Charts
- Loading branch information
Showing
16 changed files
with
824 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import MoodAreaChart from "@/app/insights/components/AreaChart"; | ||
|
||
jest.mock("@/ui/shared/PlotlyChart", () => ({ | ||
__esModule: true, | ||
default: ({ data }: { data: any }) => ( | ||
<div data-testid="plotly-chart" data-prop={JSON.stringify(data)} /> | ||
), | ||
})); | ||
|
||
describe("AreaChart Component", () => { | ||
const mockData = [ | ||
{ | ||
neurotransmitters: { | ||
dopamine: 5, | ||
serotonin: 3, | ||
adrenaline: 7, | ||
}, | ||
moodName: "Happy", | ||
timestamp: "2023-01-01T12:00:00Z", | ||
id: "1", | ||
createdAt: "2023-01-01T12:00:00Z", | ||
}, | ||
{ | ||
neurotransmitters: { | ||
dopamine: 6, | ||
serotonin: 4, | ||
adrenaline: 8, | ||
}, | ||
moodName: "Sad", | ||
timestamp: "2023-01-02T12:00:00Z", | ||
id: "2", | ||
createdAt: "2023-01-02T12:00:00Z", | ||
} | ||
]; | ||
|
||
const mockStartOfRange = new Date("2023-01-01T00:00:00Z"); | ||
const mockEndOfRange = new Date("2023-12-31T23:59:59Z"); | ||
|
||
it("renders without crashing", () => { | ||
render( | ||
<MoodAreaChart | ||
dataArray={mockData} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
const plotlyChart = screen.getByTestId("plotly-chart"); | ||
expect(plotlyChart).toBeInTheDocument(); | ||
}); | ||
|
||
it("displays a message when no data is available", () => { | ||
render( | ||
<MoodAreaChart | ||
dataArray={[]} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
expect( | ||
screen.getByText("No data available for the graph.") | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("correctly calculates percentage distribution", () => { | ||
render( | ||
<MoodAreaChart | ||
dataArray={mockData} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
const plotlyChart = screen.getByTestId("plotly-chart"); | ||
const plotlyData = JSON.parse( | ||
plotlyChart.getAttribute("data-prop") || "[]" | ||
); | ||
|
||
// Check that we have two traces (one for each mood) | ||
expect(plotlyData).toHaveLength(2); | ||
|
||
// First data point should be 100% for "Happy" | ||
const happyTrace = plotlyData.find((d: any) => d.name === "Happy"); | ||
expect(happyTrace.y[0]).toBe(100); // First point should be 100% | ||
expect(happyTrace.y[1]).toBe(50); // Second point should be 50% | ||
|
||
// Second mood should start at 0% and go to 50% | ||
const sadTrace = plotlyData.find((d: any) => d.name === "Sad"); | ||
expect(sadTrace.y[0]).toBe(0); // First point should be 0% | ||
expect(sadTrace.y[1]).toBe(50); // Second point should be 50% | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
import MoodStreamGraph from "@/app/insights/components/StreamGraph"; | ||
|
||
jest.mock("@/ui/shared/PlotlyChart", () => ({ | ||
__esModule: true, | ||
default: ({ data }: { data: any }) => ( | ||
<div data-testid="plotly-chart" data-prop={JSON.stringify(data)} /> | ||
), | ||
})); | ||
|
||
describe("StreamGraph Component", () => { | ||
const mockData = [ | ||
{ | ||
neurotransmitters: { | ||
dopamine: 5, | ||
serotonin: 3, | ||
adrenaline: 7, | ||
}, | ||
moodName: "Happy", | ||
timestamp: "2023-01-01T12:00:00Z", | ||
id: "1", | ||
createdAt: "2023-01-01T12:00:00Z", | ||
}, | ||
{ | ||
neurotransmitters: { | ||
dopamine: 6, | ||
serotonin: 4, | ||
adrenaline: 8, | ||
}, | ||
moodName: "Sad", | ||
timestamp: "2023-01-02T12:00:00Z", | ||
id: "2", | ||
createdAt: "2023-01-02T12:00:00Z", | ||
} | ||
]; | ||
|
||
const mockStartOfRange = new Date("2023-01-01T00:00:00Z"); | ||
const mockEndOfRange = new Date("2023-12-31T23:59:59Z"); | ||
|
||
it("renders without crashing", () => { | ||
render( | ||
<MoodStreamGraph | ||
dataArray={mockData} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
const plotlyChart = screen.getByTestId("plotly-chart"); | ||
expect(plotlyChart).toBeInTheDocument(); | ||
}); | ||
|
||
it("displays a message when no data is available", () => { | ||
render( | ||
<MoodStreamGraph | ||
dataArray={[]} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
expect( | ||
screen.getByText("No data available for the graph.") | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("displays a message when no data is in selected range", () => { | ||
const outOfRangeStart = new Date("2024-01-01T00:00:00Z"); | ||
const outOfRangeEnd = new Date("2024-12-31T23:59:59Z"); | ||
|
||
render( | ||
<MoodStreamGraph | ||
dataArray={mockData} | ||
startOfRange={outOfRangeStart} | ||
endOfRange={outOfRangeEnd} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
expect( | ||
screen.getByText("No data available in the selected range.") | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("correctly sets up stream graph data", () => { | ||
render( | ||
<MoodStreamGraph | ||
dataArray={mockData} | ||
startOfRange={mockStartOfRange} | ||
endOfRange={mockEndOfRange} | ||
selectedButton="day" | ||
/> | ||
); | ||
|
||
const plotlyChart = screen.getByTestId("plotly-chart"); | ||
const plotlyData = JSON.parse( | ||
plotlyChart.getAttribute("data-prop") || "[]" | ||
); | ||
|
||
// Check that we have two traces (one for each mood) | ||
expect(plotlyData).toHaveLength(2); | ||
|
||
// First mood should start with height 0.5 | ||
const firstMoodTrace = plotlyData[0]; | ||
expect(firstMoodTrace.y[0]).toBe(0.5); | ||
|
||
// Second mood should start with height -0.5 to balance the graph | ||
const secondMoodTrace = plotlyData[1]; | ||
expect(secondMoodTrace.y[0]).toBe(-0.5); | ||
|
||
// Values should sum to 0 at any point | ||
expect(firstMoodTrace.y[0] + secondMoodTrace.y[0]).toBe(0); | ||
}); | ||
}); |
Oops, something went wrong.