Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Date picker #237

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 100 additions & 6 deletions provisioning/dashboards/dateTime.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"header": true,
"isUseLocalTime": false,
"minimizeDisplayMode": "dateTimePicker",
"minimizeOutputFormat": "date",
"minimizeOutputFormat": "dateTime",
"padding": 10,
"persistent": false,
"saveSelectedGroup": false,
Expand All @@ -89,7 +89,6 @@
"tabsInOrder": true,
"variable": "first"
},
"pluginVersion": "3.6.0",
"targets": [
{
"datasource": {
Expand All @@ -102,6 +101,86 @@
"title": "Variable: First - ISO string",
"type": "volkovlabs-variable-panel"
},
{
"datasource": {
"name": "Static",
"type": "marcusolsson-static-datasource",
"uid": "P1D2C73DC01F2359B"
},
"fieldConfig": {
"defaults": {
"custom": {
"thresholdsStyle": {
"mode": "color",
"thresholds": []
}
},
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 4,
"w": 5,
"x": 5,
"y": 0
},
"id": 4,
"options": {
"alwaysVisibleFilter": false,
"autoScroll": false,
"collapsedByDefault": false,
"customValue": false,
"displayMode": "minimize",
"emptyValue": false,
"favorites": {
"enabled": false,
"storage": "browser"
},
"filter": false,
"groupSelection": false,
"header": true,
"isUseLocalTime": true,
"minimizeDisplayMode": "dateTimePicker",
"minimizeOutputFormat": "date",
"padding": 10,
"persistent": false,
"saveSelectedGroup": false,
"saveSelectedGroupKey": "",
"showGroupTotal": false,
"showLabel": false,
"showName": false,
"showResetButton": false,
"showTotal": false,
"statusSort": false,
"sticky": false,
"tabsInOrder": true,
"variable": "DateOnly"
},
"targets": [
{
"datasource": {
"type": "marcusolsson-static-datasource",
"uid": "P1D2C73DC01F2359B"
},
"refId": "A"
}
],
"title": "Variable: Date Only",
"type": "volkovlabs-variable-panel"
},
{
"datasource": {
"name": "Static",
Expand Down Expand Up @@ -170,7 +249,6 @@
"tabsInOrder": true,
"variable": "second"
},
"pluginVersion": "3.6.0",
"targets": [
{
"datasource": {
Expand Down Expand Up @@ -251,7 +329,6 @@
"tabsInOrder": true,
"variable": "third"
},
"pluginVersion": "3.6.0",
"targets": [
{
"datasource": {
Expand Down Expand Up @@ -285,7 +362,7 @@
"value": "2024-05-05T14:48:00.000Z"
}
],
"query": "2024-05-18T14:48:00.000Z",
"query": "2024-05-03T14:48:00.000Z",
"type": "textbox"
},
{
Expand All @@ -302,7 +379,7 @@
"value": "1722429030000"
}
],
"query": "1721305830000",
"query": "1720873830000",
"type": "textbox"
},
{
Expand All @@ -312,6 +389,23 @@
"options": [],
"query": "",
"type": "textbox"
},
{
"current": {
"text": "2025-01-03",
"value": "2025-01-03"
},
"label": "DateOnly",
"name": "DateOnly",
"options": [
{
"selected": true,
"text": "2025-01-03",
"value": "2025-01-03"
}
],
"query": "2025-01-03",
"type": "textbox"
}
]
},
Expand Down
25 changes: 25 additions & 0 deletions src/__mocks__/@grafana/ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,29 @@ const DateTimePickerMock = ({ onChange, date, ...restProps }: any) => {

const DateTimePicker = jest.fn(DateTimePickerMock);

/**
* Mock DatePickerWithInput component
*/
const DatePickerWithInputMock = ({ onChange, value, ...restProps }: any) => {
return (
<input
data-testid={restProps['data-testid']}
value={value}
onChange={(event) => {
/**
* Check Date type handle
*/
const value = event.target.value === '2009-09-09' ? new Date(event.target.value) : event.target.value;
if (onChange) {
onChange(value);
}
}}
/>
);
};

const DatePickerWithInput = jest.fn(DatePickerWithInputMock);

/**
* Mock Select component
*/
Expand Down Expand Up @@ -83,11 +106,13 @@ beforeEach(() => {
Select.mockImplementation(SelectMock);
DateTimePicker.mockImplementation(DateTimePickerMock);
ToolbarButtonRow.mockImplementation(ToolbarButtonRowMock);
DatePickerWithInput.mockImplementation(DatePickerWithInputMock);
});

module.exports = {
...actual,
ToolbarButtonRow,
Select,
DateTimePicker,
DatePickerWithInput,
};
168 changes: 168 additions & 0 deletions src/components/DateSelector/DateSelector.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { act, fireEvent, render, screen } from '@testing-library/react';
import { getJestSelectors } from '@volkovlabs/jest-selectors';
import React from 'react';

import { TEST_IDS } from '../../constants';
import { getDateInLocalTimeFormat, selectVariableValues } from '../../utils';
import { DateSelector } from './DateSelector';

/**
* Mock utils
*/
jest.mock('../../utils', () => ({
selectVariableValues: jest.fn(),
getDateInLocalTimeFormat: jest.fn(),
}));

/**
* Persistent Storage Mock
*/
const persistentStorageMock = {
remove: jest.fn(),
};

/**
* Mock hooks
*/
jest.mock('../../hooks', () => ({
...jest.requireActual('../../hooks'),
usePersistentStorage: jest.fn(() => persistentStorageMock),
}));

/**
* Properties
*/
type Props = React.ComponentProps<typeof DateSelector>;

/**
* Date Selector
*/
describe('Date Time Selector', () => {
const defaultVariable = {
label: 'dateTimeTS',
current: {
value: '2025-01-21',
},
};

/**
* Selectors
*/
const getSelectors = getJestSelectors(TEST_IDS.datePicker);
const selectors = getSelectors(screen);

/**
* Get Tested Component
*/
const getComponent = (props: Partial<Props>) => {
return <DateSelector variable={defaultVariable} panelEventBus={{} as any} {...(props as any)} />;
};

beforeEach(() => {
jest.mocked(selectVariableValues).mockClear();
jest.mocked(getDateInLocalTimeFormat).mockClear();
});

it('Should apply initial variable value from variable', () => {
render(
getComponent({
variable: defaultVariable as any,
})
);

expect(selectors.root()).toBeVisible();
expect(selectors.root()).toHaveValue('2025-01-21');
});

it('Should apply initial variable value from variable if value is empty', () => {
const variable = {
label: 'dateTimeTS',
current: {
value: '',
},
} as any;

render(
getComponent({
variable: variable,
})
);

expect(selectors.root()).toBeVisible();
expect(selectors.root()).toHaveValue('');
});

it('Should update variable value from string', async () => {
render(
getComponent({
variable: defaultVariable as any,
persistent: true,
})
);

expect(selectors.root()).toBeVisible();
expect(selectors.root()).toHaveValue('2025-01-21');

/**
* Change Date
*/
const date = new Date('2024-07-31 12:30:30');
await act(() => fireEvent.change(selectors.root(), { target: { value: date.toISOString() } }));

expect(selectVariableValues).toHaveBeenCalledWith({
values: ['2024-07-31'],
runtimeVariable: defaultVariable,
panelEventBus: {},
});

expect(persistentStorageMock.remove).toHaveBeenCalled();
});

it('Should update variable value', async () => {
render(
getComponent({
variable: defaultVariable as any,
persistent: true,
})
);

expect(selectors.root()).toBeVisible();
expect(selectors.root()).toHaveValue('2025-01-21');

/**
* Change Date
*/
const date = '2009-09-09';
await act(() => fireEvent.change(selectors.root(), { target: { value: date } }));

expect(selectVariableValues).toHaveBeenCalledWith({
values: ['2009-09-09'],
runtimeVariable: defaultVariable,
panelEventBus: {},
});

expect(persistentStorageMock.remove).toHaveBeenCalled();
});

it('Should update variable value in local time', async () => {
render(
getComponent({
variable: defaultVariable as any,
persistent: true,
isUseLocalTime: true,
})
);

expect(selectors.root()).toBeVisible();
expect(selectors.root()).toHaveValue('2025-01-21');

/**
* Change Date
*/
const date = '2009-09-09';
await act(() => fireEvent.change(selectors.root(), { target: { value: date } }));

const checkDate = new Date(date);
expect(getDateInLocalTimeFormat).toHaveBeenCalledWith(checkDate);
});
});
Loading
Loading