1 var Filter = require('app/filter')
2 , chai = require('chai')
3 , m = require('mithril')
4 , m$ = require('mithril-jquery')
5 , mq = require('mithril-query')
6 , sinon = require('sinon')
7 , $ = require('jquery')
11 suite('Filter', function() {
13 function prep(filterClass, initialFilter) {
15 f.tested = new filterClass({attr: 'fakeAttr'});
16 f.cfSpy = sinon.stub();
17 f.cfSpy.withArgs().returns(initialFilter);
18 f.ctrl = {currentFilter: f.cfSpy};
19 f.vdom = f.tested.view(f.ctrl);
22 suite('AnyText', function() {
23 test("uses existing filter as initial input value", function() {
24 f = prep(Filter.AnyText, ['any','ilike','%quux%']);
25 c.assert.equal(mq(f.vdom).first('input').attrs.value, "quux");
27 test("calls currentFilter when input changes", function() {
28 f = prep(Filter.AnyText);
29 mq(f.vdom).setValue('input', 'qux');
30 // Should call again to set new filter value
31 s.assert.calledWith(f.cfSpy, 'any', 'ilike', '%qux%');
34 suite('ObjectType', function() {
35 test("uses existing filter value as initial label", function() {
36 f = prep(Filter.ObjectType, ['fakeAttr','is_a','arvados#collection']);
37 c.assert.lengthOf(m$('.dropdown-toggle:contains(Type)', f.vdom), 0);
38 c.assert.lengthOf(m$('.dropdown-toggle:contains(collection)', f.vdom), 1);
40 test("uses 'Type' as initial label if no current filter", function() {
41 f = prep(Filter.ObjectType, undefined);
42 c.assert.lengthOf(m$('.dropdown-toggle:contains(Type)', f.vdom), 1);
44 test("calls currentFilter when selection clicked", function() {
45 f = prep(Filter.ObjectType);
46 mq(f.vdom).click('li a[data-value="arvados#pipelineInstance"]');
47 s.assert.calledOn(f.cfSpy, f.ctrl);
48 s.assert.calledWith(f.cfSpy, 'fakeAttr', 'is_a', 'arvados#pipelineInstance');