|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import React from 'react'; |
| 3 | +import { render, screen } from '@testing-library/react'; |
| 4 | +import componentBuilder from '../app/src/helperFunctions/componentBuilder'; |
| 5 | +import { ChildElement, MUIComponent } from '../app/src/interfaces/Interfaces'; |
| 6 | + |
| 7 | +// Mock MUITypes data |
| 8 | +const MUITypes = [ |
| 9 | + { |
| 10 | + tag: 'Button', |
| 11 | + componentData: { |
| 12 | + name: 'button', |
| 13 | + props: { children: 'Click me' } |
| 14 | + } |
| 15 | + } |
| 16 | +]; |
| 17 | +vi.mock('../redux/MUITypes', () => MUITypes); |
| 18 | + |
| 19 | +describe('componentBuilder', () => { |
| 20 | + it('renders a simple input component', () => { |
| 21 | + const elements: Array<ChildElement> = [ |
| 22 | + { |
| 23 | + type: 'HTML Element', |
| 24 | + typeId: 1, |
| 25 | + name: 'input', |
| 26 | + childId: 1, |
| 27 | + style: {}, |
| 28 | + attributes: { cssClasses: 'test-class' }, |
| 29 | + events: {}, |
| 30 | + stateProps: [], |
| 31 | + passedInProps: [], |
| 32 | + children: [] |
| 33 | + } |
| 34 | + ]; |
| 35 | + const result = componentBuilder(elements, 1); |
| 36 | + render(<>{result}</>); |
| 37 | + expect(screen.getByRole('textbox')).toBeInTheDocument(); |
| 38 | + }); |
| 39 | + |
| 40 | + it('handles MUI components', () => { |
| 41 | + const elements: Array<MUIComponent> = [ |
| 42 | + { |
| 43 | + type: 'MUI Component', |
| 44 | + typeId: 2, |
| 45 | + name: 'Button', |
| 46 | + childId: 2, |
| 47 | + style: {}, |
| 48 | + attributes: {}, |
| 49 | + events: {}, |
| 50 | + stateProps: [], |
| 51 | + passedInProps: [], |
| 52 | + children: [] |
| 53 | + } |
| 54 | + ]; |
| 55 | + const result = componentBuilder(elements, 2); |
| 56 | + render(<>{result}</>); |
| 57 | + expect(screen.getByText('Click me')).toBeInTheDocument(); // Assuming 'Click me' is rendered text for Button |
| 58 | + }); |
| 59 | + |
| 60 | + it('skips separators and continues rendering', () => { |
| 61 | + const elements: Array<ChildElement> = [ |
| 62 | + { |
| 63 | + type: 'HTML Element', |
| 64 | + typeId: 1, |
| 65 | + name: 'separator', |
| 66 | + childId: 1, |
| 67 | + style: {}, |
| 68 | + attributes: {}, |
| 69 | + events: {}, |
| 70 | + stateProps: [], |
| 71 | + passedInProps: [], |
| 72 | + children: [] |
| 73 | + }, |
| 74 | + { |
| 75 | + type: 'HTML Element', |
| 76 | + typeId: 1, |
| 77 | + name: 'img', |
| 78 | + childId: 2, |
| 79 | + style: {}, |
| 80 | + attributes: { compLink: 'http://example.com/image.png' }, |
| 81 | + events: {}, |
| 82 | + stateProps: [], |
| 83 | + passedInProps: [], |
| 84 | + children: [] |
| 85 | + } |
| 86 | + ]; |
| 87 | + const result = componentBuilder(elements, 3); |
| 88 | + render(<>{result}</>); |
| 89 | + expect(screen.getByRole('img')).toHaveAttribute( |
| 90 | + 'src', |
| 91 | + 'http://example.com/image.png' |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('renders nested components', () => { |
| 96 | + const elements: Array<ChildElement> = [ |
| 97 | + { |
| 98 | + type: 'HTML Element', |
| 99 | + typeId: 1, |
| 100 | + name: 'div', |
| 101 | + childId: 3, |
| 102 | + style: {}, |
| 103 | + attributes: { cssClasses: 'container' }, |
| 104 | + events: {}, |
| 105 | + stateProps: [], |
| 106 | + passedInProps: [], |
| 107 | + children: [ |
| 108 | + { |
| 109 | + type: 'HTML Element', |
| 110 | + typeId: 1, |
| 111 | + name: 'p', |
| 112 | + childId: 4, |
| 113 | + style: {}, |
| 114 | + attributes: { compText: 'Hello, world!' }, |
| 115 | + events: {}, |
| 116 | + stateProps: [], |
| 117 | + passedInProps: [], |
| 118 | + children: [] |
| 119 | + } |
| 120 | + ] |
| 121 | + } |
| 122 | + ]; |
| 123 | + const result = componentBuilder(elements, 4); |
| 124 | + render(<>{result}</>); |
| 125 | + expect(screen.getByText('Hello, world!')).toBeInTheDocument(); |
| 126 | + expect(screen.getByText('Hello, world!').parentNode).toHaveClass( |
| 127 | + 'container' |
| 128 | + ); |
| 129 | + }); |
| 130 | +}); |
0 commit comments