Skip to content
Open
Show file tree
Hide file tree
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
54 changes: 54 additions & 0 deletions __tests__/apiController.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const apiController = require('../server/controllers/apiController.js');
const fetch = require('node-fetch');

jest.mock('node-fetch');

describe('apiController.newYorkTimes', () => {
afterEach(() => {
jest.clearAllMocks();
});

it('should fetch top-selling books data from the New York Times API and store it in res.locals.bestSellers', async () => {
const mockResponse = {
json: jest.fn().mockResolvedValueOnce({
results: {
books: [
{ title: 'Mock Book 1', author: 'Mock Author 1', rank: 1 },
{ title: 'Mock Book 2', author: 'Mock Author 2', rank: 2 }
]
}
})
};

fetch.mockResolvedValueOnce(mockResponse);

const req = {};
const res = {};
const next = jest.fn();

await apiController.newYorkTimes(req, res, next);

expect(fetch).toHaveBeenCalledWith('INSERT THE API HERE');
expect(mockResponse.json).toHaveBeenCalled();
expect(res.locals.bestSellers).toEqual([
{ title: 'Mock Book 1', author: 'Mock Author 1', rank: 1 },
{ title: 'Mock Book 2', author: 'Mock Author 2', rank: 2 }
]);
expect(next).toHaveBeenCalled();
});

it('should handle error if there is an issue fetching top-selling books data from the New York Times API', async () => {
const mockError = new Error('Mocked error in fetch');
fetch.mockRejectedValueOnce(mockError);

const req = {};
const res = {};
const next = jest.fn();

await apiController.newYorkTimes(req, res, next);

expect(fetch).toHaveBeenCalledWith('INSERT THE API HERE');
expect(res.locals.bestSellers).toBeUndefined();
expect(next).toHaveBeenCalledWith(mockError);
});
});
4 changes: 4 additions & 0 deletions server/controllers/apiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ apiController.checkApi = async (req, res, next) => {
}
}

apiController.newYorkTimes = async (req, res, next) => {

}

// apiController.addToGlobalLibrary = async (req, res, next) => {
// try {
// const olWorkNumber = req.body;
Expand Down