$ rails g model User
belongs_to
has_one
| // Context | |
| const CartContext = createContext(null) | |
| export const CartProvider = ({ children }) => { | |
| const [items, setItems] = useState([]) | |
| const addItem = (item) => setItems([...items, item]) | |
| const removeItem = (id) => setItems(items.filter(i => i.id !== id)) | |
| return ( |
| // mocks/handlers.ts | |
| import { http, HttpResponse } from 'msw' | |
| export const handlers = [ | |
| http.get('/api/products', () => { | |
| return HttpResponse.json([ | |
| { id: 1, name: 'Product 1', price: 100 }, | |
| { id: 2, name: 'Product 2', price: 200 }, | |
| ]) | |
| }), |
| // Componente completo con múltiples interacciones | |
| describe('Shopping Cart Flow', () => { | |
| it('permite agregar, incrementar y eliminar productos', async () => { | |
| const user = userEvent.setup() | |
| render(<ShoppingApp />) | |
| // Usuario agrega producto | |
| const addButton = screen.getByRole('button', { name: /add to cart/i }) | |
| await user.click(addButton) |
| // utils/formatters.ts | |
| export const formatCurrency = (amount: number, locale = 'en-US') => { | |
| return new Intl.NumberFormat(locale, { | |
| style: 'currency', | |
| currency: 'USD', | |
| }).format(amount) | |
| } | |
| export const truncateText = (text: string, maxLength: number) => { | |
| if (text.length <= maxLength) return text |
| // Hook | |
| export const useCounter = (initialValue = 0) => { | |
| const [count, setCount] = useState(initialValue) | |
| const increment = () => setCount(c => c + 1) | |
| const decrement = () => setCount(c => c - 1) | |
| const reset = () => setCount(initialValue) | |
| return { count, increment, decrement, reset } | |
| } |
| // Component | |
| interface ButtonProps { | |
| variant: 'primary' | 'secondary' | |
| disabled?: boolean | |
| onClick: () => void | |
| children: React.ReactNode | |
| } | |
| export const Button = ({ variant, disabled, onClick, children }: ButtonProps) => ( | |
| <button |
| <?php // /app/Http/Middleware/Cors.php | |
| namespace App\Http\Middleware; | |
| use Closure; | |
| class Cors { | |
| public function handle($request, Closure $next) | |
| { | |
| return $next($request) |