The Sprint Challenge is your opportunity to demonstrate your understanding of the concepts covered in this sprint. You'll work on a comprehensive project that combines all the skills you've learned.
You'll need to demonstrate your ability to write tests for React components, including:
rerender()
function// Example of what you'll need to accomplish test('component updates when props change', () => { const { rerender, getByText, queryByText } = render(); // Test initial state expect(getByText('Initial State')).toBeInTheDocument(); expect(queryByText('Updated State')).not.toBeInTheDocument(); // Rerender with new props rerender( ); // Test updated state expect(queryByText('Initial State')).not.toBeInTheDocument(); expect(getByText('Updated State')).toBeInTheDocument(); });
You'll need to implement proper mocking techniques, including:
nanoid
or utilities// Example of mocking an external module jest.mock('external-module', () => ({ someFunction: jest.fn().mockReturnValue('mocked value') })); test('uses the mocked module correctly', () => { // Your test here expect(someFunction).toHaveBeenCalledTimes(1); expect(someFunction).toHaveBeenCalledWith(expectedArgs); });
You'll demonstrate your ability to test components that make asynchronous API calls:
waitFor
to handle asynchronous operations// Example of testing async operations jest.mock('../api/fetchData'); test('displays data from API when loaded', async () => { // Set up mock fetchData.mockResolvedValueOnce({ data: [{ id: 1, name: 'Test Item' }] }); const { getByText, getByTestId } = render(); const loadButton = getByText('Load Data'); // Trigger the async action userEvent.click(loadButton); // Wait for the component to update await waitFor(() => { expect(getByTestId('data-item')).toBeInTheDocument(); expect(getByText('Test Item')).toBeInTheDocument(); }); // Verify the API was called expect(fetchData).toHaveBeenCalledTimes(1); });
You'll need to implement and test authentication workflows:
// Example of authentication implementation const login = async (credentials) => { const response = await fetch('https://api.example.com/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(credentials) }); if (!response.ok) { throw new Error('Login failed'); } const data = await response.json(); localStorage.setItem('token', data.token); return data; };