|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +import dayjs from 'dayjs' |
| 19 | +import utc from 'dayjs/plugin/utc' |
| 20 | +import UsageRecords from '@/views/infra/UsageRecords' |
| 21 | + |
| 22 | +dayjs.extend(utc) |
| 23 | + |
| 24 | +const dateWithOffset = (date, offsetMinutes) => dayjs(date).utcOffset(offsetMinutes, true) |
| 25 | + |
| 26 | +const getParams = (dateRange, useBrowserTimezone) => { |
| 27 | + return UsageRecords.methods.getParams.call({ |
| 28 | + form: { dateRange }, |
| 29 | + handleRemoveFields: values => values, |
| 30 | + page: 1, |
| 31 | + pageSize: 20, |
| 32 | + $store: { |
| 33 | + getters: { usebrowsertimezone: useBrowserTimezone } |
| 34 | + } |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +describe('Views > infra > UsageRecords.vue', () => { |
| 39 | + describe('getParams()', () => { |
| 40 | + it('uses both selected dates when converting a local-timezone range to UTC', () => { |
| 41 | + const params = getParams([ |
| 42 | + dateWithOffset('2026-07-26', 60), |
| 43 | + dateWithOffset('2026-08-02', 60) |
| 44 | + ], true) |
| 45 | + |
| 46 | + expect(params.startdate).toBe('2026-07-25 23:00:00') |
| 47 | + expect(params.enddate).toBe('2026-08-02 22:59:59') |
| 48 | + }) |
| 49 | + |
| 50 | + it('uses the selected end date when the range crosses daylight-saving time', () => { |
| 51 | + const params = getParams([ |
| 52 | + dateWithOffset('2026-03-28', 0), |
| 53 | + dateWithOffset('2026-03-30', 60) |
| 54 | + ], true) |
| 55 | + |
| 56 | + expect(params.startdate).toBe('2026-03-28 00:00:00') |
| 57 | + expect(params.enddate).toBe('2026-03-30 22:59:59') |
| 58 | + }) |
| 59 | + |
| 60 | + it('preserves both selected dates when browser-timezone conversion is disabled', () => { |
| 61 | + const params = getParams(['2026-07-26', '2026-08-02'], false) |
| 62 | + |
| 63 | + expect(params.startdate).toBe('2026-07-26 00:00:00') |
| 64 | + expect(params.enddate).toBe('2026-08-02 23:59:59') |
| 65 | + }) |
| 66 | + |
| 67 | + it('omits date parameters when no range is selected', () => { |
| 68 | + const params = getParams([], true) |
| 69 | + |
| 70 | + expect(params).not.toHaveProperty('startdate') |
| 71 | + expect(params).not.toHaveProperty('enddate') |
| 72 | + }) |
| 73 | + |
| 74 | + it('omits date parameters when the selected range is incomplete', () => { |
| 75 | + const params = getParams([dateWithOffset('2026-07-26', 60)], true) |
| 76 | + |
| 77 | + expect(params).not.toHaveProperty('startdate') |
| 78 | + expect(params).not.toHaveProperty('enddate') |
| 79 | + }) |
| 80 | + }) |
| 81 | +}) |
0 commit comments