I am attempting to test the auth header when doing an api call:
import axios from 'axios';
import ApiClient from './apiClient';
import ApiService from './apiService';
jest.mock('./apiClient', () => {
return {
headers: { authorization: 'Bearer test token' },
get: jest.fn()
};
});
describe('apiService methods', () => {
beforeAll(() => {
ApiClient.get.mockImplementation((url: string) => {
return Promise.resolve({ data: mockData });
});
});
it('getArticles method call', () => {
ApiService.getArticles(jest.fn(), 1, 3, 'latest', 'news', 'selectedNews');
expect(axios.defaults.headers.common.Authorization).toBe(
'Bearer test token'
); // THIS GIVES UNDEFINED SO THE TEST FAILS
expect(ApiClient.get).toBeCalledWith(
'/content?type=article&page=1&limit=3&sort=latest&facet=news&article_category=selectedNews'
);
});
});
This is what I have on ApiClient to request the header:
import axios from 'axios';
import envs from '../../config/index.json';
const client = axios.create({
baseURL: envs.data.data.centralApi.baseUrl + apiVersion
});
client.defaults.headers.common.Authorization = `Bearer ${localStorage.getItem(
`adal.access.token.key${envs.data.data.adal.clientId}`
)}`;
So I need to test that the Authorization header is properly send when the api call is performed.
What should I do?