Skip to content

Commit

Permalink
fix: fixed build & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chesterkmr committed Dec 17, 2024
1 parent 7be92c9 commit e74241b
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Button, useElement } from '@ballerine/ui';
import { Button } from '@/components/atoms';
import { useMemo } from 'react';
import { useValidator } from '../../../Validator';
import { useDynamicForm } from '../../context';
import { useField } from '../../hooks/external';
import { useElement } from '../../hooks/external/useElement';
import { useField } from '../../hooks/external/useField';
import { TBaseFormElements, TDynamicFormElement } from '../../types';

export interface ISubmitButtonParams {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { Button } from '@ballerine/ui';
import { Button } from '@/components/atoms';
import '@testing-library/jest-dom';
import { cleanup, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useValidator } from '../../../Validator';
import { useDynamicForm } from '../../context';
import { useField } from '../../hooks/external';
import { useField } from '../../hooks/external/useField';
import { IFormElement, TBaseFormElements } from '../../types';
import { ISubmitButtonParams, SubmitButton } from './SubmitButton';

vi.mock('@ballerine/ui', () => ({
vi.mock('@/components/atoms', () => ({
Button: vi.fn(),
}));

vi.mock('../../hooks/external/useElement', () => ({
useElement: vi.fn().mockReturnValue({ id: 'test-id' }),
}));

vi.mock('../../hooks/external/useField', () => ({
useField: vi.fn().mockReturnValue({ disabled: false }),
}));

vi.mock('../../../Validator', () => ({
useValidator: vi.fn(),
}));
Expand All @@ -38,7 +45,7 @@ describe('SubmitButton', () => {

beforeEach(() => {
cleanup();
vi.clearAllMocks();
// vi.restoreAllMocks();

vi.mocked(Button).mockImplementation(({ children, ...props }) => (
<button {...props}>{children}</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AutocompleteInput } from '@/components/molecules';
import { createTestId } from '@/components/organisms/Renderer';
import { AutocompleteInput } from '@ballerine/ui';
import { useField } from '../../hooks/external';
import { FieldLayout } from '../../layouts/FieldLayout';
import { TBaseFormElements, TDynamicFormField } from '../../types';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { useStack } from '../FieldList/providers/StackProvider';
import { AutocompleteField, IAutocompleteFieldParams } from './AutocompleteField';

// Mock dependencies
vi.mock('@ballerine/ui', () => ({
vi.mock('@/components/molecules', () => ({
AutocompleteInput: ({ children, options, ...props }: any) => (
<input {...props} options={JSON.stringify(options)} type="text" />
<input {...props} data-options={JSON.stringify(options)} type="text" />
),
}));

Expand Down Expand Up @@ -127,6 +127,9 @@ describe('AutocompleteField', () => {

render(<AutocompleteField element={elementWithOptions} />);

expect(screen.getByTestId('test-id')).toHaveAttribute('options', JSON.stringify(mockOptions));
expect(screen.getByTestId('test-id')).toHaveAttribute(
'data-options',
JSON.stringify(mockOptions),
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const TextField: TDynamicFormField<TBaseFormElements, ITextFieldParams> =
value={value?.toString() || ''} // Ensure value is string or number
/>
)}
<FieldErrors definition={element} />
<FieldErrors element={element} />
</FieldLayout>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { useDynamicForm } from '../../../context';
import { IFormElement } from '../../../types';
import { useElementId } from '../useElementId';

export const useElement = (element: IFormElement, stack: TDeepthLevelStack = []) => {
export const useElement = <TElements, TParams>(
element: IFormElement<TElements, TParams>,
stack: TDeepthLevelStack = [],
) => {
const { values } = useDynamicForm();
const hiddenRulesResult = useRuleEngine(values, {
rules: element.hidden,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { formatId } from '@/components/organisms/Form/Validator/utils/format-id'
import { useMemo } from 'react';
import { IFormElement } from '../../../types';

export const useElementId = (element: IFormElement, stack: TDeepthLevelStack = []) => {
export const useElementId = (element: IFormElement<any, any>, stack: TDeepthLevelStack = []) => {
const formattedId = useMemo(() => formatId(element.id, stack), [element.id, stack]);

return formattedId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import { IFormElement } from '../../types';
import { FieldLayout } from './FieldLayout';

// Mock dependencies
vi.mock('@ballerine/ui', () => ({

vi.mock('@/common', () => ({
ctw: vi.fn((base, conditionals) => base),
}));

vi.mock('@/components/atoms', () => ({
Label: ({ children, ...props }: any) => <label {...props}>{children}</label>,
}));

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import matchers from '@testing-library/jest-dom/matchers';
import { cleanup } from '@testing-library/react';
import { afterEach, expect } from 'vitest';

if(matchers) {
if (matchers) {
// Extend Vitest's expect with jest-dom matchers
expect.extend(matchers);
}
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e74241b

Please sign in to comment.