Skip to content

Commit a0be0c4

Browse files
committed
kibana-app-arch suggestions
- Move decision of using defaults to the solution consumer - Add unit tests
1 parent 6c4f417 commit a0be0c4

4 files changed

Lines changed: 19 additions & 3 deletions

File tree

src/plugins/data/public/query/timefilter/timefilter.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ function clearNowTimeStub() {
5454
delete global.nowTime;
5555
}
5656

57+
test('isTimeTouched is initially set to false', () => {
58+
expect(timefilter.isTimeTouched()).toBe(false);
59+
});
60+
5761
describe('setTime', () => {
5862
let update: sinon.SinonSpy;
5963
let fetch: sinon.SinonSpy;
@@ -84,6 +88,10 @@ describe('setTime', () => {
8488
});
8589
});
8690

91+
test('should update isTimeTouched', () => {
92+
expect(timefilter.isTimeTouched()).toBe(true);
93+
});
94+
8795
test('should not add unexpected object keys to time state', () => {
8896
const unexpectedKey = 'unexpectedKey';
8997
timefilter.setTime({

src/plugins/data/public/query/timefilter/timefilter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export class Timefilter {
7070
return this._isAutoRefreshSelectorEnabled;
7171
}
7272

73+
public isTimeTouched() {
74+
return this._isTimeTouched;
75+
}
76+
7377
public getEnabledUpdated$ = () => {
7478
return this.enabledUpdated$.asObservable();
7579
};
@@ -90,8 +94,8 @@ export class Timefilter {
9094
return this.fetch$.asObservable();
9195
};
9296

93-
public getTime = (defaults?: TimeRange): TimeRange => {
94-
const { from, to } = this._isTimeTouched || !defaults ? this._time : defaults;
97+
public getTime = (): TimeRange => {
98+
const { from, to } = this._time;
9599
return {
96100
...this._time,
97101
from: moment.isMoment(from) ? from.toISOString() : from,

src/plugins/data/public/query/timefilter/timefilter_service.mock.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const createSetupContractMock = () => {
2626
const timefilterMock: jest.Mocked<TimefilterContract> = {
2727
isAutoRefreshSelectorEnabled: jest.fn(),
2828
isTimeRangeSelectorEnabled: jest.fn(),
29+
isTimeTouched: jest.fn(),
2930
getEnabledUpdated$: jest.fn(),
3031
getTimeUpdate$: jest.fn(),
3132
getRefreshIntervalUpdate$: jest.fn(),

x-pack/plugins/infra/public/hooks/use_kibana_timefilter_time.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ export const useKibanaTimefilterTime = ({
1616
const { services } = useKibanaContextForPlugin();
1717

1818
const getTime = useCallback(() => {
19-
return services.data.query.timefilter.timefilter.getTime({ from: fromDefault, to: toDefault });
19+
const timefilterService = services.data.query.timefilter.timefilter;
20+
return timefilterService.isTimeTouched()
21+
? timefilterService.getTime()
22+
: { from: fromDefault, to: toDefault };
2023
}, [services.data.query.timefilter.timefilter, fromDefault, toDefault]);
2124

2225
return [getTime, services.data.query.timefilter.timefilter.setTime];

0 commit comments

Comments
 (0)