Skip to content

Commit 7a563ef

Browse files
committed
Create transformation.test.ts
1 parent 1987e0a commit 7a563ef

File tree

1 file changed

+160
-0
lines changed

1 file changed

+160
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
import { constructTransformation, promptArrayToString } from './constructTransformation';
2+
3+
describe('constructTransformation', () => {
4+
5+
it('should construct a transformation string with a prefix, qualifier, and string value', () => {
6+
const transformation = constructTransformation({
7+
prefix: 'w',
8+
qualifier: 'width',
9+
value: '100'
10+
});
11+
expect(transformation).toBe('w_width:100');
12+
});
13+
14+
it('should construct a transformation string with a prefix, qualifier, and number value', () => {
15+
const transformation = constructTransformation({
16+
prefix: 'h',
17+
qualifier: 'height',
18+
value: 250
19+
});
20+
expect(transformation).toBe('h_height:250');
21+
});
22+
23+
it('should construct a transformation string with a qualifier and value when no prefix is provided', () => {
24+
const transformation = constructTransformation({
25+
qualifier: 'c',
26+
value: 'fill'
27+
});
28+
expect(transformation).toBe('c_fill');
29+
});
30+
31+
it('should construct a transformation with only prefix and qualifier for a boolean true value', () => {
32+
const transformation = constructTransformation({
33+
prefix: 'e',
34+
qualifier: 'grayscale',
35+
value: true
36+
});
37+
expect(transformation).toBe('e_grayscale');
38+
});
39+
40+
it('should construct a transformation with only prefix and qualifier for a string "true" value', () => {
41+
const transformation = constructTransformation({
42+
prefix: 'e',
43+
qualifier: 'sharpen',
44+
value: 'true'
45+
});
46+
expect(transformation).toBe('e_sharpen');
47+
});
48+
49+
it('should return only the qualifier when value is true and there is no prefix', () => {
50+
const transformation = constructTransformation({
51+
qualifier: 'grayscale',
52+
value: true
53+
});
54+
expect(transformation).toBe('grayscale');
55+
});
56+
57+
it('should apply converters to the value if the test passes', () => {
58+
const converters = [{
59+
test: (value) => Array.isArray(value),
60+
convert: (value) => value.join(':')
61+
}];
62+
const transformation = constructTransformation({
63+
qualifier: 'co',
64+
value: ['rgb', '2bff00'],
65+
converters
66+
});
67+
expect(transformation).toBe('co_rgb:2bff00');
68+
});
69+
70+
it('should not apply a converter if the test fails', () => {
71+
const converters = [{
72+
test: (value) => Array.isArray(value),
73+
convert: (value) => value.join(':')
74+
}];
75+
const transformation = constructTransformation({
76+
qualifier: 'w',
77+
value: 900,
78+
converters
79+
});
80+
expect(transformation).toBe('w_900');
81+
});
82+
83+
it('should correctly handle a value of 0', () => {
84+
const transformation = constructTransformation({
85+
qualifier: 'q',
86+
value: 0
87+
});
88+
expect(transformation).toBe('q_0');
89+
});
90+
91+
it('should correctly handle an empty string value', () => {
92+
const transformation = constructTransformation({
93+
prefix: 'l',
94+
qualifier: 'text',
95+
value: ''
96+
});
97+
expect(transformation).toBe('l_text:');
98+
});
99+
100+
it('should return undefined if the value is not a boolean, string, or number', () => {
101+
const transformation = constructTransformation({
102+
qualifier: 'a',
103+
value: undefined
104+
});
105+
expect(transformation).toBeUndefined();
106+
});
107+
108+
it('should return undefined for a boolean false value', () => {
109+
const transformation = constructTransformation({
110+
qualifier: 'e',
111+
value: false
112+
});
113+
expect(transformation).toBeUndefined();
114+
});
115+
116+
it('should apply multiple converters in order', () => {
117+
const converters = [
118+
{ test: (v) => typeof v === 'number', convert: (v: number) => v + 1 },
119+
{ test: (v) => typeof v === 'number', convert: (v: number) => v * 2 }
120+
];
121+
const transformation = constructTransformation({
122+
qualifier: 'w',
123+
value: 5,
124+
converters
125+
});
126+
expect(transformation).toBe('w_12'); // (5 + 1) * 2
127+
});
128+
129+
it('should return undefined if value is true but qualifier is missing', () => {
130+
const transformation = constructTransformation({
131+
value: true
132+
});
133+
expect(transformation).toBeUndefined();
134+
});
135+
136+
it('should return undefined if value is null', () => {
137+
const transformation = constructTransformation({
138+
qualifier: 'x',
139+
value: null
140+
});
141+
expect(transformation).toBeUndefined();
142+
});
143+
});
144+
145+
describe('promptArrayToString', () => {
146+
it('should convert an array of strings to a single string with parentheses and semicolons', () => {
147+
const result = promptArrayToString(['hello', 'world', 'test']);
148+
expect(result).toBe('(hello;world;test)');
149+
});
150+
151+
it('should return empty parentheses for an empty array', () => {
152+
const result = promptArrayToString([]);
153+
expect(result).toBe('()');
154+
});
155+
156+
it('should handle array with one element', () => {
157+
const result = promptArrayToString(['single']);
158+
expect(result).toBe('(single)');
159+
});
160+
});

0 commit comments

Comments
 (0)