Skip to content

Commit ac58730

Browse files
authored
fix: missing headers on search endpoint with token (conventional-changelog#121)
Headers should be part of the options if we override options. https://github.com/verdaccio/ui/issues/118
1 parent 97e8448 commit ac58730

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

src/components/Search/Search.test.tsx

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BrowserRouter } from 'react-router-dom';
55
import Search from './Search';
66

77
const SEARCH_FILE_PATH = './Search';
8-
const API_FILE_PATH = '../../utils/api';
8+
const API_FILE_PATH = '../../utils/calls';
99
const URL_FILE_PATH = '../../utils/url';
1010

1111
// Global mocks
@@ -165,8 +165,7 @@ describe('<Search /> component test', () => {
165165
const suggestions = [{ name: 'verdaccio' }, { name: 'verdaccio-htpasswd' }];
166166

167167
jest.doMock(API_FILE_PATH, () => ({
168-
request(url: string) {
169-
expect(url).toEqual('search/verdaccio');
168+
callSearch(url: string) {
170169
return Promise.resolve(apiResponse);
171170
},
172171
}));
@@ -194,7 +193,7 @@ describe('<Search /> component test', () => {
194193
test('handleFetchPackages: when browser cancel a request', async () => {
195194
const apiResponse = { name: 'AbortError' };
196195

197-
jest.doMock(API_FILE_PATH, () => ({ request: jest.fn(() => Promise.reject(apiResponse)) }));
196+
jest.doMock(API_FILE_PATH, () => ({ callSearch: jest.fn(() => Promise.reject(apiResponse)) }));
198197

199198
const Search = require(SEARCH_FILE_PATH).Search;
200199

@@ -219,8 +218,7 @@ describe('<Search /> component test', () => {
219218
const apiResponse = { name: 'BAD_REQUEST' };
220219

221220
jest.doMock(API_FILE_PATH, () => ({
222-
request(url) {
223-
expect(url).toEqual('search/verdaccio');
221+
callSearch(url) {
224222
return Promise.reject(apiResponse);
225223
},
226224
}));

src/components/Search/Search.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { default as IconSearch } from '@material-ui/icons/Search';
66
import InputAdornment from '@material-ui/core/InputAdornment';
77
import debounce from 'lodash/debounce';
88

9-
import API from '../../utils/api';
109
import AutoComplete from '../AutoComplete';
1110
import colors from '../../utils/styles/colors';
11+
import { callSearch } from '../../utils/calls';
1212

1313
export interface State {
1414
search: string;
@@ -148,7 +148,7 @@ export class Search extends Component<RouteComponentProps<{}>, State> {
148148
const signal = controller.signal;
149149
// Keep track of search requests.
150150
this.requestList.push(controller);
151-
const suggestions = await API.request(`search/${encodeURIComponent(value)}`, 'GET', { signal });
151+
const suggestions = await callSearch(value, signal);
152152
// @ts-ignore
153153
this.setState({
154154
suggestions,

src/pages/Version/Version.test.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import Version from './Version';
99
import { waitForElement } from '@testing-library/dom';
1010
import ErrorBoundary from '../../App/AppError';
1111
import { LABEL_NOT_FOUND } from '../../components/NotFound/NotFound';
12-
// import { NOT_FOUND_TEXT } from '../../components/NotFound/NotFound';
1312

1413
// :-) we mock this otherways fails on render, some weird issue on material-ui
1514
jest.mock('@material-ui/core/Avatar');
@@ -27,6 +26,8 @@ describe('test Version page', () => {
2726

2827
beforeEach(() => {
2928
jest.resetAllMocks();
29+
// @ts-ignore
30+
fetch.resetMocks();
3031
});
3132

3233
test('should render the version page', async () => {

src/utils/calls.ts

+6
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ export async function callDetailPage(packageName): Promise<PackageMetaInterface
1010

1111
return packageMeta;
1212
}
13+
14+
export function callSearch(value: string, signal: any) {
15+
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#Browser_compatibility
16+
// FUTURE: signal is not well supported for IE and Samsung Browser
17+
return API.request(`search/${encodeURIComponent(value)}`, 'GET', { signal, headers: {} });
18+
}

0 commit comments

Comments
 (0)