kybernus 3.0.1 → 3.2.0
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/README.md +1 -1
- package/dist/cli/commands/ecommerce.d.ts +3 -0
- package/dist/cli/commands/ecommerce.d.ts.map +1 -0
- package/dist/cli/commands/ecommerce.js +164 -0
- package/dist/cli/commands/ecommerce.js.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/templates/ecommerce/.env.example +10 -0
- package/templates/ecommerce/.github/workflows/ci.yml +102 -0
- package/templates/ecommerce/.github/workflows/deploy.yml +31 -0
- package/templates/ecommerce/.prettierrc +9 -0
- package/templates/ecommerce/Dockerfile +54 -0
- package/templates/ecommerce/README.md +295 -0
- package/templates/ecommerce/apps/api/.env.example +59 -0
- package/templates/ecommerce/apps/api/jest.config.ts +50 -0
- package/templates/ecommerce/apps/api/jest.integration.config.ts +45 -0
- package/templates/ecommerce/apps/api/package.json +59 -0
- package/templates/ecommerce/apps/api/prisma/migrations/20260306000137_init/migration.sql +184 -0
- package/templates/ecommerce/apps/api/prisma/migrations/migration_lock.toml +3 -0
- package/templates/ecommerce/apps/api/prisma/schema.prisma +181 -0
- package/templates/ecommerce/apps/api/prisma/seed.ts +159 -0
- package/templates/ecommerce/apps/api/src/__tests__/app.test.ts +39 -0
- package/templates/ecommerce/apps/api/src/__tests__/globalSetup.ts +34 -0
- package/templates/ecommerce/apps/api/src/__tests__/globalTeardown.ts +16 -0
- package/templates/ecommerce/apps/api/src/__tests__/setup.db.ts +18 -0
- package/templates/ecommerce/apps/api/src/__tests__/setup.env.ts +14 -0
- package/templates/ecommerce/apps/api/src/app.ts +133 -0
- package/templates/ecommerce/apps/api/src/application/admin/admin-user.service.ts +24 -0
- package/templates/ecommerce/apps/api/src/application/admin/dashboard.service.ts +102 -0
- package/templates/ecommerce/apps/api/src/application/auth/auth.service.ts +185 -0
- package/templates/ecommerce/apps/api/src/application/cart/cart.service.ts +151 -0
- package/templates/ecommerce/apps/api/src/application/cart/coupon.service.ts +51 -0
- package/templates/ecommerce/apps/api/src/application/catalog/catalog.service.ts +168 -0
- package/templates/ecommerce/apps/api/src/application/checkout/checkout.service.ts +114 -0
- package/templates/ecommerce/apps/api/src/application/orders/order.service.ts +93 -0
- package/templates/ecommerce/apps/api/src/application/ports/email.port.ts +3 -0
- package/templates/ecommerce/apps/api/src/application/ports/payment.port.ts +24 -0
- package/templates/ecommerce/apps/api/src/application/ports/shipping.port.ts +9 -0
- package/templates/ecommerce/apps/api/src/application/ports/storage.port.ts +3 -0
- package/templates/ecommerce/apps/api/src/application/ports/token-blacklist.port.ts +4 -0
- package/templates/ecommerce/apps/api/src/application/ports/token.port.ts +18 -0
- package/templates/ecommerce/apps/api/src/application/profile/profile.service.ts +76 -0
- package/templates/ecommerce/apps/api/src/domain/auth/user.entity.ts +109 -0
- package/templates/ecommerce/apps/api/src/domain/auth/user.repository.ts +11 -0
- package/templates/ecommerce/apps/api/src/domain/cart/cart.entity.ts +136 -0
- package/templates/ecommerce/apps/api/src/domain/cart/cart.repository.ts +8 -0
- package/templates/ecommerce/apps/api/src/domain/cart/coupon.entity.ts +58 -0
- package/templates/ecommerce/apps/api/src/domain/cart/coupon.repository.ts +10 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/category.entity.ts +51 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/category.repository.ts +10 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/product.entity.ts +130 -0
- package/templates/ecommerce/apps/api/src/domain/catalog/product.repository.ts +28 -0
- package/templates/ecommerce/apps/api/src/domain/checkout/order.entity.ts +121 -0
- package/templates/ecommerce/apps/api/src/domain/checkout/order.repository.ts +11 -0
- package/templates/ecommerce/apps/api/src/domain/shared/AppError.ts +12 -0
- package/templates/ecommerce/apps/api/src/infrastructure/cache/redis.ts +16 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/admin.registry.ts +13 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/auth.registry.ts +34 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/cart.registry.ts +49 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/catalog.registry.ts +24 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/checkout.registry.ts +47 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/orders.registry.ts +6 -0
- package/templates/ecommerce/apps/api/src/infrastructure/config/registry/profile.registry.ts +4 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/cart.memory.repository.ts +33 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/category.memory.repository.ts +41 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/coupon.memory.repository.ts +55 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/order.memory.repository.ts +75 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/product.memory.repository.ts +100 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/in-memory/user.memory.repository.ts +54 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/auth/user.prisma.repository.ts +83 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/catalog/category.prisma.repository.ts +69 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/catalog/product.prisma.repository.ts +185 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma/checkout/order.prisma.repository.ts +149 -0
- package/templates/ecommerce/apps/api/src/infrastructure/persistence/prisma-client.ts +17 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/email.registry.ts +18 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/ethereal.email.service.ts +38 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/noop.email.service.ts +12 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/email/smtp.email.service.ts +36 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/payment/stripe-webhook.handler.ts +83 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/payment/stripe.adapter.ts +39 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/shipping/mock.shipping.service.ts +17 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/in-memory.storage.service.ts +11 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/local-disk.storage.service.ts +27 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/s3.storage.service.ts +52 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/storage/storage.registry.ts +19 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/redis.token.blacklist.ts +23 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/token.blacklist.ts +30 -0
- package/templates/ecommerce/apps/api/src/infrastructure/services/token/token.service.ts +136 -0
- package/templates/ecommerce/apps/api/src/modules/admin/__tests__/admin.routes.integration.test.ts +250 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.controller.ts +116 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.routes.ts +21 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/admin/admin.user.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.logout.redis.test.ts +104 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.routes.integration.test.ts +211 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/auth.service.unit.test.ts +260 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/email.service.unit.test.ts +94 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/token.blacklist.redis.test.ts +65 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/user.entity.unit.test.ts +79 -0
- package/templates/ecommerce/apps/api/src/modules/auth/__tests__/user.prisma.repository.test.ts +138 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.controller.ts +148 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/auth/auth.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/redis.token.blacklist.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/token.blacklist.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/auth/token.service.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/auth/user.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.entity.unit.test.ts +144 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.routes.integration.test.ts +242 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/cart.service.unit.test.ts +151 -0
- package/templates/ecommerce/apps/api/src/modules/cart/__tests__/coupon.admin.integration.test.ts +136 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.controller.ts +94 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/cart/cart.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/cart/coupon.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/cart/shipping.service.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/catalog.routes.integration.test.ts +275 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/catalog.service.unit.test.ts +223 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/product.image.integration.test.ts +130 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/__tests__/product.prisma.repository.test.ts +174 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.controller.ts +176 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.routes.ts +38 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/catalog.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/category.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/catalog/product.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/checkout.routes.integration.test.ts +163 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/checkout.service.unit.test.ts +191 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/__tests__/order.prisma.repository.test.ts +150 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.controller.ts +59 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.routes.ts +18 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/checkout.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.entity.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.prisma.repository.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/order.repository.ts +2 -0
- package/templates/ecommerce/apps/api/src/modules/checkout/tax.service.ts +9 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.entity.unit.test.ts +68 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.routes.integration.test.ts +254 -0
- package/templates/ecommerce/apps/api/src/modules/orders/__tests__/order.service.email.unit.test.ts +142 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.controller.ts +96 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/modules/orders/order.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/__tests__/stripe-webhook.unit.test.ts +330 -0
- package/templates/ecommerce/apps/api/src/modules/payment/__tests__/stripe.adapter.unit.test.ts +84 -0
- package/templates/ecommerce/apps/api/src/modules/payment/adapters/stripe.adapter.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/payment.port.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/payment/stripe-webhook.handler.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/profile/__tests__/profile.routes.integration.test.ts +180 -0
- package/templates/ecommerce/apps/api/src/modules/profile/__tests__/profile.service.unit.test.ts +187 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.controller.ts +92 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.routes.ts +14 -0
- package/templates/ecommerce/apps/api/src/modules/profile/profile.service.ts +1 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/authenticate.ts +37 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/authorize.ts +23 -0
- package/templates/ecommerce/apps/api/src/presentation/middlewares/errorHandler.ts +48 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/admin/admin.controller.ts +116 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/admin/admin.routes.ts +21 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/auth/auth.controller.ts +147 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/auth/auth.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/cart/cart.controller.ts +94 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/cart/cart.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/catalog/catalog.controller.ts +176 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/catalog/catalog.routes.ts +38 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/checkout/checkout.controller.ts +59 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/checkout/checkout.routes.ts +18 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/orders/order.controller.ts +96 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/orders/order.routes.ts +17 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/profile/profile.controller.ts +92 -0
- package/templates/ecommerce/apps/api/src/presentation/modules/profile/profile.routes.ts +14 -0
- package/templates/ecommerce/apps/api/src/presentation/validators/uuidParam.ts +20 -0
- package/templates/ecommerce/apps/api/src/server.ts +47 -0
- package/templates/ecommerce/apps/api/src/shared/__tests__/uuid.validation.test.ts +111 -0
- package/templates/ecommerce/apps/api/src/shared/errors/AppError.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/EtherealEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/IEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/NoopEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/SmtpEmailService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/__tests__/ethereal.email.integration.test.ts +32 -0
- package/templates/ecommerce/apps/api/src/shared/infra/email/email.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/prisma.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/redis.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/IStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/InMemoryStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/LocalDiskStorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/S3StorageService.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/__tests__/s3.storage.unit.test.ts +73 -0
- package/templates/ecommerce/apps/api/src/shared/infra/storage/storage.registry.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/authenticate.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/authorize.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/middlewares/errorHandler.ts +1 -0
- package/templates/ecommerce/apps/api/src/shared/validators/uuidParam.ts +1 -0
- package/templates/ecommerce/apps/api/tsconfig.json +15 -0
- package/templates/ecommerce/apps/web/.env.example +8 -0
- package/templates/ecommerce/apps/web/index.html +19 -0
- package/templates/ecommerce/apps/web/jest.config.ts +45 -0
- package/templates/ecommerce/apps/web/package.json +38 -0
- package/templates/ecommerce/apps/web/src/App.tsx +133 -0
- package/templates/ecommerce/apps/web/src/__mocks__/fileMock.ts +1 -0
- package/templates/ecommerce/apps/web/src/__mocks__/styleMock.ts +1 -0
- package/templates/ecommerce/apps/web/src/index.css +159 -0
- package/templates/ecommerce/apps/web/src/main.tsx +13 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/CouponsAdminPage.test.tsx +134 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/DashboardPage.test.tsx +65 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/OrdersAdminPage.test.tsx +79 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/ProductsAdminPage.test.tsx +84 -0
- package/templates/ecommerce/apps/web/src/modules/admin/__tests__/UsersAdminPage.test.tsx +85 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/CouponsAdminPage.tsx +179 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/DashboardPage.tsx +58 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/OrdersAdminPage.tsx +178 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/ProductsAdminPage.tsx +444 -0
- package/templates/ecommerce/apps/web/src/modules/admin/pages/UsersAdminPage.tsx +87 -0
- package/templates/ecommerce/apps/web/src/modules/auth/LoginForm.tsx +91 -0
- package/templates/ecommerce/apps/web/src/modules/auth/RegisterForm.tsx +109 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/ForgotPasswordPage.test.tsx +42 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/LoginForm.test.tsx +76 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/RegisterForm.test.tsx +62 -0
- package/templates/ecommerce/apps/web/src/modules/auth/__tests__/ResetPasswordPage.test.tsx +66 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/ForgotPasswordPage.tsx +100 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/LoginPage.tsx +39 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/RegisterPage.tsx +39 -0
- package/templates/ecommerce/apps/web/src/modules/auth/pages/ResetPasswordPage.tsx +110 -0
- package/templates/ecommerce/apps/web/src/modules/auth/useAuthStore.ts +141 -0
- package/templates/ecommerce/apps/web/src/modules/cart/__tests__/CartPage.test.tsx +111 -0
- package/templates/ecommerce/apps/web/src/modules/cart/pages/CartPage.tsx +313 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/__tests__/ProductCard.test.tsx +59 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/__tests__/ProductFilters.test.tsx +56 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/components/ProductCard.tsx +78 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/components/ProductFilters.tsx +104 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/pages/ProductDetailPage.tsx +179 -0
- package/templates/ecommerce/apps/web/src/modules/catalog/pages/ProductListPage.tsx +100 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/__tests__/CheckoutPage.test.tsx +159 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/__tests__/StripePaymentForm.test.tsx +79 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/components/StripePaymentForm.tsx +55 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/hooks/useCheckout.ts +56 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/pages/CheckoutPage.tsx +344 -0
- package/templates/ecommerce/apps/web/src/modules/checkout/pages/CheckoutSuccessPage.tsx +12 -0
- package/templates/ecommerce/apps/web/src/modules/legal/pages/PrivacyPolicyPage.tsx +207 -0
- package/templates/ecommerce/apps/web/src/modules/legal/pages/TermsOfServicePage.tsx +175 -0
- package/templates/ecommerce/apps/web/src/modules/orders/__tests__/OrderDetailPage.test.tsx +75 -0
- package/templates/ecommerce/apps/web/src/modules/orders/__tests__/OrderHistoryPage.test.tsx +87 -0
- package/templates/ecommerce/apps/web/src/modules/orders/pages/OrderDetailPage.tsx +73 -0
- package/templates/ecommerce/apps/web/src/modules/orders/pages/OrderHistoryPage.tsx +97 -0
- package/templates/ecommerce/apps/web/src/modules/profile/__tests__/ProfilePage.test.tsx +150 -0
- package/templates/ecommerce/apps/web/src/modules/profile/pages/ProfilePage.tsx +275 -0
- package/templates/ecommerce/apps/web/src/setupTests.ts +10 -0
- package/templates/ecommerce/apps/web/src/shared/components/CookieConsent.tsx +108 -0
- package/templates/ecommerce/apps/web/src/shared/components/ErrorBoundary.tsx +112 -0
- package/templates/ecommerce/apps/web/src/shared/components/Layout.tsx +143 -0
- package/templates/ecommerce/apps/web/src/shared/components/ProtectedRoute.tsx +21 -0
- package/templates/ecommerce/apps/web/src/shared/config/siteConfig.ts +57 -0
- package/templates/ecommerce/apps/web/src/shared/hooks/usePageTitle.ts +16 -0
- package/templates/ecommerce/apps/web/src/shared/lib/apiFetch.ts +16 -0
- package/templates/ecommerce/apps/web/src/shared/pages/NotFoundPage.tsx +42 -0
- package/templates/ecommerce/apps/web/src/shared/theme/ThemeProvider.tsx +45 -0
- package/templates/ecommerce/apps/web/src/shared/theme/__tests__/ThemeProvider.test.tsx +78 -0
- package/templates/ecommerce/apps/web/src/shared/theme/createTheme.ts +58 -0
- package/templates/ecommerce/apps/web/src/shared/theme/tokens.ts +81 -0
- package/templates/ecommerce/apps/web/src/vite-env.d.ts +1 -0
- package/templates/ecommerce/apps/web/tsconfig.jest.json +12 -0
- package/templates/ecommerce/apps/web/tsconfig.json +25 -0
- package/templates/ecommerce/apps/web/tsconfig.node.json +11 -0
- package/templates/ecommerce/apps/web/vite.config.ts +30 -0
- package/templates/ecommerce/docker-compose.yml +85 -0
- package/templates/ecommerce/package-lock.json +11255 -0
- package/templates/ecommerce/package.json +27 -0
- package/templates/ecommerce/packages/shared-types/package.json +13 -0
- package/templates/ecommerce/packages/shared-types/src/index.ts +3 -0
- package/templates/ecommerce/packages/shared-types/src/theme.ts +44 -0
- package/templates/ecommerce/packages/shared-types/tsconfig.json +11 -0
- package/templates/ecommerce/scripts/customize.sh +201 -0
- package/templates/ecommerce/tsconfig.json +14 -0
- package/templates/java-spring/clean/.gitignore.hbs +72 -0
- package/templates/java-spring/clean/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/application/usecase/PaymentUseCase.java.hbs +21 -17
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/persistence/entity/UserEntity.java.hbs +52 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/persistence/repository/JpaUserRepository.java.hbs +12 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/security/JwtAuthenticationFilter.java.hbs +64 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/security/SecurityConfig.java.hbs +36 -0
- package/templates/java-spring/clean/src/main/java/{{packagePath}}/infrastructure/stripe/StripeGateway.java.hbs +63 -0
- package/templates/java-spring/clean/src/main/resources/application.properties.hbs +6 -7
- package/templates/java-spring/hexagonal/.gitignore.hbs +72 -0
- package/templates/java-spring/hexagonal/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/adapters/outbound/security/JwtFilter.java.hbs +71 -0
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/adapters/outbound/security/SecurityConfig.java.hbs +35 -0
- package/templates/java-spring/hexagonal/src/main/java/{{packagePath}}/core/service/PaymentService.java.hbs +3 -3
- package/templates/java-spring/hexagonal/src/main/resources/application.properties.hbs +4 -4
- package/templates/java-spring/mvc/.gitignore.hbs +72 -0
- package/templates/java-spring/mvc/docker-compose.yml.hbs +6 -3
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/config/SecurityConfig.java.hbs +13 -12
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/AuthController.java.hbs +9 -8
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/controller/PaymentsController.java.hbs +5 -6
- package/templates/java-spring/mvc/src/main/java/{{packagePath}}/service/StripeService.java.hbs +3 -3
- package/templates/java-spring/mvc/src/main/resources/application.yml.hbs +29 -26
- package/templates/nestjs/clean/.gitignore.hbs +42 -0
- package/templates/nestjs/clean/Dockerfile.hbs +6 -3
- package/templates/nestjs/clean/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/clean/src/app.module.ts.hbs +2 -1
- package/templates/nestjs/clean/src/application/payment.service.ts.hbs +72 -72
- package/templates/nestjs/clean/src/domain/entities/user.entity.ts.hbs +2 -2
- package/templates/nestjs/clean/src/domain/repositories/user.repository.ts.hbs +2 -2
- package/templates/nestjs/clean/src/infrastructure/database/repositories/prisma.user.repository.ts.hbs +18 -18
- package/templates/nestjs/clean/src/infrastructure/http/health.controller.ts.hbs +9 -0
- package/templates/nestjs/clean/src/main.ts.hbs +1 -4
- package/templates/nestjs/clean/src/payment.module.ts.hbs +12 -12
- package/templates/nestjs/hexagonal/.gitignore.hbs +42 -0
- package/templates/nestjs/hexagonal/Dockerfile.hbs +6 -3
- package/templates/nestjs/hexagonal/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/hexagonal/src/adapters/inbound/health.controller.ts.hbs +9 -0
- package/templates/nestjs/hexagonal/src/app.module.ts.hbs +2 -1
- package/templates/nestjs/hexagonal/src/core/domain/user.entity.ts.hbs +6 -6
- package/templates/nestjs/hexagonal/src/core/ports/ports.ts.hbs +4 -4
- package/templates/nestjs/hexagonal/src/main.ts.hbs +1 -4
- package/templates/nestjs/mvc/.gitignore.hbs +42 -0
- package/templates/nestjs/mvc/Dockerfile.hbs +6 -3
- package/templates/nestjs/mvc/docker-compose.yml.hbs +1 -11
- package/templates/nestjs/mvc/src/auth/auth.controller.ts.hbs +11 -1
- package/templates/nestjs/mvc/src/auth/auth.service.ts.hbs +3 -1
- package/templates/nestjs/mvc/src/controllers/health.controller.ts.hbs +6 -6
- package/templates/nestjs/mvc/src/main.ts.hbs +1 -4
- package/templates/nestjs/mvc/src/models/create-item.dto.ts.hbs +5 -2
- package/templates/nestjs/mvc/src/prisma/prisma.service.ts.hbs +1 -0
- package/templates/nextjs/mvc/.gitignore.hbs +42 -0
- package/templates/nextjs/mvc/Dockerfile.hbs +23 -8
- package/templates/nextjs/mvc/docker-compose.yml.hbs +1 -1
- package/templates/nodejs-express/clean/.gitignore.hbs +42 -0
- package/templates/nodejs-express/clean/Dockerfile.hbs +6 -1
- package/templates/nodejs-express/clean/docker-compose.yml.hbs +2 -2
- package/templates/nodejs-express/clean/package.json.hbs +69 -69
- package/templates/nodejs-express/clean/src/config.ts.hbs +11 -0
- package/templates/nodejs-express/clean/src/domain/entities/User.ts.hbs +46 -8
- package/templates/nodejs-express/hexagonal/.gitignore.hbs +42 -0
- package/templates/nodejs-express/hexagonal/Dockerfile.hbs +1 -1
- package/templates/nodejs-express/hexagonal/docker-compose.yml.hbs +2 -2
- package/templates/nodejs-express/hexagonal/package.json.hbs +69 -69
- package/templates/nodejs-express/hexagonal/src/adapters/inbound/http/PaymentController.ts.hbs +21 -38
- package/templates/nodejs-express/hexagonal/src/adapters/outbound/persistence/prisma.ts.hbs +2 -0
- package/templates/nodejs-express/hexagonal/src/config.ts.hbs +9 -0
- package/templates/nodejs-express/hexagonal/src/core/AuthService.ts.hbs +5 -5
- package/templates/nodejs-express/hexagonal/src/core/PaymentService.ts.hbs +7 -22
- package/templates/nodejs-express/hexagonal/src/core/domain/entities/User.ts.hbs +24 -4
- package/templates/nodejs-express/mvc/.gitignore.hbs +42 -0
- package/templates/nodejs-express/mvc/package.json.hbs +67 -67
- package/templates/python-fastapi/clean/.gitignore.hbs +76 -0
- package/templates/python-fastapi/clean/app/application/services/payment_service.py.hbs +3 -3
- package/templates/python-fastapi/clean/app/config.py.hbs +6 -7
- package/templates/python-fastapi/clean/app/domain/usecases/login_user.py.hbs +15 -0
- package/templates/python-fastapi/clean/app/infrastructure/http/auth_controller.py.hbs +40 -6
- package/templates/python-fastapi/clean/app/infrastructure/http/payment_controller.py.hbs +5 -4
- package/templates/python-fastapi/clean/app/infrastructure/security/jwt.py.hbs +23 -0
- package/templates/python-fastapi/clean/app/main.py.hbs +3 -0
- package/templates/python-fastapi/clean/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/clean/requirements.txt.hbs +3 -0
- package/templates/python-fastapi/hexagonal/.gitignore.hbs +76 -0
- package/templates/python-fastapi/hexagonal/app/adapters/inbound/http_adapter.py.hbs +6 -9
- package/templates/python-fastapi/hexagonal/app/adapters/inbound/payment_http_adapter.py.hbs +4 -3
- package/templates/python-fastapi/hexagonal/app/adapters/outbound/stripe_adapter.py.hbs +30 -19
- package/templates/python-fastapi/hexagonal/app/config.py.hbs +14 -4
- package/templates/python-fastapi/hexagonal/app/core/domain/user.py.hbs +3 -1
- package/templates/python-fastapi/hexagonal/app/core/payment_service.py.hbs +28 -18
- package/templates/python-fastapi/hexagonal/app/core/ports/__init__.py.hbs +3 -0
- package/templates/python-fastapi/hexagonal/app/core/ports/user_repository.py.hbs +15 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/database/session.py.hbs +7 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/database/user_repository.py.hbs +53 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/__init__.py.hbs +0 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/adapters.py.hbs +23 -0
- package/templates/python-fastapi/hexagonal/app/infrastructure/security/jwt.py.hbs +23 -0
- package/templates/python-fastapi/hexagonal/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/hexagonal/requirements.txt.hbs +4 -0
- package/templates/python-fastapi/mvc/.gitignore.hbs +76 -0
- package/templates/python-fastapi/mvc/app/controllers/payments.py.hbs +3 -17
- package/templates/python-fastapi/mvc/app/middleware/security.py.hbs +24 -3
- package/templates/python-fastapi/mvc/app/schemas/item.py.hbs +3 -1
- package/templates/python-fastapi/mvc/docker-compose.yml.hbs +5 -12
- package/templates/python-fastapi/mvc/requirements.txt.hbs +3 -1
- package/templates/nodejs-express/hexagonal/src/adapters/outbound/persistence/prisma.ts +0 -5
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { create } from 'zustand';
|
|
2
|
+
import { apiFetch } from '../../shared/lib/apiFetch';
|
|
3
|
+
|
|
4
|
+
export interface AuthUser {
|
|
5
|
+
id: string;
|
|
6
|
+
name: string;
|
|
7
|
+
email: string;
|
|
8
|
+
role: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface AuthState {
|
|
12
|
+
user: AuthUser | null;
|
|
13
|
+
accessToken: string | null;
|
|
14
|
+
login(email: string, password: string): Promise<void>;
|
|
15
|
+
register(name: string, email: string, password: string): Promise<void>;
|
|
16
|
+
logout(): Promise<void>;
|
|
17
|
+
init(): Promise<void>;
|
|
18
|
+
updateProfile(data: { name?: string; email?: string }): Promise<void>;
|
|
19
|
+
changePassword(data: { currentPassword: string; newPassword: string }): Promise<void>;
|
|
20
|
+
deleteAccount(password: string): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const useAuthStore = create<AuthState>((set) => ({
|
|
24
|
+
user: null,
|
|
25
|
+
// Access token is kept ONLY in memory — never persisted in localStorage.
|
|
26
|
+
// This prevents XSS attacks from reading the token via JavaScript.
|
|
27
|
+
// On page load, init() silently exchanges the httpOnly refresh-token cookie
|
|
28
|
+
// for a fresh access token via POST /api/auth/refresh.
|
|
29
|
+
accessToken: null,
|
|
30
|
+
|
|
31
|
+
async login(email, password) {
|
|
32
|
+
const res = await fetch('/api/auth/login', {
|
|
33
|
+
method: 'POST',
|
|
34
|
+
credentials: 'include', // send & receive httpOnly cookie
|
|
35
|
+
headers: { 'Content-Type': 'application/json' },
|
|
36
|
+
body: JSON.stringify({ email, password }),
|
|
37
|
+
});
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
const data = (await res.json()) as { message?: string };
|
|
40
|
+
throw new Error(data.message ?? 'Credenciais inválidas');
|
|
41
|
+
}
|
|
42
|
+
const data = (await res.json()) as { user: AuthUser; accessToken: string };
|
|
43
|
+
set({ user: data.user, accessToken: data.accessToken });
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
async register(name, email, password) {
|
|
47
|
+
const res = await fetch('/api/auth/register', {
|
|
48
|
+
method: 'POST',
|
|
49
|
+
credentials: 'include',
|
|
50
|
+
headers: { 'Content-Type': 'application/json' },
|
|
51
|
+
body: JSON.stringify({ name, email, password }),
|
|
52
|
+
});
|
|
53
|
+
if (!res.ok) {
|
|
54
|
+
const data = (await res.json()) as { message?: string };
|
|
55
|
+
throw new Error(data.message ?? 'Erro ao cadastrar');
|
|
56
|
+
}
|
|
57
|
+
const data = (await res.json()) as { user: AuthUser; accessToken: string };
|
|
58
|
+
set({ user: data.user, accessToken: data.accessToken });
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
async logout() {
|
|
62
|
+
const token = useAuthStore.getState().accessToken;
|
|
63
|
+
try {
|
|
64
|
+
await fetch('/api/auth/logout', {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
credentials: 'include',
|
|
67
|
+
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
|
68
|
+
});
|
|
69
|
+
} catch {
|
|
70
|
+
// ignore — clear local state regardless
|
|
71
|
+
}
|
|
72
|
+
set({ user: null, accessToken: null });
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
async init() {
|
|
76
|
+
// Silently try to obtain a fresh access token using the httpOnly refresh
|
|
77
|
+
// cookie. If the cookie is absent or expired the request returns 401 and
|
|
78
|
+
// we leave the user unauthenticated without throwing.
|
|
79
|
+
try {
|
|
80
|
+
const res = await fetch('/api/auth/refresh', {
|
|
81
|
+
method: 'POST',
|
|
82
|
+
credentials: 'include',
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok) return;
|
|
85
|
+
const data = (await res.json()) as { accessToken: string };
|
|
86
|
+
// Fetch user profile with the new token
|
|
87
|
+
const meRes = await fetch('/api/auth/me', {
|
|
88
|
+
credentials: 'include',
|
|
89
|
+
headers: { Authorization: `Bearer ${data.accessToken}` },
|
|
90
|
+
});
|
|
91
|
+
if (!meRes.ok) return;
|
|
92
|
+
const user = (await meRes.json()) as AuthUser;
|
|
93
|
+
set({ user, accessToken: data.accessToken });
|
|
94
|
+
} catch {
|
|
95
|
+
// Network error or server unavailable — stay logged out
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
|
|
99
|
+
async updateProfile(data) {
|
|
100
|
+
const updated = await apiFetch<AuthUser>('/api/me', {
|
|
101
|
+
method: 'PATCH',
|
|
102
|
+
body: JSON.stringify(data),
|
|
103
|
+
});
|
|
104
|
+
set((s) => ({ user: s.user ? { ...s.user, ...updated } : s.user }));
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
async changePassword(data) {
|
|
108
|
+
const token = useAuthStore.getState().accessToken;
|
|
109
|
+
const res = await fetch('/api/me/password', {
|
|
110
|
+
method: 'PATCH',
|
|
111
|
+
credentials: 'include',
|
|
112
|
+
headers: {
|
|
113
|
+
'Content-Type': 'application/json',
|
|
114
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
115
|
+
},
|
|
116
|
+
body: JSON.stringify(data),
|
|
117
|
+
});
|
|
118
|
+
if (!res.ok) {
|
|
119
|
+
const body = (await res.json()) as { message?: string };
|
|
120
|
+
throw new Error(body.message ?? `HTTP ${res.status}`);
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
async deleteAccount(password) {
|
|
125
|
+
const token = useAuthStore.getState().accessToken;
|
|
126
|
+
const res = await fetch('/api/me', {
|
|
127
|
+
method: 'DELETE',
|
|
128
|
+
credentials: 'include',
|
|
129
|
+
headers: {
|
|
130
|
+
'Content-Type': 'application/json',
|
|
131
|
+
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
132
|
+
},
|
|
133
|
+
body: JSON.stringify({ password }),
|
|
134
|
+
});
|
|
135
|
+
if (!res.ok) {
|
|
136
|
+
const body = (await res.json()) as { message?: string };
|
|
137
|
+
throw new Error(body.message ?? `HTTP ${res.status}`);
|
|
138
|
+
}
|
|
139
|
+
set({ user: null, accessToken: null });
|
|
140
|
+
},
|
|
141
|
+
}));
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { render, screen, waitFor } from '@testing-library/react';
|
|
2
|
+
import { MemoryRouter } from 'react-router-dom';
|
|
3
|
+
import { CartPage } from '../pages/CartPage';
|
|
4
|
+
import { useAuthStore } from '../../auth/useAuthStore';
|
|
5
|
+
|
|
6
|
+
const mockFetch = jest.fn();
|
|
7
|
+
beforeAll(() => { global.fetch = mockFetch; });
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
mockFetch.mockClear();
|
|
10
|
+
// Set token in Zustand memory state (not localStorage — secure in-memory only)
|
|
11
|
+
useAuthStore.setState({ accessToken: 'test-token' });
|
|
12
|
+
});
|
|
13
|
+
afterAll(() => useAuthStore.setState({ accessToken: null, user: null }));
|
|
14
|
+
|
|
15
|
+
function renderPage() {
|
|
16
|
+
return render(
|
|
17
|
+
<MemoryRouter>
|
|
18
|
+
<CartPage />
|
|
19
|
+
</MemoryRouter>,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const cartWithItems = {
|
|
24
|
+
items: [
|
|
25
|
+
{ variantId: 'v1', productId: 'p1', name: 'Camiseta Preta', sku: 'CAM-P', price: 99.9, qty: 2, image: null },
|
|
26
|
+
{ variantId: 'v2', productId: 'p2', name: 'Calça Jeans', sku: 'CAL-J', price: 199.9, qty: 1, image: null },
|
|
27
|
+
],
|
|
28
|
+
subtotal: 399.7,
|
|
29
|
+
discount: 0,
|
|
30
|
+
shippingCost: 0,
|
|
31
|
+
tax: 0,
|
|
32
|
+
total: 399.7,
|
|
33
|
+
couponCode: null,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const emptyCart = {
|
|
37
|
+
items: [],
|
|
38
|
+
subtotal: 0,
|
|
39
|
+
discount: 0,
|
|
40
|
+
shippingCost: 0,
|
|
41
|
+
tax: 0,
|
|
42
|
+
total: 0,
|
|
43
|
+
couponCode: null,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
describe('CartPage', () => {
|
|
47
|
+
it('exibe itens do carrinho com nome, qtd e preço', async () => {
|
|
48
|
+
mockFetch.mockResolvedValue({
|
|
49
|
+
ok: true,
|
|
50
|
+
json: async () => cartWithItems,
|
|
51
|
+
});
|
|
52
|
+
renderPage();
|
|
53
|
+
await waitFor(() => expect(screen.getByText('Camiseta Preta')).toBeInTheDocument());
|
|
54
|
+
expect(screen.getByText('Calça Jeans')).toBeInTheDocument();
|
|
55
|
+
expect(screen.getByDisplayValue('2')).toBeInTheDocument();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('exibe total do carrinho', async () => {
|
|
59
|
+
mockFetch.mockResolvedValue({
|
|
60
|
+
ok: true,
|
|
61
|
+
json: async () => cartWithItems,
|
|
62
|
+
});
|
|
63
|
+
renderPage();
|
|
64
|
+
await waitFor(() => {
|
|
65
|
+
const elements = screen.getAllByText(/399/);
|
|
66
|
+
expect(elements.length).toBeGreaterThanOrEqual(1);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('exibe input de cupom e botão aplicar', async () => {
|
|
71
|
+
mockFetch.mockResolvedValue({
|
|
72
|
+
ok: true,
|
|
73
|
+
json: async () => cartWithItems,
|
|
74
|
+
});
|
|
75
|
+
renderPage();
|
|
76
|
+
await waitFor(() => expect(screen.getByPlaceholderText(/cupom|código/i)).toBeInTheDocument());
|
|
77
|
+
expect(screen.getByRole('button', { name: /aplicar/i })).toBeInTheDocument();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('exibe opções de frete', async () => {
|
|
81
|
+
mockFetch.mockResolvedValue({
|
|
82
|
+
ok: true,
|
|
83
|
+
json: async () => cartWithItems,
|
|
84
|
+
});
|
|
85
|
+
renderPage();
|
|
86
|
+
await waitFor(() => expect(screen.getByPlaceholderText(/cep/i)).toBeInTheDocument());
|
|
87
|
+
expect(screen.getByRole('button', { name: /calcular/i })).toBeInTheDocument();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('botão "Ir para Checkout" navega para /checkout', async () => {
|
|
91
|
+
mockFetch.mockResolvedValue({
|
|
92
|
+
ok: true,
|
|
93
|
+
json: async () => cartWithItems,
|
|
94
|
+
});
|
|
95
|
+
renderPage();
|
|
96
|
+
await waitFor(() =>
|
|
97
|
+
expect(screen.getByRole('link', { name: /ir para checkout|finalizar/i })).toBeInTheDocument(),
|
|
98
|
+
);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('exibe mensagem quando carrinho estiver vazio', async () => {
|
|
102
|
+
mockFetch.mockResolvedValue({
|
|
103
|
+
ok: true,
|
|
104
|
+
json: async () => emptyCart,
|
|
105
|
+
});
|
|
106
|
+
renderPage();
|
|
107
|
+
await waitFor(() =>
|
|
108
|
+
expect(screen.getByText(/carrinho.*vazio|vazio/i)).toBeInTheDocument(),
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { useEffect, useState } from 'react';
|
|
2
|
+
import { Link } from 'react-router-dom';
|
|
3
|
+
import { apiFetch } from '../../../shared/lib/apiFetch';
|
|
4
|
+
import { usePageTitle } from '../../../shared/hooks/usePageTitle';
|
|
5
|
+
|
|
6
|
+
interface CartItem {
|
|
7
|
+
variantId: string;
|
|
8
|
+
productId: string;
|
|
9
|
+
name: string;
|
|
10
|
+
sku: string;
|
|
11
|
+
price: number;
|
|
12
|
+
qty: number;
|
|
13
|
+
image: string | null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface ShippingOption {
|
|
17
|
+
id: string;
|
|
18
|
+
name: string;
|
|
19
|
+
price: number;
|
|
20
|
+
days: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
interface Cart {
|
|
24
|
+
items: CartItem[];
|
|
25
|
+
subtotal: number;
|
|
26
|
+
discount: number;
|
|
27
|
+
shippingCost: number;
|
|
28
|
+
tax: number;
|
|
29
|
+
total: number;
|
|
30
|
+
couponCode: string | null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function formatBRL(value: number) {
|
|
34
|
+
return value.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' });
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function CartPage() {
|
|
38
|
+
usePageTitle('Meu Carrinho');
|
|
39
|
+
const [cart, setCart] = useState<Cart | null>(null);
|
|
40
|
+
const [loading, setLoading] = useState(true);
|
|
41
|
+
const [error, setError] = useState<string | null>(null);
|
|
42
|
+
|
|
43
|
+
const [couponInput, setCouponInput] = useState('');
|
|
44
|
+
const [couponError, setCouponError] = useState<string | null>(null);
|
|
45
|
+
const [couponLoading, setCouponLoading] = useState(false);
|
|
46
|
+
|
|
47
|
+
const [cepInput, setCepInput] = useState('');
|
|
48
|
+
const [shippingOptions, setShippingOptions] = useState<ShippingOption[]>([]);
|
|
49
|
+
const [shippingError, setShippingError] = useState<string | null>(null);
|
|
50
|
+
const [shippingLoading, setShippingLoading] = useState(false);
|
|
51
|
+
|
|
52
|
+
useEffect(() => {
|
|
53
|
+
apiFetch<Cart>('/api/cart')
|
|
54
|
+
.then((data) => { setCart(data); setLoading(false); })
|
|
55
|
+
.catch(() => { setError('Erro ao carregar carrinho'); setLoading(false); });
|
|
56
|
+
}, []);
|
|
57
|
+
|
|
58
|
+
async function handleQtyChange(variantId: string, qty: number) {
|
|
59
|
+
try {
|
|
60
|
+
const updated = await apiFetch<Cart>(`/api/cart/items/${variantId}`, {
|
|
61
|
+
method: 'PATCH',
|
|
62
|
+
body: JSON.stringify({ qty }),
|
|
63
|
+
});
|
|
64
|
+
setCart(updated);
|
|
65
|
+
} catch {
|
|
66
|
+
setError('Erro ao atualizar quantidade');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function handleRemove(variantId: string) {
|
|
71
|
+
try {
|
|
72
|
+
const updated = await apiFetch<Cart>(`/api/cart/items/${variantId}`, {
|
|
73
|
+
method: 'PATCH',
|
|
74
|
+
body: JSON.stringify({ qty: 0 }),
|
|
75
|
+
});
|
|
76
|
+
setCart(updated);
|
|
77
|
+
} catch {
|
|
78
|
+
setError('Erro ao remover item');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async function handleApplyCoupon() {
|
|
83
|
+
if (!couponInput.trim()) return;
|
|
84
|
+
setCouponLoading(true);
|
|
85
|
+
setCouponError(null);
|
|
86
|
+
try {
|
|
87
|
+
const updated = await apiFetch<Cart>('/api/cart/coupon', {
|
|
88
|
+
method: 'POST',
|
|
89
|
+
body: JSON.stringify({ code: couponInput.trim() }),
|
|
90
|
+
});
|
|
91
|
+
setCart(updated);
|
|
92
|
+
setCouponInput('');
|
|
93
|
+
} catch (err) {
|
|
94
|
+
setCouponError(err instanceof Error ? err.message : 'Cupom inválido');
|
|
95
|
+
} finally {
|
|
96
|
+
setCouponLoading(false);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function handleCalculateShipping() {
|
|
101
|
+
if (!cepInput.trim()) return;
|
|
102
|
+
setShippingLoading(true);
|
|
103
|
+
setShippingError(null);
|
|
104
|
+
try {
|
|
105
|
+
const data = await apiFetch<{ options: ShippingOption[] }>(
|
|
106
|
+
`/api/cart/shipping?cep=${cepInput.replace(/\D/g, '')}`,
|
|
107
|
+
);
|
|
108
|
+
setShippingOptions(data.options);
|
|
109
|
+
} catch {
|
|
110
|
+
setShippingError('Erro ao calcular frete. Verifique o CEP informado.');
|
|
111
|
+
} finally {
|
|
112
|
+
setShippingLoading(false);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (loading) {
|
|
117
|
+
return <p style={{ marginTop: '2rem', color: '#6b7280' }}>Carregando...</p>;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (error) {
|
|
121
|
+
return <p role="alert" style={{ marginTop: '2rem', color: '#ef4444' }}>{error}</p>;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (!cart || cart.items.length === 0) {
|
|
125
|
+
return (
|
|
126
|
+
<main style={{ maxWidth: 680, margin: '4rem auto', textAlign: 'center' }}>
|
|
127
|
+
<h1 style={{ marginBottom: '1rem' }}>Carrinho</h1>
|
|
128
|
+
<p style={{ color: '#6b7280', marginBottom: '1.5rem' }}>Seu carrinho está vazio.</p>
|
|
129
|
+
<Link
|
|
130
|
+
to="/"
|
|
131
|
+
style={{
|
|
132
|
+
display: 'inline-block',
|
|
133
|
+
padding: '0.6rem 1.5rem',
|
|
134
|
+
background: 'var(--color-primary)',
|
|
135
|
+
color: '#fff',
|
|
136
|
+
borderRadius: 8,
|
|
137
|
+
textDecoration: 'none',
|
|
138
|
+
fontWeight: 600,
|
|
139
|
+
}}
|
|
140
|
+
>
|
|
141
|
+
Ver produtos
|
|
142
|
+
</Link>
|
|
143
|
+
</main>
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return (
|
|
148
|
+
<main style={{ maxWidth: 880, margin: '2rem auto', display: 'grid', gridTemplateColumns: '1fr 320px', gap: '2rem', alignItems: 'start' }}>
|
|
149
|
+
{/* Items list */}
|
|
150
|
+
<section>
|
|
151
|
+
<h1 style={{ marginBottom: '1.5rem' }}>Carrinho</h1>
|
|
152
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.75rem' }}>
|
|
153
|
+
{cart.items.map((item) => (
|
|
154
|
+
<div
|
|
155
|
+
key={item.variantId}
|
|
156
|
+
style={{
|
|
157
|
+
display: 'grid',
|
|
158
|
+
gridTemplateColumns: '56px 1fr auto',
|
|
159
|
+
gap: '1rem',
|
|
160
|
+
alignItems: 'center',
|
|
161
|
+
background: '#fff',
|
|
162
|
+
border: '1px solid #e5e7eb',
|
|
163
|
+
borderRadius: 10,
|
|
164
|
+
padding: '0.875rem 1rem',
|
|
165
|
+
}}
|
|
166
|
+
>
|
|
167
|
+
<div
|
|
168
|
+
style={{
|
|
169
|
+
width: 56,
|
|
170
|
+
height: 56,
|
|
171
|
+
background: '#f3f4f6',
|
|
172
|
+
borderRadius: 6,
|
|
173
|
+
overflow: 'hidden',
|
|
174
|
+
flexShrink: 0,
|
|
175
|
+
}}
|
|
176
|
+
>
|
|
177
|
+
{item.image && (
|
|
178
|
+
<img src={item.image} alt={item.name} style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
|
|
179
|
+
)}
|
|
180
|
+
</div>
|
|
181
|
+
<div>
|
|
182
|
+
<p style={{ fontWeight: 600, fontSize: '0.9rem', marginBottom: '0.15rem' }}>{item.name}</p>
|
|
183
|
+
<p style={{ fontSize: '0.78rem', color: '#6b7280', marginBottom: '0.5rem' }}>SKU: {item.sku}</p>
|
|
184
|
+
<p style={{ fontWeight: 700, color: 'var(--color-primary)' }}>{formatBRL(item.price)}</p>
|
|
185
|
+
</div>
|
|
186
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.4rem', alignItems: 'flex-end' }}>
|
|
187
|
+
<input
|
|
188
|
+
type="number"
|
|
189
|
+
min={1}
|
|
190
|
+
value={item.qty}
|
|
191
|
+
onChange={(e) => handleQtyChange(item.variantId, Number(e.target.value))}
|
|
192
|
+
style={{ width: 60, padding: '0.3rem 0.5rem', border: '1px solid #d1d5db', borderRadius: 6, textAlign: 'center' }}
|
|
193
|
+
/>
|
|
194
|
+
<button
|
|
195
|
+
onClick={() => handleRemove(item.variantId)}
|
|
196
|
+
style={{ background: 'none', border: 'none', color: '#ef4444', cursor: 'pointer', fontSize: '0.8rem' }}
|
|
197
|
+
>
|
|
198
|
+
Remover
|
|
199
|
+
</button>
|
|
200
|
+
</div>
|
|
201
|
+
</div>
|
|
202
|
+
))}
|
|
203
|
+
</div>
|
|
204
|
+
</section>
|
|
205
|
+
|
|
206
|
+
{/* Summary sidebar */}
|
|
207
|
+
<aside style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
|
|
208
|
+
{/* Coupon */}
|
|
209
|
+
<div style={{ background: '#fff', border: '1px solid #e5e7eb', borderRadius: 10, padding: '1rem' }}>
|
|
210
|
+
<p style={{ fontWeight: 600, marginBottom: '0.6rem', fontSize: '0.875rem' }}>Cupom de desconto</p>
|
|
211
|
+
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
|
212
|
+
<input
|
|
213
|
+
type="text"
|
|
214
|
+
placeholder="Código do cupom"
|
|
215
|
+
value={couponInput}
|
|
216
|
+
onChange={(e) => setCouponInput(e.target.value)}
|
|
217
|
+
style={{ flex: 1, padding: '0.4rem 0.6rem', border: '1px solid #d1d5db', borderRadius: 6, fontSize: '0.875rem' }}
|
|
218
|
+
/>
|
|
219
|
+
<button
|
|
220
|
+
onClick={handleApplyCoupon}
|
|
221
|
+
disabled={couponLoading}
|
|
222
|
+
style={{ padding: '0.4rem 0.9rem', background: 'var(--color-primary)', color: '#fff', border: 'none', borderRadius: 6, cursor: 'pointer', fontSize: '0.875rem', fontWeight: 600 }}
|
|
223
|
+
>
|
|
224
|
+
{couponLoading ? '...' : 'Aplicar'}
|
|
225
|
+
</button>
|
|
226
|
+
</div>
|
|
227
|
+
{couponError && <p style={{ color: '#ef4444', fontSize: '0.8rem', marginTop: '0.4rem' }}>{couponError}</p>}
|
|
228
|
+
{cart.couponCode && (
|
|
229
|
+
<p style={{ color: '#10b981', fontSize: '0.8rem', marginTop: '0.4rem' }}>
|
|
230
|
+
Cupom <strong>{cart.couponCode}</strong> aplicado
|
|
231
|
+
</p>
|
|
232
|
+
)}
|
|
233
|
+
</div>
|
|
234
|
+
|
|
235
|
+
{/* Shipping calculator */}
|
|
236
|
+
<div style={{ background: '#fff', border: '1px solid #e5e7eb', borderRadius: 10, padding: '1rem' }}>
|
|
237
|
+
<p style={{ fontWeight: 600, marginBottom: '0.6rem', fontSize: '0.875rem' }}>Calcular frete</p>
|
|
238
|
+
<div style={{ display: 'flex', gap: '0.5rem' }}>
|
|
239
|
+
<input
|
|
240
|
+
type="text"
|
|
241
|
+
placeholder="CEP (somente números)"
|
|
242
|
+
value={cepInput}
|
|
243
|
+
onChange={(e) => setCepInput(e.target.value)}
|
|
244
|
+
maxLength={9}
|
|
245
|
+
style={{ flex: 1, padding: '0.4rem 0.6rem', border: '1px solid #d1d5db', borderRadius: 6, fontSize: '0.875rem' }}
|
|
246
|
+
/>
|
|
247
|
+
<button
|
|
248
|
+
onClick={handleCalculateShipping}
|
|
249
|
+
disabled={shippingLoading}
|
|
250
|
+
style={{ padding: '0.4rem 0.9rem', background: '#6366f1', color: '#fff', border: 'none', borderRadius: 6, cursor: 'pointer', fontSize: '0.875rem', fontWeight: 600 }}
|
|
251
|
+
>
|
|
252
|
+
{shippingLoading ? '...' : 'Calcular'}
|
|
253
|
+
</button>
|
|
254
|
+
</div>
|
|
255
|
+
{shippingError && <p style={{ color: '#ef4444', fontSize: '0.8rem', marginTop: '0.4rem' }}>{shippingError}</p>}
|
|
256
|
+
{shippingOptions.length > 0 && (
|
|
257
|
+
<ul style={{ marginTop: '0.6rem', listStyle: 'none', display: 'flex', flexDirection: 'column', gap: '0.35rem' }}>
|
|
258
|
+
{shippingOptions.map((opt) => (
|
|
259
|
+
<li key={opt.id} style={{ fontSize: '0.82rem', display: 'flex', justifyContent: 'space-between' }}>
|
|
260
|
+
<span>{opt.name} ({opt.days} dias)</span>
|
|
261
|
+
<span style={{ fontWeight: 600 }}>{formatBRL(opt.price)}</span>
|
|
262
|
+
</li>
|
|
263
|
+
))}
|
|
264
|
+
</ul>
|
|
265
|
+
)}
|
|
266
|
+
</div>
|
|
267
|
+
|
|
268
|
+
{/* Order summary */}
|
|
269
|
+
<div style={{ background: '#fff', border: '1px solid #e5e7eb', borderRadius: 10, padding: '1rem' }}>
|
|
270
|
+
<p style={{ fontWeight: 700, fontSize: '1rem', marginBottom: '0.75rem' }}>Resumo do pedido</p>
|
|
271
|
+
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.4rem', fontSize: '0.875rem' }}>
|
|
272
|
+
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
|
273
|
+
<span>Subtotal</span>
|
|
274
|
+
<span>{formatBRL(cart.subtotal)}</span>
|
|
275
|
+
</div>
|
|
276
|
+
{cart.discount > 0 && (
|
|
277
|
+
<div style={{ display: 'flex', justifyContent: 'space-between', color: '#10b981' }}>
|
|
278
|
+
<span>Desconto</span>
|
|
279
|
+
<span>- {formatBRL(cart.discount)}</span>
|
|
280
|
+
</div>
|
|
281
|
+
)}
|
|
282
|
+
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
|
|
283
|
+
<span>Frete</span>
|
|
284
|
+
<span>{cart.shippingCost > 0 ? formatBRL(cart.shippingCost) : 'A calcular'}</span>
|
|
285
|
+
</div>
|
|
286
|
+
<hr style={{ margin: '0.4rem 0', border: 'none', borderTop: '1px solid #e5e7eb' }} />
|
|
287
|
+
<div style={{ display: 'flex', justifyContent: 'space-between', fontWeight: 700, fontSize: '1rem' }}>
|
|
288
|
+
<span>Total</span>
|
|
289
|
+
<span style={{ color: 'var(--color-primary)' }}>{formatBRL(cart.total)}</span>
|
|
290
|
+
</div>
|
|
291
|
+
</div>
|
|
292
|
+
<Link
|
|
293
|
+
to="/checkout"
|
|
294
|
+
style={{
|
|
295
|
+
display: 'block',
|
|
296
|
+
marginTop: '1rem',
|
|
297
|
+
padding: '0.75rem',
|
|
298
|
+
background: 'var(--color-primary)',
|
|
299
|
+
color: '#fff',
|
|
300
|
+
borderRadius: 8,
|
|
301
|
+
textDecoration: 'none',
|
|
302
|
+
textAlign: 'center',
|
|
303
|
+
fontWeight: 700,
|
|
304
|
+
fontSize: '0.9rem',
|
|
305
|
+
}}
|
|
306
|
+
>
|
|
307
|
+
Ir para Checkout
|
|
308
|
+
</Link>
|
|
309
|
+
</div>
|
|
310
|
+
</aside>
|
|
311
|
+
</main>
|
|
312
|
+
);
|
|
313
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { render, screen } from '@testing-library/react';
|
|
2
|
+
import { MemoryRouter } from 'react-router-dom';
|
|
3
|
+
import { ProductCard } from '../components/ProductCard';
|
|
4
|
+
|
|
5
|
+
const mockProduct = {
|
|
6
|
+
id: 'prod-1',
|
|
7
|
+
name: 'Camiseta Básica',
|
|
8
|
+
slug: 'camiseta-basica',
|
|
9
|
+
price: 49.9,
|
|
10
|
+
images: ['https://via.placeholder.com/300'],
|
|
11
|
+
variants: [
|
|
12
|
+
{ id: 'var-1', sku: 'CAM-P', size: 'P', color: 'Branco', stock: 5 },
|
|
13
|
+
],
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const outOfStockProduct = {
|
|
17
|
+
...mockProduct,
|
|
18
|
+
id: 'prod-2',
|
|
19
|
+
slug: 'camiseta-sem-estoque',
|
|
20
|
+
variants: [{ id: 'var-2', sku: 'CAM-P2', size: 'P', color: 'Preto', stock: 0 }],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function renderCard(product = mockProduct) {
|
|
24
|
+
return render(
|
|
25
|
+
<MemoryRouter>
|
|
26
|
+
<ProductCard product={product} />
|
|
27
|
+
</MemoryRouter>,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
describe('ProductCard', () => {
|
|
32
|
+
it('deve exibir nome, preço formatado em BRL e imagem', () => {
|
|
33
|
+
renderCard();
|
|
34
|
+
|
|
35
|
+
expect(screen.getByText('Camiseta Básica')).toBeInTheDocument();
|
|
36
|
+
// Price should be formatted as BRL currency
|
|
37
|
+
expect(screen.getByText(/R\$\s*49[,.]?90?/)).toBeInTheDocument();
|
|
38
|
+
expect(screen.getByRole('img', { name: /camiseta básica/i })).toBeInTheDocument();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('deve exibir badge "Sem Estoque" quando todo o estoque é 0', () => {
|
|
42
|
+
renderCard(outOfStockProduct);
|
|
43
|
+
|
|
44
|
+
expect(screen.getByText(/sem estoque/i)).toBeInTheDocument();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('não deve exibir badge "Sem Estoque" quando há variação disponível', () => {
|
|
48
|
+
renderCard();
|
|
49
|
+
|
|
50
|
+
expect(screen.queryByText(/sem estoque/i)).not.toBeInTheDocument();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('deve ter link que navega para página de detalhe (/products/:slug)', () => {
|
|
54
|
+
renderCard();
|
|
55
|
+
|
|
56
|
+
const link = screen.getByRole('link');
|
|
57
|
+
expect(link).toHaveAttribute('href', '/products/camiseta-basica');
|
|
58
|
+
});
|
|
59
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { render, screen, act } from '@testing-library/react';
|
|
2
|
+
import userEvent from '@testing-library/user-event';
|
|
3
|
+
import { ProductFilters } from '../components/ProductFilters';
|
|
4
|
+
|
|
5
|
+
const categories = [
|
|
6
|
+
{ id: 'cat-1', name: 'Camisetas', slug: 'camisetas' },
|
|
7
|
+
{ id: 'cat-2', name: 'Calças', slug: 'calcas' },
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
describe('ProductFilters', () => {
|
|
11
|
+
it('deve chamar onFilterChange ao selecionar categoria', async () => {
|
|
12
|
+
const user = userEvent.setup();
|
|
13
|
+
const onFilterChange = jest.fn();
|
|
14
|
+
render(<ProductFilters categories={categories} onFilterChange={onFilterChange} />);
|
|
15
|
+
|
|
16
|
+
await user.selectOptions(screen.getByRole('combobox', { name: /categoria/i }), 'camisetas');
|
|
17
|
+
|
|
18
|
+
expect(onFilterChange).toHaveBeenCalledWith(
|
|
19
|
+
expect.objectContaining({ categorySlug: 'camisetas' }),
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('deve debounce a busca por texto (não dispara imediatamente)', async () => {
|
|
24
|
+
jest.useFakeTimers();
|
|
25
|
+
const user = userEvent.setup({ advanceTimers: jest.advanceTimersByTime });
|
|
26
|
+
const onFilterChange = jest.fn();
|
|
27
|
+
render(<ProductFilters categories={categories} onFilterChange={onFilterChange} />);
|
|
28
|
+
|
|
29
|
+
await user.type(screen.getByRole('textbox', { name: /buscar/i }), 'cam');
|
|
30
|
+
|
|
31
|
+
// Should NOT have been called yet (debounce pending)
|
|
32
|
+
expect(onFilterChange).not.toHaveBeenCalled();
|
|
33
|
+
|
|
34
|
+
act(() => { jest.advanceTimersByTime(400); });
|
|
35
|
+
|
|
36
|
+
expect(onFilterChange).toHaveBeenCalledWith(
|
|
37
|
+
expect.objectContaining({ q: 'cam' }),
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
jest.useRealTimers();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('deve limpar filtros ao clicar em "Limpar"', async () => {
|
|
44
|
+
const user = userEvent.setup();
|
|
45
|
+
const onFilterChange = jest.fn();
|
|
46
|
+
render(<ProductFilters categories={categories} onFilterChange={onFilterChange} />);
|
|
47
|
+
|
|
48
|
+
// Select a category first
|
|
49
|
+
await user.selectOptions(screen.getByRole('combobox', { name: /categoria/i }), 'camisetas');
|
|
50
|
+
onFilterChange.mockClear();
|
|
51
|
+
|
|
52
|
+
await user.click(screen.getByRole('button', { name: /limpar/i }));
|
|
53
|
+
|
|
54
|
+
expect(onFilterChange).toHaveBeenCalledWith({});
|
|
55
|
+
});
|
|
56
|
+
});
|