ArrayAssert = Y.ArrayAssert,
ObjectAssert = Y.ObjectAssert,
// -- Suite --------------------------------------------------------------------
// -- Lifecycle ----------------------------------------------------------------
name: 'Lifecycle',
_should: {
error: {
'Initialization should fail if `WidgetStdMod` has not been added': true,
'Initialization should fail if `WidgetStdMod` has been added after `WidgetButtons`': true
}
},
tearDown: function () {
},
'Initialization should fail if `WidgetStdMod` has not been added': function () {
this.widget = new FailWidget();
},
'Initialization should fail if `WidgetStdMod` has been added after `WidgetButtons`': function () {
this.widget = new FailWidget();
},
'`buttons` should default to an empty Object': function () {
this.widget = new TestWidget();
},
'`buttons` should be accessible within a subclass initializer': function () {
var called = 0,
initializer: function () {
called += 1;
}
});
this.widget = new SubclassWidget({
buttons: [
{
label : 'Foo',
section: 'header'
}
]
});
},
'`buttons` should be parsed from a `srcNode`': function () {
var markup = '' +
'<div>' +
'<div class="yui3-widget-hd">' +
'<span class="yui3-widget-buttons">' +
'<button class="yui3-button" name="foo">Foo</button>' +
'</span>' +
'</div>' +
'</div>';
this.widget = new TestWidget({
srcNode: '#test div'
});
},
'`buttons` parsed from a `srcNode` should inherit any defaults': function () {
BUTTONS: {
foo: {
label : 'Bar',
action: 'onFoo'
}
}
});
var markup = '' +
'<div>' +
'<div class="yui3-widget-hd">' +
'<span class="yui3-widget-buttons">' +
'<button class="yui3-button" name="foo">Foo</button>' +
'</span>' +
'</div>' +
'</div>';
called = 0;
this.widget = new PanelWidget({
srcNode: '#test div'
});
called += 1;
};
},
'`buttons` parsed from a `srcNode` should be overridden by user-provided `buttons`': function () {
var markup = '' +
'<div>' +
'<div class="yui3-widget-hd">' +
'<span class="yui3-widget-buttons">' +
'<button class="yui3-button" name="foo">Foo</button>' +
'</span>' +
'</div>' +
'</div>';
this.widget = new TestWidget({
srcNode: '#test div',
render : true,
buttons: [
]
});
Assert.areSame(1, this.widget.get('buttons.header').length, 'Header contained more than 1 button.');
},
'`defaultButton` should default to `null`': function () {
this.widget = new TestWidget();
},
'`destory()` should remove all buttons': function () {
var contentBox;
this.widget = new TestWidget({
});
}
}));
// -- Attributes and Properties ------------------------------------------------
name: 'Attributes and Properties',
tearDown: function () {
},
'`buttons` should be able to be specified as an Array of Objects': function () {
this.widget = new TestWidget({
buttons: [
]
});
},
'`buttons` should be able to be specified as an Object of Arrays of Objects': function () {
this.widget = new TestWidget({
buttons: {
}
});
},
'`buttons` specified as an Object of section-Arrays should override a button specific section': function () {
this.widget = new TestWidget({
buttons: {
header: [
{label: 'foo'},
]
}
});
Assert.areSame(2, this.widget.get('buttons.header').length, '`buttons.header` did not have 2 buttons.');
},
'`buttons` specified without a section should default to the footer': function () {
this.widget = new TestWidget({
buttons: [
{label: 'foo'},
{label: 'bar'}
]
});
Assert.areSame(2, this.widget.get('buttons.footer').length, '`buttons.footer` did not have 2 buttons.');
},
'`buttons` should be able to be specified as an Array of Y.Nodes': function () {
this.widget = new TestWidget({
buttons: [
]
});
},
'`buttons` should be able to be specified as an Object of Arrays of Y.Nodes': function () {
this.widget = new TestWidget({
buttons: {
}
});
},
'`buttons` should be able to be specified as a mixture of all possibile configurations': function () {
BUTTONS: {
foo: {
label : 'Foo',
}
}
});
this.widget = new PanelWidget({
buttons: [
{name: 'foo'},
]
});
},
'`buttons` should be settable to a new value': function () {
this.widget = new TestWidget({
render : '#test'
});
Assert.areSame('Bar', this.widget.getButton('foo').get('text'), '`foo` button did not have the label "Bar".');
},
'`buttons` should be settable to the same value': function () {
this.widget = new TestWidget({
render : '#test'
});
},
'Default `BUTTONS` should be usable by only providing their string name': function () {
BUTTONS: {
close: {
action: function () {
this.hide();
},
label : 'Close',
},
foo: {
label : 'Foo',
}
}
});
this.widget = new PanelWidget({
render : '#test',
buttons: {
header: ['close'],
footer: ['foo']
}
});
Assert.isNotNull(this.widget.getStdModNode('header').one('.yui3-button'), 'No button rendered in header.');
Assert.isNotNull(this.widget.getStdModNode('footer').one('.yui3-button'), 'No button rendered in footerButton.');
},
'User-provided `buttons` should override defaults': function () {
BUTTONS: {
close: {
action: function () {
this.hide();
},
label : 'Close',
}
}
});
var clicked = 0,
widget2 = new PanelWidget({
render : '#test',
buttons: [
{
type : 'close',
action: function () {
clicked += 1;
}
}
]
});
Assert.areSame('Close', widget2.get('buttons.header')[0].get('text'), '`widget2` does not have a "close" button in header.');
},
'A button configured with a `name` should be accessible by that `name`': function () {
this.widget = new TestWidget({
});
},
'Last button in should win when multiple `buttons` have the same `name`': function () {
var buttons;
this.widget = new TestWidget({
render : '#test',
buttons: [
]
});
Assert.areSame('Bar', this.widget.getButton('foo').get('text'), '`foo` button does not have text "Foo".');
},
'A button with an `action` String should be called': function () {
var called = 0;
this.widget = new TestWidget({
});
this.widget.doSomething = function (e) {
called += 1;
};
},
'A button configured with a `isDefault` should be the `defaultButton`': function () {
this.widget = new TestWidget({
});
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
},
'Last button in should win when multiple `buttons` are `isDefault`': function () {
this.widget = new TestWidget({
buttons: [
]
});
Assert.areSame(2, this.widget.get('buttons.footer').length, 'Widget did not have 2 footer buttons.');
Assert.areSame(this.widget.getButton('bar'), this.widget.get('defaultButton'), '`bar` is not the `defaultButton`.');
},
'`isDefault` should only be considered when it is `true` or "true" (any case)': function () {
this.widget = new TestWidget({
buttons: [
// The follow should evaluate to _not_ being the default;
]
});
Assert.areSame(9, this.widget.get('buttons.footer').length, 'Widget did not have 9 footer buttons.');
Assert.areSame(this.widget.getButton('zee'), this.widget.get('defaultButton'), '`zee` is not the `defaultButton`.');
},
'`defaultButton` should be read-only': function () {
var called = 0;
this.widget = new TestWidget({
buttons: [
{name: 'bar'}
]
});
called += 1;
});
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
},
'`defaultButton` should be updated when a new button that `isDefault` is added': function () {
var called = 0;
this.widget = new TestWidget({
after: {
defaultButtonChange: function (e) {
called += 1;
}
}
});
name : 'foo',
isDefault: true
});
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
},
'`defaultButton` should be updated when a new button that `isDefault` is added and receive visual styling': function () {
var called = 0;
this.widget = new TestWidget({
render: '#test',
after: {
defaultButtonChange: function (e) {
called += 1;
}
}
});
name : 'foo',
isDefault: true
});
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
Assert.isTrue(this.widget.get('defaultButton').hasClass(Y.WidgetButtons.CLASS_NAMES.primary), '`defaultButton` does not have primary CSS class.');
},
'`defaultButton` should be updated when a button that is the default is removed': function () {
var called = 0;
this.widget = new TestWidget({
buttons: [
{
name : 'foo',
isDefault: true
}
]
});
called += 1;
});
Assert.areSame(this.widget.getButton('foo'), this.widget.get('defaultButton'), '`foo` is not the `defaultButton`.');
},
}));
// -- Methods ------------------------------------------------------------------
name: 'Methods',
tearDown: function () {
},
'`addButton()` should add and render a new button': function () {
var called = 0,
Assert.areSame(0, this.widget.get('contentBox').all('.yui3-button').size(), 'Widget has button in header.');
value : 'Foo',
section: 'header',
action : function () {
called += 1;
}
});
Assert.areSame(1, this.widget.get('buttons.header').length, 'Widget does not have 1 button config in header.');
Assert.areSame(1, this.widget.getStdModNode('header').all('.yui3-button').size(), 'Widget does not have 1 button in header.');
value : 'Bar',
section: 'footer',
action : function () {
called += 1;
}
});
Assert.areSame(1, this.widget.get('buttons').footer.length, 'Widget does not have 1 button config in header.');
Assert.areSame(1, this.widget.getStdModNode('footer').all('.yui3-button').size(), 'Widget does not have 1 button in footer.');
},
'`addButton()` should default the new button to the end of the footer': function () {
this.widget = new TestWidget({
buttons: {
}
});
Assert.areSame('Bar', this.widget.get('buttons.footer')[1].get('text'), 'Last footer button was not "Bar".');
},
'`addButton()` without an `index` should default the new button to the end of the section': function () {
this.widget = new TestWidget({
buttons: {
}
});
Assert.areSame('Bar', this.widget.get('buttons.header')[1].get('text'), 'Last header button was not "Bar".');
},
'`addButton()` with an `index` should insert the new button at the correct location': function () {
this.widget = new TestWidget({
buttons: {
}
});
Assert.areSame('Bar', this.widget.get('buttons.header')[0].get('text'), 'Last header button was not "Bar".');
},
'`getButton()` should return a button by name': function () {
var button;
this.widget = new TestWidget({
render : '#test',
buttons: [
{name: 'foo'}
]
});
},
'`getButton()` should return a button by name for a section': function () {
this.widget = new TestWidget({
buttons: {
}
});
// Last `foo` wins.
Assert.areSame('Foo2', this.widget.getButton('foo').get('label'), 'Foo2 was not the main `foo` button.');
Assert.areSame('Foo2', this.widget.getButton('foo', 'footer').get('label'), 'Foo2 was not the `foo` footer button.');
Assert.areSame('Foo1', this.widget.getButton('foo', 'header').get('label'), 'Foo1 was not the `foo` header button.');
},
'`getButton()` should return a button by index and section': function () {
this.widget = new TestWidget({
render : '#test',
buttons: {
}
});
Assert.areSame(fooButton, this.widget.getButton(0, 'header'), '`fooButton` was not the first header button.');
Assert.areSame(barButton, this.widget.getButton(0, 'footer'), '`barButton` was not the first footer button.');
Assert.areSame(barButton, this.widget.getButton(0), '`getButton()` does not default to the footer section.');
},
'`removeButton()` should remove a button from the colleciton and the DOM': function () {
this.widget = new TestWidget({
render : '#test'
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'Widget did not have 1 rendered button.');
Assert.areSame(0, this.widget.get('contentBox').all('.yui3-button').size(), 'Widget had a rendered button.');
},
'`removeButton()` should remove a button by name for a section': function () {
this.widget = new TestWidget({
buttons: {
}
});
// Dup call on purpose.
},
'`removeButton()` should remove a button by `index` and default to the footer section': function () {
this.widget = new TestWidget({
buttons: {
footer: [
{label: 'Foo'},
{label: 'Bar'}
]
}
});
Assert.areSame('Bar', this.widget.get('buttons.footer')[0].get('text'), 'The only footer button was not "Bar".');
},
'`removeButton()` should remove a button by `index` and `section`': function () {
this.widget = new TestWidget({
buttons: {
header: [
{label: 'Foo'},
{label: 'Bar'}
]
}
});
Assert.areSame('Bar', this.widget.get('buttons.header')[0].get('text'), 'The only header button was not "Bar".');
}
}));
// -- Rendering ----------------------------------------------------------------
name: 'Rendering',
_should: {
ignore: {
'Default button should be focused on `visibleChange`': yeti
}
},
tearDown: function () {
},
'`buttons` should render their `value` as text': function () {
var button;
this.widget = new TestWidget({
render : '#test',
buttons: [
{
value : 'Foo',
section: 'header'
}
]
});
},
'`buttons` should render their `label` as text': function () {
var button;
this.widget = new TestWidget({
render : '#test',
buttons: [
{
label : 'Foo',
section: 'header'
}
]
});
},
'`buttons` should render in the correction section': function () {
var headerButton, footerButton;
this.widget = new TestWidget({
render : '#test',
buttons: [
{
value : 'Foo',
section: 'header'
},
{
value : 'Bar',
section: 'footer'
}
]
});
},
'`buttons` should move to the correct position': function () {
var markup = '' +
'<div>' +
'<div class="yui3-widget-hd">' +
'<span class="yui3-widget-buttons">' +
'<button class="yui3-button" name="foo">Foo</button>' +
'<button class="yui3-button" name="bar">Bar</button>' +
'</span>' +
'</div>' +
'</div>';
Assert.areSame(fooButton, headerButtons.get('firstChild'), '`foo` button is not the first header button.');
this.widget = new TestWidget({
srcNode: '#test div',
render : true,
buttons: {
header: [
]
}
});
Assert.areNotSame(fooButton, headerButtons.get('firstChild'), '`foo` button is the first header button.');
Assert.areSame(barButton, headerButtons.get('firstChild'), '`bar` button is the first header button.');
},
'Default button should be focused on `visibleChange`': function () {
var visibleCalled = 0,
focusCalled = 0;
this.widget = new TestWidget({
visible: false,
render : '#test',
buttons: [
{
name : 'foo',
label : 'Foo',
isDefault: true
}
]
});
visibleCalled += 1;
});
focusCalled += 1;
});
},
'Updating section content should not remove a button': function () {
this.widget = new TestWidget({
render : '#test',
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-widget-ft .yui3-button').size(), 'Footer does not contain 1 button.');
Assert.areSame('Bar', this.widget.get('contentBox').one('.yui3-widget-ft p').get('text'), 'New footer content was not added.');
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-widget-ft .yui3-button').size(), 'Footer does not contain 1 button.');
Assert.areSame('Foo', this.widget.getButton(0).get('label'), 'Button did not have the label "Foo".');
}
}));
// -- Events -------------------------------------------------------------------
name: 'Events',
tearDown: function () {
},
'`buttonsChange` should fire when setting new `buttons`': function () {
var called = 0,
this.widget = new TestWidget({
render : '#test',
buttons: {
}
});
called += 1;
});
{
value : 'Bar',
section: 'header'
}
]);
Assert.isNull(this.widget.get('contentBox').one('.yui3-widget-ft .yui3-button'), 'Footer button was not removed.');
Assert.areSame(1, this.widget.get('buttons.header').length, 'Widget header did not have a button.');
},
'`buttonsChange` should fire when calling `addButton()`': function () {
var called = 0,
this.widget = new TestWidget({
render : '#test',
buttons: {
}
});
called += 1;
});
value : 'Bar',
section: 'header'
});
Assert.areSame(button, this.widget.getStdModNode('footer').one('.yui3-button'), 'Footer button was re-created.');
Assert.areSame(1, this.widget.get('buttons.header').length, 'Widget header did not have a button.');
},
'`buttonsChange` should fire when calling `removeButton()`': function () {
var called = 0,
this.widget = new TestWidget({
render : '#test',
buttons: {
}
});
called += 1;
});
},
'`buttonsChange` should be preventable': function () {
var onCalled = 0,
afterCalled = 0,
this.widget = new TestWidget({
render : '#test',
on: {
buttonsChange: function (e) {
onCalled += 1;
e.preventDefault();
}
},
after: {
buttonsChange: function (e) {
afterCalled += 1;
}
}
});
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was not rendered.');
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was removed.');
},
'Preventing `buttonsChange` should cause `addButton()` to not have side-effects': function () {
var onCalled = 0,
afterCalled = 0,
this.widget = new TestWidget({
render : '#test',
on: {
buttonsChange: function (e) {
onCalled += 1;
e.preventDefault();
}
},
after: {
buttonsChange: function (e) {
afterCalled += 1;
}
}
});
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was not rendered.');
Assert.areSame(1, this.widget.get('buttons.footer').length, 'Footer buttons does not only have 1 button.');
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was removed.');
},
'Preventing `buttonsChange` should cause `removeButton()` to not have side-effects': function () {
var onCalled = 0,
afterCalled = 0,
this.widget = new TestWidget({
render : '#test',
buttons: [
{label: 'Foo'},
{label: 'Bar'}
],
on: {
buttonsChange: function (e) {
onCalled += 1;
e.preventDefault();
}
},
after: {
buttonsChange: function (e) {
afterCalled += 1;
}
}
});
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was not rendered.');
Assert.areSame(2, this.widget.get('buttons.footer').length, 'Footer buttons does not only have 2 buttons.');
Assert.areSame('Foo', this.widget.get('contentBox').one('.yui3-button').get('text'), 'Foo button was removed.');
},
'`contentUpdate` should fire when the buttons change in the DOM': function () {
var called = 0;
this.widget = new TestWidget({
render : '#test',
});
called += 1;
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
Assert.areSame('Foo', this.widget.get('contentBox').all('.yui3-button').item(0).get('label'), 'The button was not labeled: "Foo".');
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
Assert.areSame('Bar', this.widget.get('contentBox').all('.yui3-button').item(0).get('label'), 'The button was not labeled: "Bar".');
},
'`contentUpdate` should fire when a button is added': function () {
var called = 0;
this.widget = new TestWidget({
render : '#test',
});
called += 1;
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
Assert.areSame(2, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 2 buttons.');
},
'`contentUpdate` should fire when a button is removed': function () {
var called = 0;
this.widget = new TestWidget({
render : '#test',
});
called += 1;
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
Assert.areSame(0, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget has buttons.');
},
'`contentUpdate` should not fire when the buttons in the DOM do not actually change': function () {
var called = 0;
this.widget = new TestWidget({
render : '#test',
});
called += 1;
});
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
Assert.areSame(1, this.widget.get('contentBox').all('.yui3-button').size(), 'The widget did not have 1 button.');
}
}));
}, '@VERSION@', {
});