6bada521 by Adam Heath

Reorganized the spec layout, now they are stored alongside the files

they test.  Facets is close to full coverage.
1 parent e3b61d72
......@@ -60,13 +60,29 @@ module.exports = function(grunt) {
var _ = require('lodash');
var jasmineRequirejsTemplateOptions = function(withInstanbul) {
function genJasmineConfig(withCoverage, options) {
var template, templateOptions;
var src = [
'src/scripts/{,**/}*.js',
'!src/scripts/main.js',
];
var specs = [
'src/scripts/**/*.spec.js',
];
var template = require('grunt-template-jasmine-requirejs');
var templateOptions = {
requireConfigFile: 'src/scripts/config.js',
requireConfig: {
baseUrl: 'src/scripts',
},
};
if (withCoverage) {
/* global requirejs */
var callback;
if (withInstanbul) {
callback = function() {
templateOptions.requireConfig.callback = function() {
var oldLoad = requirejs.load;
requirejs.load = function (context, moduleName, url) {
var origUrl = url;
//console.log('context=' + JSON.stringify(arguments), 'moduleName=' + moduleName, 'url=' + url);
var parts = url.split('/');
for (var i = 0; i < parts.length; ) {
......@@ -87,58 +103,55 @@ module.exports = function(grunt) {
}
}
url = parts.join('/');
if (url.indexOf('src/scripts/') === 0) {
if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.spec\.js$/)) {
url = './.grunt/grunt-contrib-jasmine/' + url;
} else if (url.match(/^src\/scripts\/(.*?\/)?[^\/]+\.js$/)) {
url = './.grunt/grunt-contrib-jasmine/' + url;
}
if (url.indexOf('test/specs/') === 0) {
url = './.grunt/grunt-contrib-jasmine/' + url;
}
//console.log('url=' + url);
//console.log('url[' + origUrl + ']=' + url);
return oldLoad.apply(this, [context, moduleName, url]);
};
};
var foo = _.extend({}, template);
var bar = template.process;
foo.process = function process(grunt, task, context) {
var src = [];
context.scripts.src.forEach(function(file) {
if (!grunt.file.isMatch(specs, file)) {
src.push(file);
}
return {
requireConfigFile: 'src/scripts/config.js',
requireConfig: {
baseUrl: 'src/scripts',
callback: callback
}
};
});
context.scripts.src = src;
return bar.apply(this, arguments);
};
var jasmineInstanbulTemplateOptions = function(template, nestedOptions) {
return {
templateOptions = {
coverage: 'bin/coverage/coverage.json',
report: 'bin/coverage',
replace: false,
template: template,
templateOptions: nestedOptions
template: foo,
templateOptions: templateOptions,
files: _.concat([], src, specs),
};
};
var jasmineOptions = function(withCoverage, options) {
var defaultOptions = {};
defaultOptions.template = require('grunt-template-jasmine-requirejs');
defaultOptions.templateOptions = jasmineRequirejsTemplateOptions(withCoverage);
if (withCoverage) {
defaultOptions.templateOptions = jasmineInstanbulTemplateOptions(defaultOptions.template, defaultOptions.templateOptions);
defaultOptions.template = require('grunt-template-jasmine-istanbul');
template = require('grunt-template-jasmine-istanbul');
}
return _.extend(defaultOptions, options);
};
grunt.initConfig({
jasmine: {
all: {
src: [
'src/scripts/{,**/}*.js',
'!src/scripts/main.js',
'test/specs/**/*.js',
],
options: jasmineOptions(false, null),
},
var jasmineConfig = {
src: _.concat([],
src,
specs
),
options: {
specs: specs,
template: template,
templateOptions: templateOptions,
},
}
return jasmineConfig;
}
});
grunt.config.set(['jasmine', 'all'], genJasmineConfig(true));
};
......
......@@ -194,10 +194,6 @@ define(function(require) {
queryField: 'value_d',
facetType: 'year',
facetStats: true,
formatter: function(item) {
var value = parseInt(item.get('key'));
return '$' + value + ' - $' + (value + 1000);
},
};
var pojoItems = [
{key: '2005', value: 500},
......@@ -247,10 +243,6 @@ define(function(require) {
queryField: 'value_d',
facetType: 'year',
facetStats: true,
formatter: function(item) {
var value = parseInt(item.get('key'));
return '$' + value + ' - $' + (value + 1000);
},
};
if (hasBefore) {
if (hasAfter) {
......@@ -330,4 +322,5 @@ define(function(require) {
rangeSupportHelper(false, true);
});
});
return {};
});
......
......@@ -17,7 +17,8 @@ define(function(require) {
var Facets = NestedModels.mixin(Backbone.Model.extend({
initialize: function(data, options) {
this.url = function() {
return options.search.url();
var url = options.search.url;
return _.isFunction(url) ? url.call() : url;
};
_.each(this.keys(), _.bind(function(facetName) {
var facet = this.get(facetName);
......@@ -28,6 +29,7 @@ define(function(require) {
'change:queryMax': 'item-change',
}, function(eventValue, eventKey) {
facet.on(eventKey, function(model, value, options) {
console.log('trigger', facet.get('formName'), eventValue);
this.trigger(eventValue, facet, null, options);
}, this);
}, this);
......@@ -88,7 +90,8 @@ define(function(require) {
}
});
facet.set('suggestions', suggestions);
if (!!options.facetSuggestions && options.childFacet === facet) {
//if (!!options.facetSuggestions && options.childFacet === facet) {
if (!!options.facetSuggestions) {
return;
}
......@@ -126,10 +129,6 @@ define(function(require) {
item.set({hidden: value === 0, value: value});
} else {
item = new Facet.Item({key: key, value: value});
item.on('change:checked', function() {
this.trigger('item-change');
facet.trigger('item-change');
}, this);
}
newItems.push(item);
}, this);
......@@ -200,7 +199,7 @@ define(function(require) {
var key = item.get('key');
var fromDate, toDate;
if (key === 'before') {
return rangeFormatter('*', facet.rangeStart + '-1SECOND');
return '[* TO ' + facet.rangeStart + '-1SECOND]';
}
fromDate = new Date(key);
toDate = new Date(key);
......
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Facets = require('solr/model/Facets');
function setData(obj, key, value) {
return obj[key] = value;
}
function getData(obj, key) {
return obj[key];
}
function createSolrResponse(has) {
var solrData = {};
var facetCounts = setData(solrData, 'facet_counts', {});
var facetIntervals = setData(facetCounts, 'facet_intervals', {});
var facetRanges = setData(facetCounts, 'facet_ranges', {});
var facetFields = setData(facetCounts, 'facet_fields', {});
var facetQueries = setData(facetCounts, 'facet_queries', {});
if (has.stats) {
solrData.stats = {};
setData(solrData.stats, 'stats_fields', {});
}
return solrData;
}
function makeSolrResponseMixedCase(solrData) {
var facetCounts = getData(solrData, 'facet_counts');
return {
facetCounts: facetCounts,
facetRanges: getData(facetCounts, 'facet_ranges'),
facetFields: getData(facetCounts, 'facet_fields'),
facetQueries: getData(facetCounts, 'facet_queries'),
statsFields: getData(solrData.stats, 'stats_fields'),
};
}
describe('Facets', function() {
it('loads', function() {
expect(Facets).toBeDefined();
});
describe('foo', function() {
var f;
var formData = [];
var solrData = [];
var origNow;
var onItemChange;
beforeEach(function() {
jasmine.clock().install();
jasmine.clock().mockDate();
origNow = _.now;
_.now = function() { return new Date().getTime(); };
spyOn(Backbone, 'ajax');
var facets = {
model: new Facets.Facet({
formName: 'model',
label: 'Model',
}, {
queryField: 'model_s',
facetType: 'field',
bins: {
misc: ['saturn', 'jeep'],
startsWithB: ['buick'],
},
}),
year: new Facets.Facet({
formName: 'year',
label: 'Year',
}, {
queryField: 'year_d',
comparator: Facets.Sort.date,
facetType: 'year',
facetStats: true,
}),
dateOfBirth: new Facets.Facet({
formName: 'dateOfBirth',
label: 'Birthday',
}, {
queryField: 'dateOfBirth_d',
comparator: Facets.Sort.date,
facetType: 'year',
facetStats: true,
other: 'before',
start: 'NOW/YEAR-25YEAR',
}),
price: new Facets.Facet({
formName: 'price',
label: 'Price',
}, {
queryField: 'price_i',
comparator: Facets.Sort.number,
facetType: 'field',
facetStats: true,
}),
};
onItemChange = jasmine.createSpy('onItemChange');
f = new Facets(facets, {search: {url: '/'}});
f.on('item-change', onItemChange);
});
afterEach(function() {
jasmine.clock().uninstall();
_.now = origNow;
});
describe('initial settings', function() {
it('foo', function() {
expect(true).toBe(true);
});
});
describe('step 1', function() {
beforeEach(function() {
formData.push(f.getFacetFormData());
});
function expectItem(item, key, value, checked) {
expect(item.get('key')).toEqual(key);
expect(item.get('value')).toEqual(value);
expect(item.get('checked')).toEqual(checked);
}
describe('facets', function() {
var modelFacet, modelItems;
var yearFacet, yearItems;
var dobFacet, dobItems;
var priceFacet, priceItems;
beforeEach(function() {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
//console.log('solrData', JSON.stringify(solrResponse));
solrData.facetRanges.year = {
counts: [
'2000-01-01T00:00:00.000Z', 1,
'2001-01-01T00:00:00.000Z', 4,
'2002-01-01T00:00:00.000Z', 0,
'2003-01-01T00:00:00.000Z', 3,
'2004-01-01T00:00:00.000Z', 0,
'2005-01-01T00:00:00.000Z', 0,
'2006-01-01T00:00:00.000Z', 0,
'2007-01-01T00:00:00.000Z', 0,
'2008-01-01T00:00:00.000Z', 7,
'2009-01-01T00:00:00.000Z', 0,
'2010-01-01T00:00:00.000Z', 1,
],
};
solrData.facetRanges.dateOfBirth = {
counts: [
'1974-01-01T00:00:00.000Z', 1,
'1972-01-01T00:00:00.000Z', 1,
'1970-01-01T00:00:00.000Z', 1,
],
before: 5,
};
solrData.facetFields.model = [
'ford', 23,
'chevy', 18,
'toyota', 5,
'saturn', 7, // keep here to test the bin/exclude feature
'jeep', 19, // keep here to test the bin/exclude feature
'buick', 0, // keep here to test the bin/exclude feature
'dodge', 0,
];
solrData.facetFields.price = [
'23456', 1,
'5678', 2,
'1234', 3,
];
solrData.facetQueries['model:misc'] = 26;
solrData.facetQueries['model:startsWithB'] = 0;
solrData.statsFields.year = {min: '2000', max: '2010'};
solrData.statsFields.dateOfBirth = {min: '2000', max: '2010'};
solrData.statsFields.price = {min: '1234', max: '23456'};
f.applyFacetResults(solrResponse);
modelFacet = f.get('model');
modelItems = modelFacet.get('items');
yearFacet = f.get('year');
yearItems = yearFacet.get('items');
dobFacet = f.get('dateOfBirth');
dobItems = dobFacet.get('items');
priceFacet = f.get('price');
priceItems = priceFacet.get('items');
});
it('initial', function() {
//console.log(JSON.stringify(yearItems));
expect(yearItems.length).toBe(5);
expectItem(yearItems.at(0), '2000-01-01T00:00:00.000Z', 1, false);
expectItem(yearItems.at(1), '2001-01-01T00:00:00.000Z', 4, false);
expectItem(yearItems.at(2), '2003-01-01T00:00:00.000Z', 3, false);
expectItem(yearItems.at(3), '2008-01-01T00:00:00.000Z', 7, false);
expectItem(yearItems.at(4), '2010-01-01T00:00:00.000Z', 1, false);
expect(modelItems.length).toBe(4);
expectItem(modelItems.at(0), 'chevy', 18, false);
expectItem(modelItems.at(1), 'ford', 23, false);
expectItem(modelItems.at(2), 'misc', 26, false);
expectItem(modelItems.at(3), 'toyota', 5, false);
expect(dobItems.length).toBe(4);
expectItem(dobItems.at(0), 'before', 5, false);
expectItem(dobItems.at(1), '1970-01-01T00:00:00.000Z', 1, false);
expectItem(dobItems.at(2), '1972-01-01T00:00:00.000Z', 1, false);
expectItem(dobItems.at(3), '1974-01-01T00:00:00.000Z', 1, false);
expect(priceItems.length).toBe(3);
expectItem(priceItems.at(0), '1234', 3, false);
expectItem(priceItems.at(1), '5678', 2, false);
expectItem(priceItems.at(2), '23456', 1, false);
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).not.toHaveBeenCalled();
});
describe('click', function() {
beforeEach(function() {
modelItems.get('ford').set('checked', true);
modelItems.get('toyota').set('checked', true);
yearItems.get('2003-01-01T00:00:00.000Z').set('checked', true);
dobItems.get('before').set('checked', true);
priceFacet.set('queryMin', 1234);
priceFacet.set('queryMax', 6000);
});
it('result', function() {
expect(Backbone.ajax).not.toHaveBeenCalled();
expect(onItemChange).toHaveBeenCalled();
expect(onItemChange.calls.count()).toEqual(6);
console.log(JSON.stringify(onItemChange.calls.allArgs(), null, 1));
var facetFormData = f.getFacetFormData();
console.log(JSON.stringify(facetFormData, null, 1));
expect(facetFormData.facet).toBe(true);
expect(facetFormData.stats).toBe(true);
expect(facetFormData['facet.missing']).toBe(true);
expect(facetFormData.fq).toEqual([
"{!tag=model}model_s:(\"ford\" OR \"toyota\")",
"{!tag=year}year_d:([2003-01-01T00:00:00.000Z TO 2004-01-01T00:00:00.000Z])",
"{!tag=dateOfBirth}dateOfBirth_d:([* TO NOW/YEAR-25YEAR-1SECOND])",
"{!tag=price}price_i:([1234 TO 6000])"
]);
});
});
});
describe('suggestions', function() {
beforeEach(function() {
f.get('model').set('query', 'fo');
});
it('initial', function() {
expect(true).toBe(true);
expect(Backbone.ajax).not.toHaveBeenCalled();
});
describe('call solr', function() {
beforeEach(function() {
jasmine.clock().tick(300);
});
it('ajax called with', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
var ajaxData = Backbone.ajax.calls.argsFor(0);
//console.log(JSON.stringify(ajaxData, null, 1));
expect(ajaxData.length).toBe(1);
var arg0 = ajaxData[0];
expect(arg0.facetSuggestions).toBe(true);
expect(arg0.childFacet).toBe(f.get('model'));
var data = arg0.data;
expect(data.rows).toBe(0);
});
describe('result', function() {
var wantedSuggestions;
beforeEach(function() {
var solrResponse = createSolrResponse({stats: true});
var solrData = makeSolrResponseMixedCase(solrResponse);
//console.log('solrData', JSON.stringify(solrData));
solrData.facetFields['suggestions:model'] = [
'toyota', 5,
'ford', 23,
'chevy', 18,
'nissan', 0,
];
var ajaxData = Backbone.ajax.calls.argsFor(0);
var arg0 = ajaxData[0];
arg0.success(solrResponse, arg0);
wantedSuggestions = [
{key: 'toyota', value: 5},
{key: 'ford', value: 23},
{key: 'chevy', value: 18},
];
});
it('applied', function() {
expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
});
describe('reset suggestions', function() {
beforeEach(function() {
f.get('model').set('query', '');
});
it('foo', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
expect(f.get('model').get('suggestions')).toEqual(wantedSuggestions);
});
describe('call solr', function() {
beforeEach(function() {
jasmine.clock().tick(300);
});
it('foo', function() {
expect(Backbone.ajax).toHaveBeenCalled();
expect(Backbone.ajax.calls.count()).toBe(1);
expect(f.get('model').get('suggestions')).toEqual([]);
});
});
});
});
});
});
});
});
});
});
define(function(require) {
'use strict';
var _ = require('underscore');
var Backbone = require('backbone');
var Facets = require('solr/model/Facets');
describe('Facets', function() {
it('loads', function() {
expect(Facets).toBeDefined();
});
});
});