Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add fetchQutoes test #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion js/quote.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getRandomQuote } from './quote';
import { getRandomQuote, fetchQuotes } from './quote';

describe('getRandomQuote', () => {
test('returns a random quote from an array of quotes', () => {
Expand All @@ -10,4 +10,51 @@ describe('getRandomQuote', () => {
const randomQuote = getRandomQuote(sampleQuotes);
expect(sampleQuotes).toContain(randomQuote);
});
});


describe('fetchQuotes', () => {
it('should return an array of quotes', async () => {
const quotes = await fetchQuotes();
expect(Array.isArray(quotes)).toBe(true);
});

it('should throw an error if the server returns an error status', async () => {
// Mock a failed fetch response
global.fetch = jest.fn(() =>
Promise.resolve({
ok: false,
status: 404,
statusText: 'Not Found',
}),
);

try {
await fetchQuotes();
} catch (error) {
expect(error.message).toBe('Server returned 404 Not Found');
}

// Restore the original fetch function
global.fetch.mockRestore();
});

it('should throw an error if the data structure is invalid', async () => {
// Mock an invalid fetch response
global.fetch = jest.fn(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({}),
}),
);

try {
await fetchQuotes();
} catch (error) {
expect(error.message).toBe('Invalid data structure: missing quotes array');
}

// Restore the original fetch function
global.fetch.mockRestore();
});
});