| 1 |
import '@testing-library/jest-dom'; |
| 2 |
import { screen } from '@testing-library/react'; |
| 3 |
import userEvent from '@testing-library/user-event'; |
| 4 |
import Customers from '../../pages/Customers'; |
| 5 |
import { createMockCustomersList, renderWithProviders } from '../setup/helpers'; |
| 6 |
|
| 7 |
jest.mock('../../features/customers/customersApi', () => ({ |
| 8 |
useGetCustomersQuery: jest.fn(), |
| 9 |
})); |
| 10 |
|
| 11 |
const { |
| 12 |
useGetCustomersQuery, |
| 13 |
} = require('../../features/customers/customersApi'); |
| 14 |
|
| 15 |
const mockData = createMockCustomersList(3); |
| 16 |
|
| 17 |
describe('Customers', () => { |
| 18 |
afterEach(() => { |
| 19 |
jest.clearAllMocks(); |
| 20 |
}); |
| 21 |
|
| 22 |
function setupLoaded(data = mockData) { |
| 23 |
useGetCustomersQuery.mockReturnValue({ |
| 24 |
data, |
| 25 |
isLoading: false, |
| 26 |
isFetching: false, |
| 27 |
}); |
| 28 |
} |
| 29 |
|
| 30 |
describe('Rendering', () => { |
| 31 |
it('renders page heading and subtitle', () => { |
| 32 |
setupLoaded(); |
| 33 |
renderWithProviders(<Customers />); |
| 34 |
|
| 35 |
expect(screen.getByText('Customers')).toBeInTheDocument(); |
| 36 |
expect( |
| 37 |
screen.getByText( |
| 38 |
'Customers who used at least one Disco campaign' |
| 39 |
) |
| 40 |
).toBeInTheDocument(); |
| 41 |
}); |
| 42 |
|
| 43 |
it('renders customer names', () => { |
| 44 |
setupLoaded(); |
| 45 |
renderWithProviders(<Customers />); |
| 46 |
|
| 47 |
expect(screen.getByText('Customer 1')).toBeInTheDocument(); |
| 48 |
expect(screen.getByText('Customer 2')).toBeInTheDocument(); |
| 49 |
}); |
| 50 |
|
| 51 |
it('renders customer emails', () => { |
| 52 |
setupLoaded(); |
| 53 |
renderWithProviders(<Customers />); |
| 54 |
|
| 55 |
expect( |
| 56 |
screen.getByText('[email protected]') |
| 57 |
).toBeInTheDocument(); |
| 58 |
}); |
| 59 |
|
| 60 |
it('renders table headers', () => { |
| 61 |
setupLoaded(); |
| 62 |
renderWithProviders(<Customers />); |
| 63 |
|
| 64 |
expect(screen.getByText('Customer')).toBeInTheDocument(); |
| 65 |
expect(screen.getByText('State')).toBeInTheDocument(); |
| 66 |
expect(screen.getByText('Campaigns Used')).toBeInTheDocument(); |
| 67 |
expect(screen.getByText(/Orders/)).toBeInTheDocument(); |
| 68 |
expect(screen.getByText(/Spent/)).toBeInTheDocument(); |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
describe('Loading states', () => { |
| 73 |
it('shows loading skeletons', () => { |
| 74 |
useGetCustomersQuery.mockReturnValue({ |
| 75 |
data: undefined, |
| 76 |
isLoading: true, |
| 77 |
isFetching: true, |
| 78 |
}); |
| 79 |
const { container } = renderWithProviders(<Customers />); |
| 80 |
|
| 81 |
const pulseElements = container.querySelectorAll( |
| 82 |
'.disco-animate-pulse' |
| 83 |
); |
| 84 |
expect(pulseElements.length).toBeGreaterThan(0); |
| 85 |
}); |
| 86 |
}); |
| 87 |
|
| 88 |
describe('Empty states', () => { |
| 89 |
it('shows empty state when no customers', () => { |
| 90 |
useGetCustomersQuery.mockReturnValue({ |
| 91 |
data: { |
| 92 |
data: [], |
| 93 |
collection: { count: 0, total: 0, total_pages: 1 }, |
| 94 |
}, |
| 95 |
isLoading: false, |
| 96 |
isFetching: false, |
| 97 |
}); |
| 98 |
renderWithProviders(<Customers />); |
| 99 |
|
| 100 |
expect( |
| 101 |
screen.getByText('Customers Not Found!') |
| 102 |
).toBeInTheDocument(); |
| 103 |
}); |
| 104 |
}); |
| 105 |
|
| 106 |
describe('Sorting', () => { |
| 107 |
it('default sort is by spent descending', () => { |
| 108 |
setupLoaded(); |
| 109 |
renderWithProviders(<Customers />); |
| 110 |
|
| 111 |
const firstCall = useGetCustomersQuery.mock.calls[0][0]; |
| 112 |
expect(firstCall.sort_by).toBe('total_spent'); |
| 113 |
expect(firstCall.order).toBe('desc'); |
| 114 |
}); |
| 115 |
}); |
| 116 |
|
| 117 |
describe('Search', () => { |
| 118 |
it('resets page and passes search to hook', async () => { |
| 119 |
setupLoaded(); |
| 120 |
const user = userEvent.setup(); |
| 121 |
renderWithProviders(<Customers />); |
| 122 |
|
| 123 |
const searchInput = screen.getByPlaceholderText( |
| 124 |
'Search Customers...' |
| 125 |
); |
| 126 |
await user.type(searchInput, 'jane'); |
| 127 |
|
| 128 |
const lastCall = useGetCustomersQuery.mock.calls.at(-1)[0]; |
| 129 |
expect(lastCall.search).toBe('jane'); |
| 130 |
expect(lastCall.page).toBe(1); |
| 131 |
}); |
| 132 |
}); |
| 133 |
}); |
| 134 |
|