Created
February 11, 2026 18:45
-
-
Save claudiainbytes/e8d690600b57977c6ae26bd627bb0156 to your computer and use it in GitHub Desktop.
Testing con Context/State Management
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 ( | |
| <CartContext.Provider value={{ items, addItem, removeItem }}> | |
| {children} | |
| </CartContext.Provider> | |
| ) | |
| } | |
| // Test Helper | |
| const renderWithCart = (ui, options) => { | |
| return render(<CartProvider>{ui}</CartProvider>, options) | |
| } | |
| // Test | |
| describe('Cart Integration', () => { | |
| it('comparte estado entre componentes', async () => { | |
| const user = userEvent.setup() | |
| renderWithCart( | |
| <> | |
| <ProductList /> | |
| <CartSummary /> | |
| </> | |
| ) | |
| // Agregar desde ProductList | |
| const addButton = screen.getByRole('button', { name: /add product/i }) | |
| await user.click(addButton) | |
| // Verificar en CartSummary | |
| expect(screen.getByText(/1 item/i)).toBeInTheDocument() | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment