Send asynchronous browser requests with fetch, XMLHttpRequest, JSON, errors, and progressive UI.
Lessons
Mock requests and assert UI updates.
Testing AJAX Code is easiest to learn by reading the example, changing it, and observing the result.
// Mock fetch in a test
beforeEach(() => {
global.fetch = jest.fn(() =>
Promise.resolve({ ok: true, json: () => Promise.resolve([{ title: 'AJAX' }]) })
);
});
test('loads lessons', async () => {
const data = await loadLessons();
expect(data[0].title).toBe('AJAX');
});Practice the Testing AJAX Code example in a small scratch file, then explain what changed and why.