diff --git a/cypress/e2e/onboarding.cy.ts b/cypress/e2e/onboarding.cy.ts index 6f1908c..d2aa75a 100644 --- a/cypress/e2e/onboarding.cy.ts +++ b/cypress/e2e/onboarding.cy.ts @@ -6,7 +6,8 @@ describe('Supplier Portal Onboarding & Feature E2E Flow', () => { // 2. Registration Page cy.contains('Get Started Today').click(); - cy.get('input#reg-email').type('test_seller@example.com'); + const randUser = `test_seller_${Date.now()}@example.com`; + cy.get('input#reg-email').type(randUser); cy.get('input#reg-phone').type('9876543210'); cy.get('input#reg-pass').type('super_secure_pass_123'); cy.get('input#reg-confirm-pass').type('super_secure_pass_123'); @@ -109,7 +110,8 @@ describe('Supplier Portal Onboarding & Feature E2E Flow', () => { // 2. Go to Registration cy.contains('Get Started Today').click(); - cy.get('input#reg-email').type('security_test@example.com'); + const randSecurityUser = `security_test_${Date.now()}@example.com`; + cy.get('input#reg-email').type(randSecurityUser); cy.get('input#reg-phone').type('9000000000'); cy.get('input#reg-pass').type('password123'); cy.get('input#reg-confirm-pass').type('password123'); diff --git a/src/App.test.tsx b/src/App.test.tsx index 0ab93a2..2bf68a7 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -2,9 +2,27 @@ import { render, screen, fireEvent } from '@testing-library/react' import { describe, it, expect, vi, beforeAll } from 'vitest' import App from './App' -// Mock window.scrollTo since jsdom does not implement it +// Mock window.scrollTo and fetch since jsdom does not implement them beforeAll(() => { window.scrollTo = vi.fn() + global.fetch = vi.fn((url) => { + let responseData: any = {}; + if (url.includes('/api/auth/register') || url.includes('/api/auth/login')) { + responseData = { id: 1, username: 'test_seller', email: 'test@example.com' }; + } else if (url.includes('/api/products')) { + responseData = []; + } else if (url.includes('/api/orders')) { + responseData = []; + } else if (url.includes('/api/returns')) { + responseData = []; + } else if (url.includes('/api/wallet')) { + responseData = { outstanding: 850.00, withdrawn: 1250.00, transactions: [] }; + } + return Promise.resolve({ + ok: true, + json: () => Promise.resolve(responseData) + } as Response); + }) }) describe('Supplier Portal Onboarding & Active Dashboard Tests', () => { @@ -64,7 +82,7 @@ describe('Supplier Portal Onboarding & Active Dashboard Tests', () => { fireEvent.click(registerBtn) // Step 1: Contact Verification - expect(screen.getByText(/Step 1 of 3: Contact Verification/i)).toBeInTheDocument() + expect(await screen.findByText(/Step 1 of 3: Contact Verification/i)).toBeInTheDocument() const sendWhatsappBtn = screen.getByText(/Send WhatsApp OTP/i) fireEvent.click(sendWhatsappBtn) @@ -151,7 +169,7 @@ describe('Supplier Portal Onboarding & Active Dashboard Tests', () => { alertMock.mockRestore() }) - it('logs in successfully and navigates dashboard tools', () => { + it('logs in successfully and navigates dashboard tools', async () => { render() const loginBtn = screen.getByRole('button', { name: /^Login$/i }) @@ -167,7 +185,7 @@ describe('Supplier Portal Onboarding & Active Dashboard Tests', () => { fireEvent.click(submitBtn) // Check dashboard rendering - expect(screen.getByText('Performance Analytics')).toBeInTheDocument() + expect(await screen.findByText('Performance Analytics')).toBeInTheDocument() // Check Sidebar Tab click: Manage Products const productsTabBtn = screen.getByText(/Manage Products/i) diff --git a/src/App.tsx b/src/App.tsx index dba3702..e864612 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -269,7 +269,34 @@ export default function App() { return; } - navigateTo('profile-completion') + fetch(`${CONFIG.apiBaseUrl}/api/auth/register/`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ + username: email, + email: email, + phone: phone, + password: password + }) + }) + .then(res => { + if (!res.ok) throw new Error('Registration failed. Username/email might already be taken.'); + return res.json(); + }) + .then(() => { + // Auto login after signup + fetch(`${CONFIG.apiBaseUrl}/api/auth/login/`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: email, password: password }) + }) + .then(() => { + navigateTo('profile-completion') + }) + }) + .catch(err => { + alert(err.message); + }); } const handleLoginSubmit = (e: React.FormEvent) => { @@ -282,8 +309,22 @@ export default function App() { return; } - setIsProfileComplete(true) - navigateTo('dashboard', true) + fetch(`${CONFIG.apiBaseUrl}/api/auth/login/`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ username: email, password: password }) + }) + .then(res => { + if (!res.ok) throw new Error('Invalid credentials.'); + return res.json(); + }) + .then(() => { + setIsProfileComplete(true) + navigateTo('dashboard', true) + }) + .catch(err => { + alert(err.message); + }); } const handleProfileSubmit = (e: React.FormEvent) => {