Initial commit

This commit is contained in:
zhongjin
2020-06-15 10:58:47 +08:00
commit 4f1dfe7564
8590 changed files with 1516878 additions and 0 deletions
@@ -0,0 +1,58 @@
var CustomListItemModel = require('builder/components/custom-list/custom-list-item-model');
var CustomListCollection = require('builder/components/custom-list/custom-list-collection');
describe('components/custom-list/custom-list-collection', function () {
beforeEach(function () {
this.collection = new CustomListCollection([
new CustomListItemModel({ val: 'hi' }),
new CustomListItemModel({ val: 'howdy' }),
new CustomListItemModel({ val: 'hello' })
]);
});
it('should search properly', function () {
expect(this.collection.search('+').size()).toBe(0);
expect(this.collection.search('h').size()).toBe(3);
expect(this.collection.search('he').size()).toBe(1);
expect(this.collection.search('/').size()).toBe(0);
});
it('should remove previous selected item when a new one is chosen', function () {
this.collection.at(1).set('selected', true);
expect(this.collection.where({ selected: true }).length).toBe(1);
this.collection.at(0).set('selected', true);
expect(this.collection.where({ selected: true }).length).toBe(1);
expect(this.collection.at(1).get('selected')).toBeFalsy();
});
it('should remove any selected item', function () {
this.collection.at(1).set('selected', true);
this.collection.removeSelected();
expect(this.collection.where({ selected: true }).length).toBe(0);
});
describe('sort', function () {
it('should sort the collection by value', function () {
this.collection.sortByKey('val');
expect(this.collection.at(0).get('val')).toBe('hello');
expect(this.collection.at(1).get('val')).toBe('hi');
expect(this.collection.at(2).get('val')).toBe('howdy');
});
it('should sort the collection correctly if there is a null or an undefined value', function () {
var collection = new CustomListCollection([
{ val: 'hi' },
{ val: 'howdy' },
{ val: 'hello' },
{ val: 'hola' },
{ val: null },
{ val: undefined }
]);
collection.sortByKey('val');
expect(collection.at(2).get('val')).toBe('hola');
expect(collection.at(4).get('val')).toBeNull();
expect(collection.at(5).get('val')).toBeUndefined();
});
});
});
@@ -0,0 +1,58 @@
var CustomListItemModel = require('builder/components/custom-list/custom-list-item-model');
var CustomListItemView = require('builder/components/custom-list/custom-list-item-view');
describe('components/custom-list/custom-list-item-view', function () {
beforeEach(function () {
this.model = new CustomListItemModel({
val: 'hello'
});
this.view = new CustomListItemView({
model: this.model,
typeLabel: 'column'
});
this.view.render();
});
it('should render properly', function () {
expect(this.view.$el.data('val')).not.toBeUndefined();
expect(this.view.$('button').length).toBe(1);
expect(this.view.$('button').attr('title')).toBe(this.model.getName());
});
it('should add disabled class if it has disabled property', function () {
expect(this.view.$el.hasClass('is-disabled')).toBeFalsy();
this.model.set('disabled', true);
this.view.render();
expect(this.view.$el.hasClass('is-disabled')).toBeTruthy();
});
describe('hover', function () {
beforeEach(function () {
this.view.$el.trigger('mouseenter');
});
it('should add class when it is hovered', function () {
expect(this.view.$el.hasClass('is-highlighted')).toBeTruthy();
});
it('should remove class when it is unhovered', function () {
this.view.$el.trigger('mouseleave');
expect(this.view.$el.hasClass('is-highlighted')).toBeFalsy();
});
});
it('should set selected when it is clicked', function () {
expect(this.model.get('selected')).toBeFalsy();
this.view.$el.trigger('click');
expect(this.model.get('selected')).toBeTruthy();
});
it('shouldn\'t toggle by default when clicking', function () {
expect(this.model.get('selected')).toBeFalsy();
this.view.$el.trigger('click');
this.view.$el.trigger('click');
expect(this.model.get('selected')).toBeTruthy();
});
});
@@ -0,0 +1,21 @@
var CustomListItemModel = require('builder/components/custom-list/custom-list-item-model');
var CustomListCollection = require('builder/components/custom-list/custom-list-multi-collection');
describe('components/custom-list/custom-list-multi-collection', function () {
beforeEach(function () {
this.collection = new CustomListCollection([
new CustomListItemModel({ val: 'hi' }),
new CustomListItemModel({ val: 'howdy' }),
new CustomListItemModel({ val: 'hello' })
]);
});
it('shouldn\'t remove previous selected item when a new one is chosen', function () {
this.collection.at(1).set('selected', true);
expect(this.collection.where({ selected: true }).length).toBe(1);
this.collection.at(0).set('selected', true);
expect(this.collection.where({ selected: true }).length).toBe(2);
expect(this.collection.at(0).get('selected')).toBeTruthy();
expect(this.collection.at(1).get('selected')).toBeTruthy();
});
});
@@ -0,0 +1,25 @@
var CustomListItemModel = require('builder/components/custom-list/custom-list-item-model');
var CustomListItemView = require('builder/components/custom-list/custom-list-multi-item-view');
describe('components/custom-list/custom-list-item-view', function () {
beforeEach(function () {
this.model = new CustomListItemModel({
val: 'hello'
});
this.view = new CustomListItemView({
model: this.model,
typeLabel: 'column'
});
this.view.render();
});
it('should toggle when clicking', function () {
expect(this.model.get('selected')).toBeFalsy();
this.view.$el.trigger('click');
expect(this.model.get('selected')).toBeTruthy();
this.view.$el.trigger('click');
expect(this.model.get('selected')).toBeFalsy();
});
});
@@ -0,0 +1,70 @@
var Backbone = require('backbone');
var _ = require('underscore');
var CustomListSearchView = require('builder/components/custom-list/custom-list-search-view');
describe('components/custom-list/custom-list-search-view', function () {
var createViewFn = function (options) {
this.model = new Backbone.Model({ visible: true, query: '' });
var defaultOptions = {
model: this.model,
typeLabel: 'column'
};
spyOn(CustomListSearchView.prototype, '_checkButtons').and.callThrough();
this.view = new CustomListSearchView(_.extend(defaultOptions, options));
this.view.render();
};
beforeEach(function () {
this.createView = createViewFn.bind(this);
});
describe('render', function () {
it('should render properly', function () {
this.createView();
expect(this.view.$('input[type="text"]').length).toBe(1);
expect(this.view.$('button[type="button"]').length).toBe(1);
expect(this.view.$('input').attr('placeholder')).toEqual('components.custom-list.placeholder');
});
it('should render custom placeholder if provided', function () {
var customSearchPlaceholder = 'search by column';
this.createView({
searchPlaceholder: customSearchPlaceholder
});
expect(this.view.$('input').attr('placeholder')).toEqual(customSearchPlaceholder);
});
});
describe('.binds', function () {
beforeEach(function () {
this.createView();
});
it('should submit form on key pressed in the text input', function () {
spyOn(this.view, '_submit');
this.view.$('input').trigger('keyup');
expect(this.view._submit).toHaveBeenCalled();
});
it('should check buttons when query changes', function () {
var $button = this.view.$('button[type="button"]');
expect($button.hasClass('u-transparent')).toBe(true);
this.model.set('query', 'h');
expect(this.view._checkButtons).toHaveBeenCalled();
expect($button.hasClass('u-transparent')).toBe(false);
});
it('should remove query when clear is pressed', function () {
spyOn(this.view, 'focus');
var $button = this.view.$('button[type="button"]');
this.model.set('query', 'h');
$button.click();
expect(this.model.get('query')).toBe('');
expect(this.view.focus).toHaveBeenCalled();
});
});
});
@@ -0,0 +1,188 @@
var Backbone = require('backbone');
var _ = require('underscore');
var CustomListView = require('builder/components/custom-list/custom-list-view');
var CustomCollection = require('builder/components/custom-list/custom-list-collection');
var itemTemplate = require('builder/components/custom-list/custom-list-item.tpl');
var CustomListItemView = require('builder/components/custom-list/custom-list-item-view');
describe('components/custom-list/custom-list-view', function () {
var mouseOverAction;
var mouseOutAction;
beforeEach(function () {
this.collection = new CustomCollection([
{
val: 'hello'
}, {
label: 'Hi',
val: 'hi'
}, {
label: null,
val: 'howdy'
}, {
label: 0,
val: 'holi'
}
]);
mouseOverAction = jasmine.createSpy('mouseOverAction');
mouseOutAction = jasmine.createSpy('mouseOutAction');
this.view = new CustomListView({
model: new Backbone.Model({ visible: true, query: '' }),
collection: this.collection,
typeLabel: 'column',
itemView: CustomListItemView,
itemTemplate: itemTemplate,
mouseOverAction: mouseOverAction,
mouseOutAction: mouseOutAction
});
});
describe('with free text input', function () {
beforeEach(function () {
this.viewWithFreeText = new CustomListView({
model: new Backbone.Model({ visible: true, query: 'tomato' }),
collection: this.collection,
typeLabel: 'column',
itemView: CustomListItemView,
itemTemplate: itemTemplate,
allowFreeTextInput: true
});
spyOn(this.viewWithFreeText, '_applyCustomScroll');
spyOn(this.viewWithFreeText, '_applyArrowBinds');
spyOn(this.collection, 'search').and.callThrough();
this.viewWithFreeText.render();
});
it('should render the add message when there are no items', function () {
spyOn(this.collection, 'size').and.returnValue(0);
this.viewWithFreeText.render();
expect(this.viewWithFreeText.$el.text()).toContain('tomato');
expect(this.viewWithFreeText.$el.text()).toContain('components.custom-list.add-custom-result');
});
});
describe('render', function () {
beforeEach(function () {
jasmine.clock().install();
spyOn(this.view, '_applyCustomScroll');
spyOn(this.view, '_applyArrowBinds');
spyOn(this.collection, 'search').and.callThrough();
this.view.render();
});
it('should render empty message when there are no items', function () {
spyOn(this.collection, 'size').and.returnValue(0);
this.view.render();
expect(this.view.$('.CustomList-message').length).toBe(1);
});
it('should render collection items', function () {
expect(this.view.$('.js-list').length).toBe(1);
expect(this.view.$('li').length).toBe(4);
expect(this.view.$('li:eq(0)').text()).toContain('hello');
expect(this.view.$('li:eq(1)').text()).toContain('Hi');
expect(this.view.$('li:eq(2)').text()).toContain('howdy');
expect(this.view.$('li:eq(3)').text()).toContain('0');
expect(this.collection.search).toHaveBeenCalled();
});
it('should apply the custom scroll and the arrows binding', function () {
jasmine.clock().tick(100);
expect(this.view._applyCustomScroll).toHaveBeenCalled();
expect(this.view._applyArrowBinds).toHaveBeenCalled();
});
afterEach(function () {
jasmine.clock().uninstall();
});
});
describe('arrow + ENTER bindings', function () {
beforeEach(function () {
this.view.render();
});
it('should highlight next (first) item when arrow key is pressed', function () {
dispatchDocumentEvent('keydown', { which: 40 });
expect(this.view.$('.is-highlighted').length).toBe(1);
expect(this.view.$('li:eq(0)').hasClass('is-highlighted')).toBeTruthy();
});
it('should highlight prev (last) item when arrow key is pressed', function () {
for (var i = 0, l = 3; i<l; i++) { // eslint-disable-line
dispatchDocumentEvent('keydown', { which: 40 });
}
dispatchDocumentEvent('keydown', { which: 38 });
expect(this.view.$('.is-highlighted').length).toBe(1);
expect(this.view.$('li:eq(1)').hasClass('is-highlighted')).toBeTruthy();
});
it('should select current highlighed item when ENTER is pressed', function () {
for (var i = 0, l = 2; i<l; i++) { // eslint-disable-line
dispatchDocumentEvent('keydown', { which: 40 });
}
expect(this.view.$('li:eq(1)').hasClass('is-highlighted')).toBeTruthy();
var selectedItem = _.first(this.collection.where({ val: this.view.$('li:eq(1)').data('val') }));
expect(selectedItem.get('selected')).toBeFalsy();
dispatchDocumentEvent('keydown', { which: 13 });
expect(selectedItem.get('selected')).toBeTruthy();
});
});
it('should remove bindings when view is cleaned', function () {
spyOn(this.view, '_removeArrowBinds');
spyOn(this.view, '_destroyCustomScroll');
this.view.clean();
expect(this.view._removeArrowBinds).toHaveBeenCalled();
expect(this.view._destroyCustomScroll).toHaveBeenCalled();
});
it('should remove bindings properly', function () {
spyOn(this.view, 'render');
this.view.clean();
dispatchDocumentEvent('keydown', { which: 40 });
expect(this.view.render).not.toHaveBeenCalled();
});
it('should highlight selected value on show', function () {
this.collection.at(2).set({ selected: true });
this.view.render();
this.view.highlight();
expect(this.view.$('li:eq(2)').hasClass('is-highlighted')).toBeTruthy();
});
describe('mouseOverAction', function () {
describe('._onMouseOver', function () {
it('should trigger mouseOverAction', function () {
this.view.render();
this.view._onMouseOver();
expect(mouseOverAction).toHaveBeenCalled();
});
});
});
describe('mouseOutAction', function () {
describe('._onMouseOut', function () {
it('should trigger mouseOutAction', function () {
this.view.render();
this.view._onMouseOut();
expect(mouseOutAction).toHaveBeenCalled();
});
});
});
function dispatchDocumentEvent (type, opts) {
var e = document.createEvent('HTMLEvents');
e.initEvent(type, false, true);
if (opts.which) {
e.which = opts.which;
}
document.dispatchEvent(e, opts);
}
});
@@ -0,0 +1,171 @@
var Backbone = require('backbone');
var CustomView = require('builder/components/custom-list/custom-view');
describe('components/custom-list/custom-view', function () {
var mouseOverAction;
var mouseOutAction;
beforeEach(function () {
this.searchPlaceholderText = 'a search placeholder';
mouseOverAction = jasmine.createSpy('mouseOverAction');
mouseOutAction = jasmine.createSpy('mouseOutAction');
this.view = new CustomView({
searchPlaceholder: this.searchPlaceholderText,
options: [
{
val: 'hello'
}, {
val: 'hi'
}, {
val: 'howdy'
}
],
mouseOverAction: mouseOverAction,
mouseOutAction: mouseOutAction
});
});
it('should generate internal collection and model', function () {
expect(this.view.model).toBeDefined();
expect(this.view.collection).toBeDefined();
});
describe('render', function () {
beforeEach(function () {
spyOn(this.view, '_renderHeader').and.callThrough();
spyOn(this.view, '_renderSearch').and.callThrough();
spyOn(this.view, '_renderActions').and.callThrough();
spyOn(this.view, '_renderList');
this.view.render();
});
it('should render list and search view by default', function () {
expect(this.view._renderHeader).toHaveBeenCalled();
expect(this.view._renderSearch).toHaveBeenCalled();
expect(this.view._renderList).toHaveBeenCalled();
expect(this.view._renderActions).not.toHaveBeenCalled();
expect(this.view._searchView.options.searchPlaceholder).toEqual(this.searchPlaceholderText);
});
it('should not render search view if showSearch is disabled', function () {
expect(this.view._renderSearch.calls.count()).toBe(1);
expect(this.view._renderList.calls.count()).toBe(1);
this.view.options.showSearch = false;
this.view.render();
expect(this.view._renderSearch.calls.count()).toBe(1);
expect(this.view._renderList.calls.count()).toBe(2);
});
});
describe('with actions', function () {
beforeEach(function () {
this.actions = [
{ label: 'All', action: function () {} },
{ label: 'None', action: function () {} }
];
this.viewWithActions = new CustomView({
searchPlaceholder: this.searchPlaceholderText,
options: [
{ val: 'hello' },
{ val: 'hi' },
{ val: 'howdy' }
],
actions: this.actions
});
spyOn(this.viewWithActions, '_renderActions').and.callThrough();
spyOn(this.actions[0], 'action');
spyOn(this.actions[1], 'action');
this.viewWithActions.render();
});
it('should render the actions', function () {
expect(this.viewWithActions._renderActions).toHaveBeenCalled();
});
it('should call the provided action', function () {
expect(this.actions[0].action).not.toHaveBeenCalled();
expect(this.actions[1].action).not.toHaveBeenCalled();
this.viewWithActions.$('.js-header button').each(function (i, button) {
button.click();
});
expect(this.actions[0].action).toHaveBeenCalled();
expect(this.actions[1].action).toHaveBeenCalled();
});
it('should hide the actions if there is a search query', function () {
this.viewWithActions.model.set('query', 'rick');
expect(this.viewWithActions.$('.js-actions').hasClass('u-hide')).toBe(true);
});
});
describe('on change visibility', function () {
beforeEach(function () {
spyOn(this.view, '_toggleVisibility');
spyOn(this.view, 'clearSubViews');
spyOn(this.view, 'render');
this.view.model.set({
visible: true,
query: 'hello'
});
});
it('should reset query attribute, toggle the visibility and render', function () {
expect(this.view._toggleVisibility).toHaveBeenCalled();
expect(this.view.model.get('query')).toBe('');
expect(this.view.render).toHaveBeenCalled();
});
it('should clear subviews when visibility changes to false', function () {
this.view.render.calls.reset();
this.view.model.set({
visible: false
});
expect(this.view.clearSubViews).toHaveBeenCalled();
expect(this.view.render).not.toHaveBeenCalled();
});
it('should set default value to query if _selectModel is present', function () {
this.viewWithSelectModel = new CustomView({
searchPlaceholder: this.searchPlaceholderText,
options: [
{ val: 'hello' },
{ val: 'hi' },
{ val: 'howdy' }
],
typeLabel: 'test',
selectModel: new Backbone.Model({ test: 'Morty' })
});
this.viewWithSelectModel._resetQuery();
expect(this.viewWithSelectModel.model.get('query')).toEqual('Morty');
});
});
describe('mouseOverAction', function () {
describe('._onMouseOver', function () {
it('should trigger mouseOverAction', function () {
this.view.render();
this.view._onMouseOver();
expect(mouseOverAction).toHaveBeenCalled();
});
});
});
describe('mouseOutAction', function () {
describe('._onMouseOut', function () {
it('should trigger mouseOutAction', function () {
this.view.render();
this.view._onMouseOut();
expect(mouseOutAction).toHaveBeenCalled();
});
});
});
});