mirror of https://github.com/grafana/grafana
Explore: Fix timezone not changing for graph and table (#42430)
* Pass timezone to graph in Explore * Fix timezone issues for table * Fix type error * Update public/app/features/explore/TableContainer.test.tsx Co-authored-by: Giordano Ricci <me@giordanoricci.com> * Replace UTC with InternalTimeZones * Update CEST to cest Co-authored-by: Giordano Ricci <me@giordanoricci.com>pull/42636/head
parent
feea6e456c
commit
d28e7b6185
@ -1,41 +1,102 @@ |
|||||||
import React from 'react'; |
import React from 'react'; |
||||||
import { render, shallow } from 'enzyme'; |
import { screen, render, within } from '@testing-library/react'; |
||||||
import { TableContainer } from './TableContainer'; |
import { TableContainer } from './TableContainer'; |
||||||
import { DataFrame } from '@grafana/data'; |
import { DataFrame, toDataFrame, FieldType, InternalTimeZones } from '@grafana/data'; |
||||||
import { ExploreId } from 'app/types/explore'; |
import { ExploreId } from 'app/types/explore'; |
||||||
|
|
||||||
|
function getTable(): HTMLElement { |
||||||
|
return screen.getAllByRole('table')[0]; |
||||||
|
} |
||||||
|
|
||||||
|
function getRowsData(rows: HTMLElement[]): Object[] { |
||||||
|
let content = []; |
||||||
|
for (let i = 1; i < rows.length; i++) { |
||||||
|
content.push({ |
||||||
|
time: within(rows[i]).getByText(/2021*/).textContent, |
||||||
|
text: within(rows[i]).getByText(/test_string_*/).textContent, |
||||||
|
}); |
||||||
|
} |
||||||
|
return content; |
||||||
|
} |
||||||
|
|
||||||
|
const dataFrame = toDataFrame({ |
||||||
|
name: 'A', |
||||||
|
fields: [ |
||||||
|
{ |
||||||
|
name: 'time', |
||||||
|
type: FieldType.time, |
||||||
|
values: [1609459200000, 1609470000000, 1609462800000, 1609466400000], |
||||||
|
config: { |
||||||
|
custom: { |
||||||
|
filterable: false, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
{ |
||||||
|
name: 'text', |
||||||
|
type: FieldType.string, |
||||||
|
values: ['test_string_1', 'test_string_2', 'test_string_3', 'test_string_4'], |
||||||
|
config: { |
||||||
|
custom: { |
||||||
|
filterable: false, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
], |
||||||
|
}); |
||||||
|
|
||||||
|
const defaultProps = { |
||||||
|
exploreId: ExploreId.left as ExploreId, |
||||||
|
loading: false, |
||||||
|
width: 800, |
||||||
|
onCellFilterAdded: jest.fn(), |
||||||
|
tableResult: dataFrame, |
||||||
|
splitOpen: (() => {}) as any, |
||||||
|
range: {} as any, |
||||||
|
timeZone: InternalTimeZones.utc, |
||||||
|
}; |
||||||
|
|
||||||
describe('TableContainer', () => { |
describe('TableContainer', () => { |
||||||
it('should render component', () => { |
it('should render component', () => { |
||||||
const props = { |
render(<TableContainer {...defaultProps} />); |
||||||
exploreId: ExploreId.left as ExploreId, |
expect(getTable()).toBeInTheDocument(); |
||||||
loading: false, |
const rows = within(getTable()).getAllByRole('row'); |
||||||
width: 800, |
expect(rows).toHaveLength(5); |
||||||
onCellFilterAdded: jest.fn(), |
expect(getRowsData(rows)).toEqual([ |
||||||
tableResult: {} as DataFrame, |
{ time: '2021-01-01 00:00:00', text: 'test_string_1' }, |
||||||
splitOpen: (() => {}) as any, |
{ time: '2021-01-01 03:00:00', text: 'test_string_2' }, |
||||||
range: {} as any, |
{ time: '2021-01-01 01:00:00', text: 'test_string_3' }, |
||||||
}; |
{ time: '2021-01-01 02:00:00', text: 'test_string_4' }, |
||||||
|
]); |
||||||
const wrapper = shallow(<TableContainer {...props} />); |
|
||||||
expect(wrapper).toMatchSnapshot(); |
|
||||||
}); |
}); |
||||||
|
|
||||||
it('should render 0 series returned on no items', () => { |
it('should render 0 series returned on no items', () => { |
||||||
const props = { |
const emptyFrames = { |
||||||
exploreId: ExploreId.left as ExploreId, |
name: 'TableResultName', |
||||||
loading: false, |
fields: [], |
||||||
width: 800, |
length: 0, |
||||||
onCellFilterAdded: jest.fn(), |
} as DataFrame; |
||||||
tableResult: { |
render(<TableContainer {...defaultProps} tableResult={emptyFrames} />); |
||||||
name: 'TableResultName', |
expect(screen.getByText('0 series returned')).toBeInTheDocument(); |
||||||
fields: [], |
}); |
||||||
length: 0, |
|
||||||
} as DataFrame, |
it('should update time when timezone changes', () => { |
||||||
splitOpen: (() => {}) as any, |
const { rerender } = render(<TableContainer {...defaultProps} />); |
||||||
range: {} as any, |
const rowsBeforeChange = within(getTable()).getAllByRole('row'); |
||||||
}; |
expect(getRowsData(rowsBeforeChange)).toEqual([ |
||||||
|
{ time: '2021-01-01 00:00:00', text: 'test_string_1' }, |
||||||
const wrapper = render(<TableContainer {...props} />); |
{ time: '2021-01-01 03:00:00', text: 'test_string_2' }, |
||||||
expect(wrapper.find('0 series returned')).toBeTruthy(); |
{ time: '2021-01-01 01:00:00', text: 'test_string_3' }, |
||||||
|
{ time: '2021-01-01 02:00:00', text: 'test_string_4' }, |
||||||
|
]); |
||||||
|
|
||||||
|
rerender(<TableContainer {...defaultProps} timeZone="cest" />); |
||||||
|
const rowsAfterChange = within(getTable()).getAllByRole('row'); |
||||||
|
expect(getRowsData(rowsAfterChange)).toEqual([ |
||||||
|
{ time: '2020-12-31 19:00:00', text: 'test_string_1' }, |
||||||
|
{ time: '2020-12-31 22:00:00', text: 'test_string_2' }, |
||||||
|
{ time: '2020-12-31 20:00:00', text: 'test_string_3' }, |
||||||
|
{ time: '2020-12-31 21:00:00', text: 'test_string_4' }, |
||||||
|
]); |
||||||
}); |
}); |
||||||
}); |
}); |
||||||
|
@ -1,19 +0,0 @@ |
|||||||
// Jest Snapshot v1, https://goo.gl/fbAQLP |
|
||||||
|
|
||||||
exports[`TableContainer should render component 1`] = ` |
|
||||||
<Collapse |
|
||||||
isOpen={true} |
|
||||||
label="Table" |
|
||||||
loading={false} |
|
||||||
> |
|
||||||
<Memo(MetaInfoText) |
|
||||||
metaItems={ |
|
||||||
Array [ |
|
||||||
Object { |
|
||||||
"value": "0 series returned", |
|
||||||
}, |
|
||||||
] |
|
||||||
} |
|
||||||
/> |
|
||||||
</Collapse> |
|
||||||
`; |
|
Loading…
Reference in new issue