@gem-sdk/system 1.58.0-dev.142 → 1.58.0-dev.143
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/dist/cjs/index.js +0 -6
- package/dist/esm/index.js +0 -1
- package/dist/types/index.d.ts +1 -8
- package/package.json +6 -15
- package/src/component/__tests__/ template.test.tsx +76 -0
- package/src/component/__tests__/createAttr.test.ts +62 -0
- package/src/component/__tests__/createClass.test.ts +68 -0
- package/src/component/__tests__/createContent.test.ts +52 -0
- package/src/component/__tests__/createStateOrContext.test.ts +129 -0
- package/src/component/__tests__/createStyle.test.ts +63 -0
- package/src/component/createAttr.ts +44 -0
- package/src/component/createClass.ts +48 -0
- package/src/component/createContent.ts +20 -0
- package/src/component/createStateOrContext.ts +70 -0
- package/src/component/createStyle.ts +53 -0
- package/src/component/template.ts +119 -0
- package/src/component/types.ts +9 -0
- package/src/component/utils/__tests__/toCamelCaseKeys.test.ts +79 -0
- package/src/component/utils/toCamelCaseKeys.ts +20 -0
- package/src/e2e-tests/README.md +1 -0
- package/src/examples/components/text/DemoText.liquid.ts +49 -0
- package/src/examples/components/text/DemoText.tsx +50 -0
- package/src/examples/components/text/common/__tests__/globalTypoClasses.test.ts +11 -0
- package/src/examples/components/text/common/getAttr.ts +7 -0
- package/src/examples/components/text/common/getStyle.ts +5 -0
- package/src/examples/components/text/common/globalTypoClasses.ts +5 -0
- package/src/examples/components/text/e2e-tests/DemoText.spec.tsx +23 -0
- package/src/examples/components/text/e2e-tests/DemoText.tsx +23 -0
- package/src/index.ts +34 -0
- package/src/validator/README.md +1 -0
- package/dist/cjs/component/template.js +0 -89
- package/dist/esm/component/template.js +0 -83
package/dist/cjs/index.js
CHANGED
|
@@ -5,7 +5,6 @@ var createStyle = require('./component/createStyle.js');
|
|
|
5
5
|
var createContent = require('./component/createContent.js');
|
|
6
6
|
var createClass = require('./component/createClass.js');
|
|
7
7
|
var createStateOrContext = require('./component/createStateOrContext.js');
|
|
8
|
-
var template = require('./component/template.js');
|
|
9
8
|
|
|
10
9
|
const createAttrReact = createAttr.createAttr;
|
|
11
10
|
const createContentReact = createContent.createContent;
|
|
@@ -17,11 +16,6 @@ exports.createStyleReact = createStyle.createStyleReact;
|
|
|
17
16
|
exports.createContent = createContent.createContent;
|
|
18
17
|
exports.createClass = createClass.createClass;
|
|
19
18
|
exports.createStateOrContext = createStateOrContext.createStateOrContext;
|
|
20
|
-
exports.For = template.For;
|
|
21
|
-
exports.If = template.If;
|
|
22
|
-
exports.Liquid = template.Liquid;
|
|
23
|
-
exports.LiquidFor = template.LiquidFor;
|
|
24
|
-
exports.LiquidIf = template.LiquidIf;
|
|
25
19
|
exports.createAttrReact = createAttrReact;
|
|
26
20
|
exports.createClassReact = createClassReact;
|
|
27
21
|
exports.createContentReact = createContentReact;
|
package/dist/esm/index.js
CHANGED
|
@@ -3,7 +3,6 @@ export { createStyle, createStyleReact } from './component/createStyle.js';
|
|
|
3
3
|
import { createContent } from './component/createContent.js';
|
|
4
4
|
import { createClass } from './component/createClass.js';
|
|
5
5
|
export { createStateOrContext } from './component/createStateOrContext.js';
|
|
6
|
-
export { For, If, Liquid, LiquidFor, LiquidIf } from './component/template.js';
|
|
7
6
|
|
|
8
7
|
const createAttrReact = createAttr;
|
|
9
8
|
const createContentReact = createContent;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -27,13 +27,6 @@ declare const createStateOrContext: (obj: {
|
|
|
27
27
|
[key: string]: any;
|
|
28
28
|
};
|
|
29
29
|
|
|
30
|
-
type CallbackCondition = () => JSX.Element | string;
|
|
31
|
-
declare const Liquid: (code: string) => string;
|
|
32
|
-
declare const For: <T>(items: T[], renderFn: (item: T, index: number) => JSX.Element) => JSX.Element[];
|
|
33
|
-
declare const LiquidFor: (c: string, t: string | CallbackCondition) => string;
|
|
34
|
-
declare const If: (condition: boolean | null | undefined, trueResult: string | JSX.Element | CallbackCondition, falseResult?: string | JSX.Element | CallbackCondition) => JSX.Element | string | null;
|
|
35
|
-
declare const LiquidIf: (c: string, t: string | CallbackCondition, f?: string | CallbackCondition) => string;
|
|
36
|
-
|
|
37
30
|
declare const createAttrReact: (obj: {
|
|
38
31
|
[key: string]: string | number;
|
|
39
32
|
}) => {
|
|
@@ -46,4 +39,4 @@ declare const createClassReact: (obj: {
|
|
|
46
39
|
[key: string]: boolean | undefined;
|
|
47
40
|
}) => string;
|
|
48
41
|
|
|
49
|
-
export {
|
|
42
|
+
export { createAttr, createAttrReact, createClass, createClassReact, createContent, createContentReact, createStateOrContext, createStyle, createStyleReact };
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gem-sdk/system",
|
|
3
|
-
"version": "1.58.0-dev.
|
|
3
|
+
"version": "1.58.0-dev.143",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"sideEffects": false,
|
|
6
|
-
"main": "
|
|
6
|
+
"main": "src/index.ts",
|
|
7
7
|
"files": [
|
|
8
|
-
"dist"
|
|
8
|
+
"dist",
|
|
9
|
+
"src"
|
|
9
10
|
],
|
|
10
11
|
"scripts": {
|
|
11
12
|
"cleanup": "rimraf es dist lib",
|
|
@@ -20,17 +21,7 @@
|
|
|
20
21
|
"type-check": "yarn tsc --noEmit"
|
|
21
22
|
},
|
|
22
23
|
"dependencies": {
|
|
23
|
-
"@gem-sdk/core": "1.58.0-dev.
|
|
24
|
+
"@gem-sdk/core": "1.58.0-dev.135"
|
|
24
25
|
},
|
|
25
|
-
"devDependencies": {}
|
|
26
|
-
"module": "dist/esm/index.js",
|
|
27
|
-
"types": "dist/types/index.d.ts",
|
|
28
|
-
"exports": {
|
|
29
|
-
"./package.json": "./package.json",
|
|
30
|
-
".": {
|
|
31
|
-
"import": "./dist/esm/index.js",
|
|
32
|
-
"require": "./dist/cjs/index.js",
|
|
33
|
-
"types": "./dist/types/index.d.ts"
|
|
34
|
-
}
|
|
35
|
-
}
|
|
26
|
+
"devDependencies": {}
|
|
36
27
|
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, test, expect, jest } from '@jest/globals';
|
|
2
|
+
import { For, If, Liquid, LiquidFor, LiquidIf, Unless, LiquidUnless } from '../template';
|
|
3
|
+
|
|
4
|
+
// Mock callback
|
|
5
|
+
const mockCallback = jest.fn(() => '<div>Mock Element</div>');
|
|
6
|
+
|
|
7
|
+
describe('Testing utility functions', () => {
|
|
8
|
+
test('Liquid should return the input string', () => {
|
|
9
|
+
const input = "Hello, World!";
|
|
10
|
+
const result = Liquid(input);
|
|
11
|
+
expect(result).toBe(input);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('For should render a list of JSX elements', () => {
|
|
15
|
+
const items = [1, 2, 3];
|
|
16
|
+
const renderFn = (item: number) => <div key={item}>Item {item}</div>;
|
|
17
|
+
const result = For(items, renderFn);
|
|
18
|
+
expect(result).toHaveLength(3);
|
|
19
|
+
expect(result[0]).toEqual(<div key={items[0]}>Item {items[0]}</div>);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('LiquidFor should render a Liquid template', () => {
|
|
23
|
+
const code = "item in items";
|
|
24
|
+
const template = "Content";
|
|
25
|
+
const result = LiquidFor(code, template);
|
|
26
|
+
expect(result).toBe(`{% for ${code} %}${template}{% endfor %}`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('If should return trueResult when condition is true', () => {
|
|
30
|
+
const result = If(true, "Yes", "No");
|
|
31
|
+
expect(result).toBe("Yes");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('If should return falseResult when condition is false', () => {
|
|
35
|
+
const result = If(false, "Yes", "No");
|
|
36
|
+
expect(result).toBe("No");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('If should handle JSX.Element', () => {
|
|
40
|
+
const result = If(true, <div>True</div>, <div>False</div>);
|
|
41
|
+
expect(result).toEqual(<div>True</div>);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test('LiquidIf should render a correct Liquid if-else template', () => {
|
|
45
|
+
const condition = "x > 10";
|
|
46
|
+
const trueResult = "Greater";
|
|
47
|
+
const falseResult = "Smaller";
|
|
48
|
+
const result = LiquidIf(condition, trueResult, falseResult);
|
|
49
|
+
expect(result).toBe(`{% if ${condition} %}${trueResult}{% else %}${falseResult}{% endif %}`);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('Unless should reverse the condition', () => {
|
|
53
|
+
const result = Unless(false, "True", "False");
|
|
54
|
+
expect(result).toBe("True");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('LiquidUnless should render a correct Liquid unless-else template', () => {
|
|
58
|
+
const condition = "x > 10";
|
|
59
|
+
const trueResult = "Greater";
|
|
60
|
+
const falseResult = "Smaller";
|
|
61
|
+
const result = LiquidUnless(condition, trueResult, falseResult);
|
|
62
|
+
expect(result).toBe(`{% unless ${condition} %}${trueResult}{% else %}${falseResult}{% endunless %}`);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('If should handle callback conditions', () => {
|
|
66
|
+
const trueResult = mockCallback;
|
|
67
|
+
If(true, trueResult);
|
|
68
|
+
expect(mockCallback).toHaveBeenCalledTimes(1);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
test('LiquidIf should handle callback conditions', () => {
|
|
72
|
+
const condition = "x > 10";
|
|
73
|
+
LiquidIf(condition, mockCallback);
|
|
74
|
+
expect(mockCallback).toHaveBeenCalledTimes(2); // Call count should include other tests
|
|
75
|
+
});
|
|
76
|
+
});
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
import { createAttr } from '../createAttr';
|
|
3
|
+
|
|
4
|
+
// Mock console.error to capture error messages
|
|
5
|
+
global.console.error = jest.fn();
|
|
6
|
+
|
|
7
|
+
describe('createAttr', () => {
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
// Clear the console.error mock before each test
|
|
10
|
+
(console.error as jest.Mock).mockClear();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('should log error for key without "data-gp-" prefix', () => {
|
|
14
|
+
createAttr({ invalidKey: 'some value' });
|
|
15
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
16
|
+
'Invalid attribute key: "invalidKey". Must start with "data-gp-".',
|
|
17
|
+
);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
test('should log error for key with uppercase letters', () => {
|
|
21
|
+
createAttr({ 'data-gp-UppercaseKey': 'some value' });
|
|
22
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
23
|
+
'Invalid attribute key: "data-gp-UppercaseKey". Must not contain uppercase letters.',
|
|
24
|
+
);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test('should log error for nested object', () => {
|
|
28
|
+
createAttr({ 'data-gp-nested': { fontSize: '12px' } } as any);
|
|
29
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
30
|
+
'Invalid nested attribute for key "data-gp-nested". Nested objects are not supported.',
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should log error for invalid value type', () => {
|
|
35
|
+
createAttr({ 'data-gp-invalidType': true as any });
|
|
36
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
37
|
+
'Invalid attribute value for key "data-gp-invalidType": true. Must be a string or number.',
|
|
38
|
+
);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('should handle multiple errors in one call', () => {
|
|
42
|
+
createAttr({
|
|
43
|
+
invalidKey: 'some value', // Invalid prefix
|
|
44
|
+
'data-gp-UppercaseKey': 'some value', // Uppercase in key
|
|
45
|
+
'data-gp-nested': { fontSize: '12px' }, // Nested object
|
|
46
|
+
'data-gp-invalidType': true as any, // Invalid value type
|
|
47
|
+
} as any);
|
|
48
|
+
|
|
49
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
50
|
+
'Invalid attribute key: "invalidKey". Must start with "data-gp-".',
|
|
51
|
+
);
|
|
52
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
53
|
+
'Invalid attribute key: "data-gp-UppercaseKey". Must not contain uppercase letters.',
|
|
54
|
+
);
|
|
55
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
56
|
+
'Invalid nested attribute for key "data-gp-nested". Nested objects are not supported.',
|
|
57
|
+
);
|
|
58
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
59
|
+
'Invalid attribute value for key "data-gp-invalidType": true. Must be a string or number.',
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, test, expect, beforeAll, afterEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
// Import the function
|
|
4
|
+
import { createClass } from '../createClass';
|
|
5
|
+
|
|
6
|
+
// Declare console.error as a Jest mock
|
|
7
|
+
beforeAll(() => {
|
|
8
|
+
global.console.error = jest.fn();
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
// Clear the console.error mock after each test
|
|
13
|
+
(console.error as jest.Mock).mockClear();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('createClass', () => {
|
|
17
|
+
test('should return empty string and log error for invalid input', () => {
|
|
18
|
+
expect(createClass(null as any)).toBe('');
|
|
19
|
+
expect(console.error).toHaveBeenCalledWith('Expected an object as input.');
|
|
20
|
+
|
|
21
|
+
expect(createClass(123 as any)).toBe('');
|
|
22
|
+
expect(console.error).toHaveBeenCalledWith('Expected an object as input.');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
test('should log an error for class names exceeding 30 characters', () => {
|
|
26
|
+
createClass({ averyverylongclassnameexceeding30characters: true });
|
|
27
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
28
|
+
'Class name "averyverylongclassnameexceeding30characters" exceeds the maximum length of 30 characters.',
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('should log an error for class names with spaces', () => {
|
|
33
|
+
createClass({ 'class with space': true });
|
|
34
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
35
|
+
'Class name "class with space" should not contain spaces.',
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('should log an error for class names with uppercase letters', () => {
|
|
40
|
+
createClass({ ClassWithUpperCase: true });
|
|
41
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
42
|
+
'Class name "ClassWithUpperCase" should be in lowercase.',
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test('should not log any errors for valid class names', () => {
|
|
47
|
+
createClass({ validclassname: true, anotherclass: true });
|
|
48
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('should handle multiple errors in one call', () => {
|
|
52
|
+
createClass({
|
|
53
|
+
ValidClassWithUpper: true,
|
|
54
|
+
'class with space': true,
|
|
55
|
+
averyverylongclassnameexceeding30characters: true,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
59
|
+
'Class name "ValidClassWithUpper" should be in lowercase.',
|
|
60
|
+
);
|
|
61
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
62
|
+
'Class name "class with space" should not contain spaces.',
|
|
63
|
+
);
|
|
64
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
65
|
+
'Class name "averyverylongclassnameexceeding30characters" exceeds the maximum length of 30 characters.',
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { createContent } from '../createContent';
|
|
4
|
+
|
|
5
|
+
// Mock console.error to capture error messages
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createContent', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the console.error mock before each test
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should return empty string and log error if content is not a string', () => {
|
|
15
|
+
expect(createContent(12345 as any)).toBe('');
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith('Invalid content type: Content must be a string.');
|
|
17
|
+
|
|
18
|
+
expect(createContent({} as any)).toBe('');
|
|
19
|
+
expect(console.error).toHaveBeenCalledWith('Invalid content type: Content must be a string.');
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('should return empty string and log error for content with invalid {{}} pattern', () => {
|
|
23
|
+
expect(createContent('{{Hello}} there')).toBe('');
|
|
24
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
25
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
26
|
+
);
|
|
27
|
+
|
|
28
|
+
expect(createContent('Welcome {{World}}')).toBe('');
|
|
29
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
30
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('should return valid content without errors', () => {
|
|
35
|
+
expect(createContent('This is valid content')).toBe('This is valid content');
|
|
36
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
37
|
+
|
|
38
|
+
expect(createContent('Content with {{123}} is valid')).toBe('Content with {{123}} is valid');
|
|
39
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
40
|
+
|
|
41
|
+
expect(createContent('Mix of {{valid1}} and {{valid2}}')).toBe(
|
|
42
|
+
'Mix of {{valid1}} and {{valid2}}',
|
|
43
|
+
);
|
|
44
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('should pass with valid {{}} pattern containing more than just letters', () => {
|
|
48
|
+
const content = '{{ product.title | t }}';
|
|
49
|
+
expect(createContent(content)).toBe(content);
|
|
50
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
51
|
+
});
|
|
52
|
+
});
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
import { createStateOrContext } from '../createStateOrContext';
|
|
4
|
+
|
|
5
|
+
// Mock console.error to capture error messages
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createStateOrContext', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the console.error mock before each test
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should log error for key length exceeding 20 characters', () => {
|
|
15
|
+
createStateOrContext({ thisKeyIsWayTooLongForOurRequirements: 'value' });
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
17
|
+
'Invalid key "thisKeyIsWayTooLongForOurRequirements": Key length must not exceed 20 characters.',
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should log error for key containing special characters', () => {
|
|
22
|
+
createStateOrContext({ invalid_key: 'value' });
|
|
23
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
24
|
+
'Invalid key "invalid_key": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should log error for key containing numbers', () => {
|
|
29
|
+
createStateOrContext({ invalidKey1: 'value' });
|
|
30
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
31
|
+
'Invalid key "invalidKey1": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
32
|
+
);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test('should log error for key not in camelCase format', () => {
|
|
36
|
+
createStateOrContext({ Invalidkey: 'value' });
|
|
37
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
38
|
+
'Invalid key "Invalidkey": Key must be in camelCase format.',
|
|
39
|
+
);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('should log error for nested object deeper than 3 levels', () => {
|
|
43
|
+
createStateOrContext({
|
|
44
|
+
levelOne: {
|
|
45
|
+
levelTwo: {
|
|
46
|
+
levelThree: {
|
|
47
|
+
tooDeepKey: 'too deep',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
53
|
+
'Invalid structure: Data must not be nested deeper than 3 levels.',
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should log error for invalid values (undefined, null, blank, false)', () => {
|
|
58
|
+
createStateOrContext({
|
|
59
|
+
validKey: 'value', // Valid
|
|
60
|
+
undefinedKey: undefined, // Invalid
|
|
61
|
+
nullKey: null, // Invalid
|
|
62
|
+
emptyStringKey: '', // Invalid
|
|
63
|
+
falseKey: false, // Invalid
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
67
|
+
'Invalid value for key "undefinedKey": Value must not be undefined, null, blank, or false.',
|
|
68
|
+
);
|
|
69
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
70
|
+
'Invalid value for key "nullKey": Value must not be undefined, null, blank, or false.',
|
|
71
|
+
);
|
|
72
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
73
|
+
'Invalid value for key "emptyStringKey": Value must not be undefined, null, blank, or false.',
|
|
74
|
+
);
|
|
75
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
76
|
+
'Invalid value for key "falseKey": Value must not be undefined, null, blank, or false.',
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('should not log any errors for valid keys and values', () => {
|
|
81
|
+
createStateOrContext({
|
|
82
|
+
validKey: 'value',
|
|
83
|
+
anotherValidKey: 123,
|
|
84
|
+
nestedObject: {
|
|
85
|
+
validNestedKey: 'nestedValue',
|
|
86
|
+
anotherLevel: {
|
|
87
|
+
validKey: 'value',
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test('should handle multiple errors in one call', () => {
|
|
95
|
+
createStateOrContext({
|
|
96
|
+
thisKeyIsWayTooLongForOurRequirements: 'value', // Key length
|
|
97
|
+
invalid_key: 'value', // Special character
|
|
98
|
+
invalidKey1: 'value', // Contains number
|
|
99
|
+
InvalidCamelCase: 'value', // Not camelCase
|
|
100
|
+
validKey: {
|
|
101
|
+
nestedTooDeep: {
|
|
102
|
+
anotherLevel: {
|
|
103
|
+
tooDeepKey: 'too deep', // Exceeds depth
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
undefinedValue: undefined, // Invalid value
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
111
|
+
'Invalid key "thisKeyIsWayTooLongForOurRequirements": Key length must not exceed 20 characters.',
|
|
112
|
+
);
|
|
113
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
114
|
+
'Invalid key "invalid_key": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
115
|
+
);
|
|
116
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
117
|
+
'Invalid key "invalidKey1": Key must contain only alphabetic characters (no numbers or special characters).',
|
|
118
|
+
);
|
|
119
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
120
|
+
'Invalid key "InvalidCamelCase": Key must be in camelCase format.',
|
|
121
|
+
);
|
|
122
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
123
|
+
'Invalid structure: Data must not be nested deeper than 3 levels.',
|
|
124
|
+
);
|
|
125
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
126
|
+
'Invalid value for key "undefinedValue": Value must not be undefined, null, blank, or false.',
|
|
127
|
+
);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, test, expect, beforeEach, jest } from '@jest/globals';
|
|
2
|
+
|
|
3
|
+
// Import the function
|
|
4
|
+
import { createStyle } from '../createStyle';
|
|
5
|
+
|
|
6
|
+
global.console.error = jest.fn();
|
|
7
|
+
|
|
8
|
+
describe('createStyle', () => {
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
// Clear the mock for console.error
|
|
11
|
+
(console.error as jest.Mock).mockClear();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('should log error for ignored property', () => {
|
|
15
|
+
createStyle({ ignore: 'some value' });
|
|
16
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
17
|
+
'Ignored property detected: "ignore". This property is not supported in the current styling setup.',
|
|
18
|
+
);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('should log error for invalid property type', () => {
|
|
22
|
+
createStyle({ 'valid-property': true as any });
|
|
23
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
24
|
+
'Invalid style value for "valid-property": true. Must be a string or number.',
|
|
25
|
+
);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('should not log any errors for valid keys and values', () => {
|
|
29
|
+
createStyle({
|
|
30
|
+
'background-color': 'blue',
|
|
31
|
+
padding: 10,
|
|
32
|
+
});
|
|
33
|
+
expect(console.error).not.toHaveBeenCalled();
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test('should log error for key containing uppercase letters', () => {
|
|
37
|
+
createStyle({ fontSize: '14px' });
|
|
38
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
39
|
+
'Invalid key "fontSize": Keys must be lowercase letters and may only contain "-".',
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('should log error for key containing numbers', () => {
|
|
44
|
+
createStyle({ border1: 'solid' });
|
|
45
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
46
|
+
'Invalid key "border1": Keys must be lowercase letters and may only contain "-".',
|
|
47
|
+
);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('should log error for key containing special characters other than "-"', () => {
|
|
51
|
+
createStyle({ invalid_key: 'value' });
|
|
52
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
53
|
+
'Invalid key "invalid_key": Keys must be lowercase letters and may only contain "-".',
|
|
54
|
+
);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('should log error for nested objects', () => {
|
|
58
|
+
createStyle({ nestedProp: { width: '100px' } } as any);
|
|
59
|
+
expect(console.error).toHaveBeenCalledWith(
|
|
60
|
+
'Invalid nested object for property "nestedProp". Nested objects are not supported.',
|
|
61
|
+
);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export const createAttr = (obj: { [key: string]: string | number }) => {
|
|
2
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
3
|
+
console.error('Expected an object as input.');
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
const isDevOrStaging =
|
|
8
|
+
!process.env.APP_ENV ||
|
|
9
|
+
process.env.APP_ENV === 'development' ||
|
|
10
|
+
process.env.APP_ENV === 'staging';
|
|
11
|
+
|
|
12
|
+
if (isDevOrStaging) {
|
|
13
|
+
for (const key in obj) {
|
|
14
|
+
const value = obj[key];
|
|
15
|
+
|
|
16
|
+
// 1. Check if the key starts with "data-gp-"
|
|
17
|
+
if (!key.startsWith('data-gp-')) {
|
|
18
|
+
console.error(`Invalid attribute key: "${key}". Must start with "data-gp-".`);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// 2. Check if the key contains uppercase letters
|
|
22
|
+
if (key !== key.toLowerCase()) {
|
|
23
|
+
console.error(`Invalid attribute key: "${key}". Must not contain uppercase letters.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// 3. Check if the value is an object (nested object check)
|
|
27
|
+
if (typeof value === 'object' && value !== null) {
|
|
28
|
+
console.error(
|
|
29
|
+
`Invalid nested attribute for key "${key}". Nested objects are not supported.`,
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// 4. Check if the value is a valid type (string or number)
|
|
34
|
+
const isValidType = typeof value === 'string' || typeof value === 'number';
|
|
35
|
+
if (!isValidType) {
|
|
36
|
+
console.error(
|
|
37
|
+
`Invalid attribute value for key "${key}": ${value}. Must be a string or number.`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return obj;
|
|
44
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
type ClassValue = Record<string, boolean | undefined | null> | string | null | undefined;
|
|
2
|
+
|
|
3
|
+
function toVal(mix: ClassValue) {
|
|
4
|
+
if (typeof mix === 'string') {
|
|
5
|
+
return mix;
|
|
6
|
+
} else if (typeof mix === 'object' && mix !== null) {
|
|
7
|
+
return Object.keys(mix)
|
|
8
|
+
.filter((key) => mix[key])
|
|
9
|
+
.join(' ');
|
|
10
|
+
} else {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function cls(...classes: ClassValue[]) {
|
|
16
|
+
return classes.map(toVal).filter(Boolean).join(' ');
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const createClass = (obj: { [key: string]: boolean | undefined }): string => {
|
|
20
|
+
if (typeof obj !== 'object' || obj === null) {
|
|
21
|
+
console.error('Expected an object as input.');
|
|
22
|
+
return '';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const isDevOrStaging =
|
|
26
|
+
!process.env.APP_ENV ||
|
|
27
|
+
process.env.APP_ENV === 'development' ||
|
|
28
|
+
process.env.APP_ENV === 'staging';
|
|
29
|
+
|
|
30
|
+
if (isDevOrStaging) {
|
|
31
|
+
// Validate each class name length
|
|
32
|
+
for (const className in obj) {
|
|
33
|
+
if (className.length > 30) {
|
|
34
|
+
console.error(`Class name "${className}" exceeds the maximum length of 30 characters.`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (className.includes(' ')) {
|
|
38
|
+
console.error(`Class name "${className}" should not contain spaces.`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (className !== className.toLowerCase()) {
|
|
42
|
+
console.error(`Class name "${className}" should be in lowercase.`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return cls(obj);
|
|
48
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const createContent = (content: string): string => {
|
|
2
|
+
// Check if content is a string
|
|
3
|
+
if (typeof content !== 'string') {
|
|
4
|
+
console.error('Invalid content type: Content must be a string.');
|
|
5
|
+
return '';
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
// Regex to match {{}} pattern with only letters inside, e.g., {{Hello}}
|
|
9
|
+
const invalidPattern = /\{\{[A-Za-z]+\}\}/g;
|
|
10
|
+
|
|
11
|
+
// Check if content contains any invalid patterns
|
|
12
|
+
if (invalidPattern.test(content)) {
|
|
13
|
+
console.error(
|
|
14
|
+
'Invalid content format: "{{}}" placeholders must not contain only letters, e.g., "{{Hello}}".',
|
|
15
|
+
);
|
|
16
|
+
return '';
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return content;
|
|
20
|
+
};
|