1
0
mirror of https://github.com/verdaccio/verdaccio.git synced 2024-11-13 03:35:52 +01:00
verdaccio/test/unit/webui/components/search.spec.js
Priscila 9f6106cfe9 Package Refactor (#1145)
* chore: fix unit test, forgotten line on merge

chore: add node 11 to circleci (#1153)

chore: fix the use of Node 11 in CircleCI (#1154)

refactor: added css in js, new design, added types

feat: added Tag component

refactor: added modifiers

refactor: replaced by polished ellipsis

refactor: added types, new design and css in js

refactor: removed ul. it needs refactor

refactor: changed media queries

refactor: changed icon height

refactor: changed Header media queries

refactor: removed margin top

refactor: updated snapshots

refactor: removed unnecessary format author

refactor: added license icon & changed size

refactor: changed media queries

refactor: added icon size

refactor: changed media queries

refactor: updated snap

refactor: replaced Description by Details

refactor: updated tests

refactor: fixed modifiers type

feat: created missed types

fix: fixed types

fix: fixed prop type

fix: fixed undefined prop

refactor: replaced label by name

refactor: updated snap

refactor: moved polished to dev

refactor: changed formatAuthor and pkg author type

fix: removed incorrect meta's type

refactor: removed polished

refactor: added spacing & ellipsis mixin

refactor: added modifier type

refactor: changed datetime format

refactor: updated snapshots

refactor: rreplaced polished by mixins func

* refactor: updated styled

* fix: added missed semicolon

* refactor: updated media

* refactor: updated tests

* refactor: organized modifiers

* refactor: removed unused imports

* refactor: design adjusts

* refactor: updated snapshot

* refactor: changed minor things

* refactor: updated snapshots

* refactor: commented tests. fix later
2018-12-13 19:05:47 +01:00

195 lines
6.6 KiB
JavaScript

/**
* @flow
* @prettier
*/
import React from 'react';
import { mount } from 'enzyme';
import Search from '../../../../src/webui/components/Search/index';
const SEARCH_FILE_PATH = '../../../../src/webui/components/Search/index';
const API_FILE_PATH = '../../../../src/webui/utils/api';
const URL_FILE_PATH = '../../../../src/webui/utils/url';
// Global mocks
const event = {
stopPropagation: jest.fn(),
};
window.location.assign = jest.fn();
describe('<Search /> component test', () => {
let wrapper;
beforeEach(() => {
wrapper = mount(<Search />);
});
test('should load the component in default state', () => {
expect(wrapper.html()).toMatchSnapshot();
});
test('onBlur: should cancel all search requests', async () => {
const { onBlur, requestList } = wrapper.instance();
const spy = jest.spyOn(wrapper.instance(), 'cancelAllSearchRequests');
const request = {
abort: jest.fn(),
};
// adds a request for AbortController
wrapper.instance().requestList = [request];
onBlur(event);
expect(request.abort).toHaveBeenCalled();
expect(event.stopPropagation).toHaveBeenCalled();
expect(wrapper.state('error')).toBeFalsy();
expect(wrapper.state('loaded')).toBeFalsy();
expect(wrapper.state('loading')).toBeFalsy();
expect(spy).toHaveBeenCalled();
expect(requestList).toEqual([]);
});
test('handleSearch: when user type package name in search component and set loading to true', () => {
const { handleSearch } = wrapper.instance();
const newValue = 'verdaccio';
handleSearch(event, { newValue, method: 'type' });
expect(event.stopPropagation).toHaveBeenCalled();
expect(wrapper.state('error')).toBeFalsy();
expect(wrapper.state('loaded')).toBeFalsy();
expect(wrapper.state('loading')).toBeTruthy();
expect(wrapper.state('search')).toEqual(newValue);
});
test('handleSearch: cancel all search requests when there is no value in search component with type method', () => {
const { handleSearch, requestList } = wrapper.instance();
const spy = jest.spyOn(wrapper.instance(), 'cancelAllSearchRequests');
const newValue = '';
handleSearch(event, { newValue, method: 'type' });
expect(event.stopPropagation).toHaveBeenCalled();
expect(wrapper.state('error')).toBeFalsy();
expect(wrapper.state('loaded')).toBeFalsy();
expect(wrapper.state('loading')).toBeTruthy();
expect(wrapper.state('search')).toEqual(newValue);
expect(spy).toHaveBeenCalled();
expect(requestList).toEqual([]);
});
test('handleSearch: when method is not type method', () => {
const { handleSearch } = wrapper.instance();
const newValue = '';
handleSearch(event, { newValue, method: 'click' });
expect(event.stopPropagation).toHaveBeenCalled();
expect(wrapper.state('error')).toBeFalsy();
expect(wrapper.state('loaded')).toBeFalsy();
expect(wrapper.state('loading')).toBeFalsy();
expect(wrapper.state('search')).toEqual(newValue);
});
test('handlePackagesClearRequested: should clear suggestions', () => {
const { handlePackagesClearRequested } = wrapper.instance();
handlePackagesClearRequested();
expect(wrapper.state('suggestions')).toEqual([]);
});
});
describe('<Search /> component: mocks specific tests ', () => {
beforeEach(() => {
jest.resetModules();
jest.doMock('lodash/debounce', () => {
return function debounceMock(fn, delay) {
return fn;
};
});
});
test('handleFetchPackages: should load the packages from API', async () => {
const apiResponse = [{ name: 'verdaccio' }, { name: 'verdaccio-htpasswd' }];
const suggestions = [{ name: 'verdaccio' }, { name: 'verdaccio-htpasswd' }];
jest.doMock(API_FILE_PATH, () => ({
request(url) {
expect(url).toEqual('search/verdaccio');
return Promise.resolve(apiResponse);
},
}));
const Search = require(SEARCH_FILE_PATH).default;
const component = mount(<Search />);
component.setState({ search: 'verdaccio' });
const { handleFetchPackages } = component.instance();
await handleFetchPackages({ value: 'verdaccio' });
expect(component.state('suggestions')).toEqual(suggestions);
expect(component.state('error')).toBeFalsy();
expect(component.state('loaded')).toBeTruthy();
expect(component.state('loading')).toBeFalsy();
});
test('handleFetchPackages: when browser cancel a request', async () => {
const apiResponse = { name: 'AbortError' };
jest.doMock(API_FILE_PATH, () => ({ request: jest.fn(() => Promise.reject(apiResponse)) }));
const Search = require(SEARCH_FILE_PATH).default;
const component = mount(<Search />);
component.setState({ search: 'verdaccio' });
const { handleFetchPackages } = component.instance();
await handleFetchPackages({ value: 'verdaccio' });
expect(component.state('error')).toBeFalsy();
expect(component.state('loaded')).toBeFalsy();
expect(component.state('loading')).toBeFalsy();
});
test('handleFetchPackages: when API server failed request', async () => {
const apiResponse = { name: 'BAD_REQUEST' };
jest.doMock(API_FILE_PATH, () => ({
request(url) {
expect(url).toEqual('search/verdaccio');
return Promise.reject(apiResponse);
},
}));
const Search = require(SEARCH_FILE_PATH).default;
const component = mount(<Search />);
component.setState({ search: 'verdaccio' });
const { handleFetchPackages } = component.instance();
await handleFetchPackages({ value: 'verdaccio' });
expect(component.state('error')).toBeTruthy();
expect(component.state('loaded')).toBeFalsy();
expect(component.state('loading')).toBeFalsy();
});
test('handleClickSearch: should change the window location on click or return key', () => {
const getDetailPageURL = jest.fn(() => 'detail/page/url');
jest.doMock(URL_FILE_PATH, () => ({ getDetailPageURL }));
const suggestionValue = [];
const Search = require(SEARCH_FILE_PATH).default;
const component = mount(<Search />);
const { handleClickSearch } = component.instance();
// click
handleClickSearch(event, { suggestionValue, method: 'click' });
expect(event.stopPropagation).toHaveBeenCalled();
expect(getDetailPageURL).toHaveBeenCalledWith(suggestionValue);
expect(window.location.assign).toHaveBeenCalledWith('detail/page/url');
// return key
handleClickSearch(event, { suggestionValue, method: 'enter' });
expect(event.stopPropagation).toHaveBeenCalled();
expect(getDetailPageURL).toHaveBeenCalledWith(suggestionValue);
expect(window.location.assign).toHaveBeenCalledWith('detail/page/url');
});
});