-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcslpdata.test.ts
More file actions
219 lines (191 loc) · 7.86 KB
/
cslpdata.test.ts
File metadata and controls
219 lines (191 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { extractDetailsFromCslp, isValidCslp } from "../cslpdata";
describe("isValidCslp", () => {
describe("valid cases", () => {
test("should return true for valid v1 format with 3 parts", () => {
expect(
isValidCslp("content_type_uid.entry_uid.locale")
).toBeTruthy();
});
test("should return true for valid v1 format with field path", () => {
expect(
isValidCslp("content_type_uid.entry_uid.locale.field_path")
).toBeTruthy();
});
test("should return true for valid v2 format with 3 parts", () => {
expect(
isValidCslp("v2:content_type_uid.entry_uid_variant_uid.locale")
).toBeTruthy();
});
test("should return true for valid v2 format with field path", () => {
expect(
isValidCslp(
"v2:content_type_uid.entry_uid_variant_uid.locale.field_path"
)
).toBeTruthy();
});
});
describe("invalid cases", () => {
test("should return false for null", () => {
expect(isValidCslp(null)).toBeFalsy();
});
test("should return false for undefined", () => {
expect(isValidCslp(undefined)).toBeFalsy();
});
test("should return false for empty string", () => {
expect(isValidCslp("")).toBeFalsy();
});
test("should return false for string with less than 3 parts", () => {
expect(isValidCslp("invalid")).toBeFalsy();
});
test("should return false for string with only 2 parts", () => {
expect(isValidCslp("a.b")).toBeFalsy();
});
test("should return false for v2 prefix with no data", () => {
expect(isValidCslp("v2:")).toBeFalsy();
});
test("should return false for v2 prefix with only 2 parts", () => {
expect(isValidCslp("v2:a.b")).toBeFalsy();
});
test("should return false for v2 format where entry_uid_variant_uid has no underscore", () => {
expect(
isValidCslp("v2:content_type_uid.entry.locale")
).toBeFalsy();
});
test("should return false for v2 format where entry_uid_variant_uid is missing variant_uid", () => {
expect(
isValidCslp("v2:content_type_uid.entry_.locale")
).toBeFalsy();
});
test("should return false for v2 format where entry_uid_variant_uid is missing entry_uid", () => {
expect(
isValidCslp("v2:content_type_uid._variant_uid.locale")
).toBeFalsy();
});
test("should return false for v2 format with empty parts", () => {
expect(isValidCslp("v2:..locale")).toBeFalsy();
});
test("should return false for v2 format with empty content_type_uid", () => {
expect(isValidCslp("v2:.entry_uid_variant_uid.locale")).toBeFalsy();
});
test("should return false for v2 format with empty locale", () => {
expect(
isValidCslp("v2:content_type_uid.entry_uid_variant_uid.")
).toBeFalsy();
});
test("should return false for v1 format with empty parts", () => {
expect(isValidCslp("..locale")).toBeFalsy();
});
test("should return false for v1 format with empty content_type_uid", () => {
expect(isValidCslp(".entry_uid.locale")).toBeFalsy();
});
test("should return false for v1 format with empty entry_uid", () => {
expect(isValidCslp("content_type_uid..locale")).toBeFalsy();
});
test("should return false for whitespace-only string", () => {
expect(isValidCslp(" ")).toBeFalsy();
});
test("should return false for tab and newline whitespace", () => {
expect(isValidCslp("\t\n")).toBeFalsy();
});
test("should return false for string with only dots", () => {
expect(isValidCslp("...")).toBeFalsy();
});
test("should return false for string with only two dots", () => {
expect(isValidCslp("..")).toBeFalsy();
});
});
});
describe("extractDetailsFromCslp", () => {
test("should extract details from a CSLP value string with nested multiple field", () => {
const cslpValue =
"content_type_uid.entry_uid.locale.field1.field2.0.field3";
const expected = {
entry_uid: "entry_uid",
content_type_uid: "content_type_uid",
locale: "locale",
cslpValue:
"content_type_uid.entry_uid.locale.field1.field2.0.field3",
fieldPath: "field1.field2.field3",
fieldPathWithIndex: "field1.field2.0.field3",
multipleFieldMetadata: {
parentDetails: {
parentPath: "field1.field2",
parentCslpValue:
"content_type_uid.entry_uid.locale.field1.field2",
},
index: 0,
},
instance: {
fieldPathWithIndex: "field1.field2.0.field3",
}
};
expect(extractDetailsFromCslp(cslpValue)).toEqual(expected);
});
test("should handle a CSLP value string with no multiple field", () => {
const cslpValue =
"content_type_uid.entry_uid.locale.field1.field2.field3";
const expected = {
entry_uid: "entry_uid",
content_type_uid: "content_type_uid",
locale: "locale",
cslpValue: "content_type_uid.entry_uid.locale.field1.field2.field3",
fieldPath: "field1.field2.field3",
fieldPathWithIndex: "field1.field2.field3",
multipleFieldMetadata: {
parentDetails: null,
index: -1,
},
instance: {
fieldPathWithIndex: "field1.field2.field3",
}
};
expect(extractDetailsFromCslp(cslpValue)).toEqual(expected);
});
test("should handle a CSLP value string with one level multiple field", () => {
const cslpValue = "content_type_uid.entry_uid.locale.field.0";
const expected = {
entry_uid: "entry_uid",
content_type_uid: "content_type_uid",
locale: "locale",
cslpValue: "content_type_uid.entry_uid.locale.field.0",
fieldPath: "field",
fieldPathWithIndex: "field",
multipleFieldMetadata: {
parentDetails: {
parentCslpValue: "content_type_uid.entry_uid.locale.field",
parentPath: "field",
},
index: 0,
},
instance: {
fieldPathWithIndex: "field.0",
}
};
expect(extractDetailsFromCslp(cslpValue)).toEqual(expected);
});
test("should handle a CSLP value string with nested group field", () => {
const cslpValue =
"content_type_uid.entry_uid.locale.field1.0.field3.field4.3";
const expected = {
entry_uid: "entry_uid",
content_type_uid: "content_type_uid",
locale: "locale",
cslpValue:
"content_type_uid.entry_uid.locale.field1.0.field3.field4.3",
fieldPath: "field1.field3.field4",
fieldPathWithIndex: "field1.0.field3.field4",
multipleFieldMetadata: {
parentDetails: {
parentCslpValue:
"content_type_uid.entry_uid.locale.field1.0.field3.field4",
parentPath: "field1.0.field3.field4",
},
index: 3,
},
instance: {
fieldPathWithIndex: "field1.0.field3.field4.3",
}
};
expect(extractDetailsFromCslp(cslpValue)).toEqual(expected);
});
});