|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; |
|
|
import { renderHook } from '@testing-library/react'; |
|
|
import React from 'react'; |
|
|
import { Provider } from 'react-redux'; |
|
|
import configureStore from 'redux-mock-store'; |
|
|
import { useSiteMetricsData } from '../components/site-monitoring'; |
|
|
import { calculateTimeRange } from '../components/time-range-picker'; |
|
|
import { useSiteMetricsQuery } from '../hooks/use-metrics-query'; |
|
|
|
|
|
jest.mock( 'calypso/sites/monitoring/hooks/use-metrics-query.ts', () => ( { |
|
|
__esModule: true, |
|
|
useSiteMetricsQuery: jest.fn( () => { |
|
|
return { |
|
|
data: { |
|
|
data: { periods: [] }, |
|
|
}, |
|
|
}; |
|
|
} ), |
|
|
} ) ); |
|
|
|
|
|
const INITIAL_STATE = { |
|
|
sites: { |
|
|
items: {}, |
|
|
}, |
|
|
ui: { |
|
|
selectedSiteId: '1', |
|
|
}, |
|
|
}; |
|
|
|
|
|
const mockStore = configureStore(); |
|
|
const store = mockStore( INITIAL_STATE ); |
|
|
const queryClient = new QueryClient(); |
|
|
const timeRange = calculateTimeRange( '24-hours' ); |
|
|
|
|
|
describe( 'useSiteMetrics test', () => { |
|
|
beforeAll( () => { |
|
|
|
|
|
Object.defineProperty( window, 'matchMedia', { |
|
|
writable: true, |
|
|
value: jest.fn().mockImplementation( ( query ) => ( { |
|
|
matches: false, |
|
|
media: query, |
|
|
onchange: null, |
|
|
addListener: jest.fn(), |
|
|
removeListener: jest.fn(), |
|
|
addEventListener: jest.fn(), |
|
|
removeEventListener: jest.fn(), |
|
|
dispatchEvent: jest.fn(), |
|
|
} ) ), |
|
|
} ); |
|
|
jest.clearAllMocks(); |
|
|
} ); |
|
|
|
|
|
it( 'should return formattedData for the case with an empty array for dimension', () => { |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { periods: [ { timestamp: 1685577600, dimension: {} } ] }, |
|
|
}, |
|
|
} ); |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { periods: [ { timestamp: 1685577600, dimension: {} } ] }, |
|
|
}, |
|
|
} ); |
|
|
|
|
|
|
|
|
const wrapper = ( { children } ) => ( |
|
|
<QueryClientProvider client={ queryClient }> |
|
|
<Provider store={ store }>{ children }</Provider> |
|
|
</QueryClientProvider> |
|
|
); |
|
|
|
|
|
|
|
|
const { result } = renderHook( () => useSiteMetricsData( timeRange ), { wrapper } ); |
|
|
|
|
|
|
|
|
const { formattedData } = result.current; |
|
|
|
|
|
|
|
|
expect( formattedData ).toEqual( [ |
|
|
[ 1685577600 ], |
|
|
[ 0 ], |
|
|
[ 0 ], |
|
|
] ); |
|
|
} ); |
|
|
|
|
|
it( 'should return formattedData for the case with an object for dimension', () => { |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { |
|
|
periods: [ |
|
|
{ timestamp: 1685577600, dimension: { 'example.com': 0.0030000000000000005 } }, |
|
|
], |
|
|
}, |
|
|
}, |
|
|
} ); |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { |
|
|
periods: [ |
|
|
{ timestamp: 1685577600, dimension: { 'example.com': 0.0030000000000000005 } }, |
|
|
], |
|
|
}, |
|
|
}, |
|
|
} ); |
|
|
|
|
|
const wrapper = ( { children } ) => ( |
|
|
<QueryClientProvider client={ queryClient }> |
|
|
<Provider store={ store }>{ children }</Provider> |
|
|
</QueryClientProvider> |
|
|
); |
|
|
|
|
|
const { result } = renderHook( () => useSiteMetricsData( timeRange ), { wrapper } ); |
|
|
|
|
|
const { formattedData } = result.current; |
|
|
|
|
|
expect( formattedData ).toEqual( [ |
|
|
[ 1685577600 ], |
|
|
[ 0.18000000000000002 ], |
|
|
[ 3.0000000000000004 ], |
|
|
] ); |
|
|
} ); |
|
|
|
|
|
it( 'should return formattedData for the case with dimension being an array and an object', () => { |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { |
|
|
periods: [ |
|
|
{ timestamp: 1685577600, dimension: { 'example.com': 0.0030000000000000005 } }, |
|
|
{ timestamp: 1685577800, dimension: {} }, |
|
|
], |
|
|
}, |
|
|
}, |
|
|
} ); |
|
|
useSiteMetricsQuery.mockReturnValueOnce( { |
|
|
data: { |
|
|
data: { |
|
|
periods: [ |
|
|
{ timestamp: 1685577600, dimension: { 'example.com': 0.0030000000000000005 } }, |
|
|
{ timestamp: 1685577800, dimension: {} }, |
|
|
], |
|
|
}, |
|
|
}, |
|
|
} ); |
|
|
|
|
|
const wrapper = ( { children } ) => ( |
|
|
<QueryClientProvider client={ queryClient }> |
|
|
<Provider store={ store }>{ children }</Provider> |
|
|
</QueryClientProvider> |
|
|
); |
|
|
|
|
|
const { result } = renderHook( () => useSiteMetricsData( timeRange ), { wrapper } ); |
|
|
|
|
|
const { formattedData } = result.current; |
|
|
|
|
|
expect( formattedData ).toEqual( [ |
|
|
[ 1685577600, 1685577800 ], |
|
|
[ 0.18000000000000002, 0 ], |
|
|
[ 3.0000000000000004, 0 ], |
|
|
] ); |
|
|
} ); |
|
|
} ); |
|
|
|