11import { QueryClientProvider } from "@tanstack/react-query" ;
2- import { render , screen } from '@testing-library/react' ;
2+ import { render , screen , fireEvent } from '@testing-library/react' ;
33import { WeightEntry } from "components/BodyWeight/model" ;
44import { getWeights } from "services" ;
55import { testQueryClient } from "tests/queryClient" ;
66import { BodyWeight } from "./index" ;
7+ import axios from "axios" ;
8+ import { FilterType } from "./widgets/FilterButtons" ;
79
810const { ResizeObserver } = window ;
911
@@ -29,13 +31,13 @@ describe("Test BodyWeight component", () => {
2931 jest . restoreAllMocks ( ) ;
3032 } ) ;
3133
32- test ( 'renders without crashing' , async ( ) => {
34+ // Arrange
35+ const weightData = [
36+ new WeightEntry ( new Date ( '2021-12-10' ) , 80 , 1 ) ,
37+ new WeightEntry ( new Date ( '2021-12-20' ) , 90 , 2 ) ,
38+ ] ;
3339
34- // Arrange
35- const weightData = [
36- new WeightEntry ( new Date ( '2021-12-10' ) , 80 , 1 ) ,
37- new WeightEntry ( new Date ( '2021-12-20' ) , 90 , 2 ) ,
38- ] ;
40+ test ( 'renders without crashing' , async ( ) => {
3941
4042 // @ts -ignore
4143 getWeights . mockImplementation ( ( ) => Promise . resolve ( weightData ) ) ;
@@ -57,4 +59,42 @@ describe("Test BodyWeight component", () => {
5759 const textElement2 = await screen . findByText ( "90" ) ;
5860 expect ( textElement2 ) . toBeInTheDocument ( ) ;
5961 } ) ;
62+
63+
64+ test ( 'changes filter and updates displayed data' , async ( ) => {
65+
66+ // Mock the getWeights response based on the filter
67+ // @ts -ignore
68+ getWeights . mockImplementation ( ( filter : FilterType ) => {
69+ if ( filter === 'lastYear' ) {
70+ return Promise . resolve ( weightData ) ;
71+ } else if ( filter === 'lastMonth' ) {
72+ return Promise . resolve ( [ ] ) ;
73+ }
74+ return Promise . resolve ( [ ] ) ;
75+ } ) ;
76+
77+ render (
78+ < QueryClientProvider client = { testQueryClient } >
79+ < BodyWeight />
80+ </ QueryClientProvider >
81+ ) ;
82+
83+ // Initially should display data for last year
84+ expect ( await screen . findByText ( "80" ) ) . toBeInTheDocument ( ) ;
85+ expect ( await screen . findByText ( "90" ) ) . toBeInTheDocument ( ) ;
86+
87+ // Change filter to 'lastMonth'
88+ const filterButton = screen . getByRole ( 'button' , { name : / l a s t M o n t h / i } ) ;
89+ fireEvent . click ( filterButton ) ;
90+
91+ // Expect getWeights to be called with 'lastMonth'
92+ expect ( getWeights ) . toHaveBeenCalledWith ( 'lastMonth' ) ;
93+
94+ // Check that entries for last year are no longer in the document
95+ expect ( screen . queryByText ( "80" ) ) . not . toBeInTheDocument ( ) ;
96+ expect ( screen . queryByText ( "90" ) ) . not . toBeInTheDocument ( ) ;
97+ } ) ;
98+
99+
60100} ) ;
0 commit comments