@webitel/ui-datalist 26.8.27 → 26.8.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-datalist",
3
- "version": "26.8.27",
3
+ "version": "26.8.28",
4
4
  "description": "Toolkit for building data lists in webitel ui system",
5
5
  "scripts": {
6
6
  "build:types": "vue-tsc -b tsconfig.build.json",
@@ -0,0 +1,149 @@
1
+ import { mount } from '@vue/test-utils';
2
+ import {
3
+ getCalendarDayRangeErrors,
4
+ getResourceGroupTimeRangeErrors,
5
+ } from '@webitel/api-services/validations';
6
+ import { describe, expect, it } from 'vitest';
7
+ import { defineComponent, ref } from 'vue';
8
+
9
+ import { useTimeRangesValidation } from '../useTimeRangesValidation';
10
+
11
+ type CalendarDay = {
12
+ day: number;
13
+ disabled: boolean;
14
+ start: number;
15
+ end: number;
16
+ };
17
+
18
+ const row = (day: number, start: number, end: number): CalendarDay => ({
19
+ day,
20
+ disabled: false,
21
+ start,
22
+ end,
23
+ });
24
+
25
+ const setup = (rows: CalendarDay[]) => {
26
+ const dataList = ref(rows);
27
+
28
+ const Comp = defineComponent({
29
+ setup: () => useTimeRangesValidation(dataList, getCalendarDayRangeErrors),
30
+ template: '<div />',
31
+ });
32
+
33
+ return {
34
+ dataList,
35
+ ...(mount(Comp).vm as unknown as ReturnType<
36
+ typeof useTimeRangesValidation
37
+ >),
38
+ };
39
+ };
40
+
41
+ describe('useTimeRangesValidation with calendar days', () => {
42
+ it('marks nothing for rows that do not overlap', () => {
43
+ const { getRangeValidation } = setup([
44
+ row(0, 540, 541),
45
+ row(0, 600, 660),
46
+ ]);
47
+
48
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
49
+ expect(getRangeValidation(1, 'end')).toBeUndefined();
50
+ });
51
+
52
+ it('marks both ends of a row that starts after it ends', () => {
53
+ const { getRangeValidation } = setup([
54
+ row(0, 600, 540),
55
+ ]);
56
+
57
+ expect(getRangeValidation(0, 'start')).toEqual({
58
+ $error: true,
59
+ $errors: [
60
+ 'Time From cannot be greater than To',
61
+ ],
62
+ });
63
+ expect(getRangeValidation(0, 'end')?.$error).toBe(true);
64
+ });
65
+
66
+ it('marks every row of an overlap, and clears once it is resolved', () => {
67
+ const { dataList, getRangeValidation } = setup([
68
+ row(0, 540, 720),
69
+ row(0, 600, 780),
70
+ ]);
71
+
72
+ expect(getRangeValidation(0, 'start')?.$errors).toEqual([
73
+ 'Time intervals on the same day cannot overlap',
74
+ ]);
75
+ expect(getRangeValidation(1, 'start')?.$error).toBe(true);
76
+
77
+ dataList.value[1].start = 900;
78
+ dataList.value[1].end = 960;
79
+
80
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
81
+ expect(getRangeValidation(1, 'start')).toBeUndefined();
82
+ });
83
+
84
+ it('marks a time outside the day', () => {
85
+ const { getRangeValidation } = setup([
86
+ row(0, 0, 24 * 60),
87
+ ]);
88
+
89
+ expect(getRangeValidation(0, 'end')?.$errors).toEqual([
90
+ 'Hours must be from 00 to 23',
91
+ ]);
92
+ });
93
+ });
94
+
95
+ describe('useTimeRangesValidation with resource group ranges', () => {
96
+ const setupRanges = (
97
+ rows: {
98
+ start: number;
99
+ end: number;
100
+ }[],
101
+ ) => {
102
+ const ranges = ref(rows);
103
+
104
+ const Comp = defineComponent({
105
+ setup: () =>
106
+ useTimeRangesValidation(ranges, getResourceGroupTimeRangeErrors),
107
+ template: '<div />',
108
+ });
109
+
110
+ return {
111
+ ranges,
112
+ ...(mount(Comp).vm as unknown as ReturnType<
113
+ typeof useTimeRangesValidation
114
+ >),
115
+ };
116
+ };
117
+
118
+ it('marks both ends of a reversed range', () => {
119
+ const { getRangeValidation } = setupRanges([
120
+ {
121
+ start: 600,
122
+ end: 540,
123
+ },
124
+ ]);
125
+
126
+ expect(getRangeValidation(0, 'start')?.$error).toBe(true);
127
+ expect(getRangeValidation(0, 'end')?.$error).toBe(true);
128
+ });
129
+
130
+ it('marks intersecting ranges regardless of day', () => {
131
+ const { ranges, getRangeValidation } = setupRanges([
132
+ {
133
+ start: 540,
134
+ end: 720,
135
+ },
136
+ {
137
+ start: 600,
138
+ end: 780,
139
+ },
140
+ ]);
141
+
142
+ expect(getRangeValidation(1, 'end')?.$error).toBe(true);
143
+
144
+ ranges.value[1].start = 900;
145
+ ranges.value[1].end = 960;
146
+
147
+ expect(getRangeValidation(0, 'start')).toBeUndefined();
148
+ });
149
+ });
@@ -0,0 +1,38 @@
1
+ import type { TimeRangeError } from '@webitel/api-services/validations';
2
+ import { computed, type MaybeRefOrGetter, toValue } from 'vue';
3
+ import { useI18n } from 'vue-i18n';
4
+
5
+ export function useTimeRangesValidation(
6
+ items: MaybeRefOrGetter<unknown>,
7
+ getErrors: (items: unknown) => TimeRangeError[],
8
+ ) {
9
+ const { t } = useI18n();
10
+
11
+ const errors = computed(() => {
12
+ const byField = new Map<string, string>();
13
+
14
+ for (const { index, prop, key } of getErrors(toValue(items))) {
15
+ const field = `${index}.${prop}`;
16
+ if (!byField.has(field)) byField.set(field, key);
17
+ }
18
+
19
+ return byField;
20
+ });
21
+
22
+ const getRangeValidation = (index: number, prop: string) => {
23
+ const key = errors.value.get(`${index}.${prop}`);
24
+
25
+ if (!key) return undefined;
26
+
27
+ return {
28
+ $error: true,
29
+ $errors: [
30
+ t(`validation.${key}`),
31
+ ],
32
+ };
33
+ };
34
+
35
+ return {
36
+ getRangeValidation,
37
+ };
38
+ }
@@ -4,5 +4,6 @@ export * from './composables/useCardListNavigation';
4
4
  export * from './composables/useCardStoreProvider';
5
5
  export * from './composables/useCardTabs';
6
6
  export * from './composables/useNestedCardComponent';
7
+ export * from './composables/useTimeRangesValidation';
7
8
  export * from './stores/createCardStore';
8
9
  export * from './types/CardValidationFields.types';