@integry/sdk 4.6.10 → 4.6.12
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/.vscode/launch.json +2 -2
- package/THIRD_PARTY_LICENSES +37 -0
- package/dist/esm/index.csm.js +1 -7310
- package/dist/umd/index.umd.js +1 -7310
- package/generateTests.js +50 -21
- package/package.json +2 -1
- package/src/components/Button/index.test.ts +0 -0
- package/src/components/form/ObjectField/__snapshots__/index.ts.test.tsx.snap +3 -0
- package/src/components/form/ObjectField/index.ts +5 -1
- package/src/components/form/ObjectField/index.ts.test.tsx +0 -108
- package/src/features/common/FunctionForm/index.ts +9 -0
package/generateTests.js
CHANGED
|
@@ -6,45 +6,74 @@ import dotenv from 'dotenv';
|
|
|
6
6
|
dotenv.config();
|
|
7
7
|
|
|
8
8
|
const openai = new OpenAI({
|
|
9
|
-
apiKey: "sk-
|
|
9
|
+
apiKey: "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",
|
|
10
10
|
});
|
|
11
11
|
|
|
12
|
-
async function generateTest(filePath) {
|
|
12
|
+
async function generateTest(filePath, feedback = '') {
|
|
13
13
|
const code = fs.readFileSync(filePath, 'utf-8');
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
let messages = [
|
|
16
|
+
{ role: 'system', content: 'You are a senior Preact developer with expertise in testing. Use Vitest framework.' },
|
|
17
|
+
{ role: 'user', content: `Write Vitest unit tests for the following Preact component:\n${code}\n
|
|
18
|
+
Make sure to cover:
|
|
19
|
+
- Props validation
|
|
20
|
+
- Event handlers
|
|
21
|
+
- Conditional rendering
|
|
22
|
+
- User interactions
|
|
23
|
+
- Edge cases
|
|
24
|
+
` }
|
|
25
|
+
];
|
|
17
26
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
|
|
27
|
+
if (feedback) {
|
|
28
|
+
messages.push({
|
|
29
|
+
role: 'user',
|
|
30
|
+
content: `Here is the previous generated test:\n${feedback}\nPlease improve the tests based on the following feedback:
|
|
31
|
+
- Add more edge cases.
|
|
32
|
+
- You didn't test fields inside objects represented by field.properties or field.template_fields
|
|
33
|
+
- If object has no field, it should render a textarea.`
|
|
34
|
+
});
|
|
35
|
+
}
|
|
25
36
|
|
|
26
37
|
const completion = await openai.chat.completions.create({
|
|
27
38
|
model: 'gpt-4-turbo',
|
|
28
|
-
messages
|
|
29
|
-
|
|
30
|
-
{ role: 'user', content: prompt },
|
|
31
|
-
],
|
|
32
|
-
temperature: 0.2,
|
|
39
|
+
messages,
|
|
40
|
+
temperature: 0.3,
|
|
33
41
|
});
|
|
34
42
|
|
|
35
43
|
const generatedTests = completion.choices[0]?.message?.content || '';
|
|
36
44
|
|
|
37
|
-
const testFilePath =
|
|
45
|
+
const testFilePath = filePath.replace('.ts', '.test.ts');
|
|
38
46
|
fs.writeFileSync(testFilePath, generatedTests);
|
|
39
|
-
|
|
47
|
+
|
|
48
|
+
console.log(`✅ Test file generated: ${testFilePath}`);
|
|
49
|
+
|
|
50
|
+
return generatedTests;
|
|
40
51
|
}
|
|
41
52
|
|
|
42
53
|
async function main() {
|
|
43
|
-
const componentsFolder = './src/components/form/ObjectField
|
|
54
|
+
// const componentsFolder = './src/components/form/ObjectField'; // Update this path
|
|
55
|
+
const componentsFolder = process.argv[2]; // Get folder path from command line argument
|
|
56
|
+
|
|
57
|
+
if (!componentsFolder) {
|
|
58
|
+
console.error('❌ Please provide the components folder path as an argument.');
|
|
59
|
+
process.exit(1);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!fs.existsSync(componentsFolder)) {
|
|
63
|
+
console.error(`❌ Path "${componentsFolder}" does not exist.`);
|
|
64
|
+
process.exit(1);
|
|
65
|
+
}
|
|
44
66
|
const files = fs.readdirSync(componentsFolder).filter((file) => file.endsWith('.ts'));
|
|
45
|
-
|
|
67
|
+
|
|
46
68
|
for (const file of files) {
|
|
47
|
-
|
|
69
|
+
const testFilePath = path.join(componentsFolder, `${file.replace('.ts', '.test.ts')}`);
|
|
70
|
+
let previousTest = '';
|
|
71
|
+
|
|
72
|
+
if (fs.existsSync(testFilePath)) {
|
|
73
|
+
previousTest = fs.readFileSync(testFilePath, 'utf-8');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await generateTest(path.join(componentsFolder, file), previousTest);
|
|
48
77
|
}
|
|
49
78
|
}
|
|
50
79
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@integry/sdk",
|
|
3
|
-
"version": "4.6.
|
|
3
|
+
"version": "4.6.12",
|
|
4
4
|
"description": "Integry SDK",
|
|
5
5
|
"main": "dist/umd/index.umd.js",
|
|
6
6
|
"module": "dist/esm/index.csm.js",
|
|
@@ -51,6 +51,7 @@
|
|
|
51
51
|
"@storybook/preact": "^8.5.7",
|
|
52
52
|
"@storybook/preact-webpack5": "^8.5.7",
|
|
53
53
|
"@storybook/preset-scss": "^1.0.3",
|
|
54
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
54
55
|
"@testing-library/preact": "^2.0.1",
|
|
55
56
|
"@testing-library/user-event": "^14.6.1",
|
|
56
57
|
"@types/jest": "^26.0.23",
|
|
File without changes
|
|
@@ -367,7 +367,11 @@ const ObjectField = (props: ObjectFieldProps) => {
|
|
|
367
367
|
isMappable=${activityOutputDataRaw &&
|
|
368
368
|
Object.entries(activityOutputDataRaw).length > 0}
|
|
369
369
|
allowTagsInText=${activityOutputDataRaw}
|
|
370
|
-
value=${
|
|
370
|
+
value=${objectHasFields
|
|
371
|
+
? (objectArray[objectIndex] || {})[
|
|
372
|
+
fieldDetails?.meta?.machine_name || ''
|
|
373
|
+
] || ''
|
|
374
|
+
: formatValueForTextarea(objectValue)}
|
|
371
375
|
><//>
|
|
372
376
|
`}
|
|
373
377
|
${fieldType === 'CHECKBOX' &&
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
To write Vitest unit tests for the `ObjectField` Preact component, we need to cover various aspects such as props validation, event handlers, conditional rendering, user interactions, and edge cases. Below are examples of how these tests can be structured using Vitest along with the Testing Library for Preact.
|
|
2
|
-
|
|
3
|
-
### Setup
|
|
4
|
-
|
|
5
|
-
First, ensure you have the necessary dependencies:
|
|
6
|
-
|
|
7
|
-
```bash
|
|
8
|
-
npm install vitest @testing-library/preact @testing-library/user-event --save-dev
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
### Test File: `ObjectField.test.tsx`
|
|
12
|
-
|
|
13
|
-
```tsx
|
|
14
|
-
import { describe, it, expect, vi } from 'vitest';
|
|
15
|
-
import { render, fireEvent, screen } from '@testing-library/preact';
|
|
16
|
-
import userEvent from '@testing-library/user-event';
|
|
17
|
-
import { ObjectField } from './ObjectField';
|
|
18
|
-
|
|
19
|
-
describe('ObjectField Component', () => {
|
|
20
|
-
const mockOnChange = vi.fn();
|
|
21
|
-
const mockOnChangeInFlow = vi.fn();
|
|
22
|
-
const mockApiHandler = vi.fn();
|
|
23
|
-
|
|
24
|
-
const baseProps = {
|
|
25
|
-
field: {
|
|
26
|
-
title: 'Test Field',
|
|
27
|
-
description: 'Test Description',
|
|
28
|
-
template_fields: [],
|
|
29
|
-
type: 'TEXTFIELD',
|
|
30
|
-
properties: {},
|
|
31
|
-
},
|
|
32
|
-
functionArguments: {},
|
|
33
|
-
connectedAccount: 'test-account',
|
|
34
|
-
appName: 'Test App',
|
|
35
|
-
onChange: mockOnChange,
|
|
36
|
-
onChangeInFlow: mockOnChangeInFlow,
|
|
37
|
-
apiHandler: mockApiHandler,
|
|
38
|
-
activityOutputData: {},
|
|
39
|
-
activityOutputDataRaw: {},
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
it('renders without crashing', () => {
|
|
43
|
-
const { container } = render(<ObjectField {...baseProps} />);
|
|
44
|
-
expect(container).toBeInTheDocument();
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it('calls onChange when input changes', async () => {
|
|
48
|
-
const user = userEvent.setup();
|
|
49
|
-
render(<ObjectField {...baseProps} />);
|
|
50
|
-
const input = screen.getByRole('textbox');
|
|
51
|
-
await user.type(input, 'New Value');
|
|
52
|
-
expect(mockOnChange).toHaveBeenCalled();
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
it('conditionally renders add more button based on isArray prop', () => {
|
|
56
|
-
const { rerender } = render(<ObjectField {...baseProps} isArray={true} />);
|
|
57
|
-
expect(screen.queryByText('+ Add another Test Field')).toBeInTheDocument();
|
|
58
|
-
|
|
59
|
-
rerender(<ObjectField {...baseProps} isArray={false} />);
|
|
60
|
-
expect(screen.queryByText('+ Add another Test Field')).not.toBeInTheDocument();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('handles array of objects and adds more on button click', async () => {
|
|
64
|
-
const user = userEvent.setup();
|
|
65
|
-
render(<ObjectField {...baseProps} isArray={true} />);
|
|
66
|
-
await user.click(screen.getByText('+ Add another Test Field'));
|
|
67
|
-
expect(screen.getAllByText('Test Field').length).toBe(2);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('removes an item from the list when delete is clicked', async () => {
|
|
71
|
-
const user = userEvent.setup();
|
|
72
|
-
render(<ObjectField {...baseProps} isArray={true} objectValue={[{}, {}]} />);
|
|
73
|
-
expect(screen.getAllByText('Test Field').length).toBe(2);
|
|
74
|
-
await user.click(screen.getAllByRole('button')[0]); // Assuming the delete button is the first button
|
|
75
|
-
expect(screen.getAllByText('Test Field').length).toBe(1);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('validates props and throws if required props are missing', () => {
|
|
79
|
-
const consoleError = vi.spyOn(console, 'error');
|
|
80
|
-
consoleError.mockImplementation(() => {});
|
|
81
|
-
|
|
82
|
-
expect(() => render(<ObjectField />)).toThrow();
|
|
83
|
-
expect(consoleError).toHaveBeenCalled();
|
|
84
|
-
|
|
85
|
-
consoleError.mockRestore();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
// Additional tests can be added here to cover more complex interactions and edge cases
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
### Explanation
|
|
93
|
-
|
|
94
|
-
1. **Setup and Imports**: We import necessary functions from Vitest and Testing Library. We also mock functions that will be used as props.
|
|
95
|
-
|
|
96
|
-
2. **Base Props**: A base props object is defined to reuse in multiple tests, ensuring consistency and reducing redundancy.
|
|
97
|
-
|
|
98
|
-
3. **Simple Render Test**: Checks if the component renders without throwing.
|
|
99
|
-
|
|
100
|
-
4. **Event Handler Test**: Simulates user input and checks if the appropriate handler (`onChange`) is called.
|
|
101
|
-
|
|
102
|
-
5. **Conditional Rendering**: Tests the conditional rendering of the "Add another" button based on the `isArray` prop.
|
|
103
|
-
|
|
104
|
-
6. **Dynamic List Manipulation**: Tests adding and removing items from a list, which is crucial for components managing arrays of data.
|
|
105
|
-
|
|
106
|
-
7. **Props Validation**: Optionally, you can test for required props and ensure the component throws or logs errors when props are missing or invalid.
|
|
107
|
-
|
|
108
|
-
This setup provides a robust starting point for testing the `ObjectField` component comprehensively, ensuring it behaves correctly under various conditions and interactions.
|
|
@@ -193,6 +193,15 @@ class FunctionForm extends Component<
|
|
|
193
193
|
value = value === '' || value === '[]' ? [] : value;
|
|
194
194
|
if (fieldDetails.items && fieldDetails.items.type === 'number') {
|
|
195
195
|
value = value.map((item: string) => Number(item));
|
|
196
|
+
} else if (
|
|
197
|
+
fieldDetails.items &&
|
|
198
|
+
fieldDetails.items.type === 'string'
|
|
199
|
+
) {
|
|
200
|
+
try {
|
|
201
|
+
value = JSON.parse(value);
|
|
202
|
+
} catch (e) {
|
|
203
|
+
value = [];
|
|
204
|
+
}
|
|
196
205
|
}
|
|
197
206
|
|
|
198
207
|
break;
|