ktmud commented on a change in pull request #12793:
URL: https://github.com/apache/superset/pull/12793#discussion_r565842785



##########
File path: 
superset-frontend/spec/javascripts/explore/components/DateFilterControl/utils_spec.ts
##########
@@ -0,0 +1,339 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  customTimeRangeEncode,
+  customTimeRangeDecode,
+} from 'src/explore/components/controls/DateFilterControl/utils';
+
+describe('Custom TimeRange', () => {
+  describe('customTimeRangeEncode', () => {
+    it('1) default value', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'specific',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual(
+        'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 
2021-01-27T00:00:00',
+      );
+    });
+
+    it('2) specific : specific', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'specific',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'specific',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual('2021-01-20T00:00:00 : 2021-01-27T00:00:00');
+    });
+
+    it('3) specific : relative', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'specific',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'relative',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual(
+        '2021-01-20T00:00:00 : DATEADD(DATETIME("2021-01-20T00:00:00"), 7, 
day)',
+      );
+    });
+
+    it('4) now : relative', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'now',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'relative',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual('now : DATEADD(DATETIME("now"), 7, day)');
+    });
+
+    it('5) today : relative', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'today',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'relative',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual('today : DATEADD(DATETIME("today"), 7, day)');
+    });
+
+    it('6) relative : specific', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'specific',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual(
+        'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 
2021-01-27T00:00:00',
+      );
+    });
+
+    it('7) relative : now', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'now',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual('DATEADD(DATETIME("now"), -7, day) : now');
+    });
+
+    it('8) relative : today', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'today',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual('DATEADD(DATETIME("today"), -7, day) : today');
+    });
+
+    it('9) relative : relative (now)', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'relative',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'now',
+          anchorValue: 'now',
+        }),
+      ).toEqual(
+        'DATEADD(DATETIME("now"), -7, day) : DATEADD(DATETIME("now"), 7, day)',
+      );
+    });
+
+    it('10) relative : relative (date/time)', () => {
+      expect(
+        customTimeRangeEncode({
+          sinceDatetime: '2021-01-20T00:00:00',
+          sinceMode: 'relative',
+          sinceGrain: 'day',
+          sinceGrainValue: -7,
+          untilDatetime: '2021-01-27T00:00:00',
+          untilMode: 'relative',
+          untilGrain: 'day',
+          untilGrainValue: 7,
+          anchorMode: 'specific',
+          anchorValue: '2021-01-27T00:00:00',
+        }),
+      ).toEqual(
+        'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 
DATEADD(DATETIME("2021-01-27T00:00:00"), 7, day)',
+      );
+    });
+  });
+
+  describe('customTimeRangeDecode', () => {
+    it('1) default value', () => {
+      expect(
+        customTimeRangeDecode(
+          'DATEADD(DATETIME("2021-01-27T00:00:00"), -7, day) : 
2021-01-27T00:00:00',
+        ),
+      ).toEqual({
+        customRange: {
+          sinceDatetime: '2021-01-20T00:00:00',

Review comment:
       Then shouldn't `sinceDatetime` be set to `2021-01-27T00:00:00` here, 
just to be more consistent with what is set in the input values?
   
   Do you also do decode/encode when switching between non-advanced modes (i.e. 
modes that doesn't allow free text)? Not sure how feasible it is, but gut 
feeling tells me that it might be easier and cleaner to use he same config 
object internally for all frames and only do decode/encode when the time picker 
needs to communicate with the outside world.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to