|
| 1 | +import type { IExecuteFunctions, IHookFunctions } from 'n8n-workflow'; |
| 2 | +import { NodeApiError } from 'n8n-workflow'; |
| 3 | + |
| 4 | +import { spotifyApiRequest } from '../GenericFunctions'; |
| 5 | + |
| 6 | +describe('Spotify -> GenericFunctions', () => { |
| 7 | + let mockThis: IHookFunctions | IExecuteFunctions; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + mockThis = { |
| 11 | + helpers: { |
| 12 | + httpRequestWithAuthentication: jest.fn(), |
| 13 | + }, |
| 14 | + getNode: jest.fn().mockReturnValue({}), |
| 15 | + } as unknown as IHookFunctions | IExecuteFunctions; |
| 16 | + }); |
| 17 | + |
| 18 | + it('should make a request with the correct options', async () => { |
| 19 | + const method = 'GET'; |
| 20 | + const endpoint = '/me'; |
| 21 | + const body = {}; |
| 22 | + const query = { limit: 10 }; |
| 23 | + const response = { data: 'test' }; |
| 24 | + |
| 25 | + (mockThis.helpers.httpRequestWithAuthentication as jest.Mock).mockResolvedValue(response); |
| 26 | + |
| 27 | + const result = await spotifyApiRequest.call(mockThis, method, endpoint, body, query); |
| 28 | + |
| 29 | + expect(mockThis.helpers.httpRequestWithAuthentication).toHaveBeenCalledWith( |
| 30 | + 'spotifyOAuth2Api', |
| 31 | + { |
| 32 | + method, |
| 33 | + headers: { |
| 34 | + 'User-Agent': 'n8n', |
| 35 | + 'Content-Type': 'text/plain', |
| 36 | + Accept: ' application/json', |
| 37 | + }, |
| 38 | + qs: query, |
| 39 | + url: `https://api.spotify.com/v1${endpoint}`, |
| 40 | + json: true, |
| 41 | + }, |
| 42 | + ); |
| 43 | + |
| 44 | + expect(result).toEqual(response); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should throw a NodeApiError on request failure', async () => { |
| 48 | + const method = 'GET'; |
| 49 | + const endpoint = '/me'; |
| 50 | + const body = {}; |
| 51 | + const query = { limit: 10 }; |
| 52 | + const error = new Error('Request failed'); |
| 53 | + |
| 54 | + (mockThis.helpers.httpRequestWithAuthentication as jest.Mock).mockRejectedValue(error); |
| 55 | + |
| 56 | + await expect(spotifyApiRequest.call(mockThis, method, endpoint, body, query)).rejects.toThrow( |
| 57 | + NodeApiError, |
| 58 | + ); |
| 59 | + |
| 60 | + expect(mockThis.getNode).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | +}); |
0 commit comments