You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
7.2 KiB

11 years ago
(function () {
module('Ply');
var layer;
11 years ago
function hasParent(el) {
return !!(el && el.parentNode && el.parentNode.nodeType !== 11);
}
11 years ago
test('core', function () {
equal(typeof Ply, 'function');
ok(new Ply instanceof Ply);
});
function sleep(fn, ms) {
return Ply.promise(function (resolve) {
setTimeout(function () {
fn();
resolve();
}, ms);
});
}
function checkVisiblity(layer, state, msg) {
equal(!!layer.visible, state, '[' + msg + '] visible: ' + state);
11 years ago
equal(hasParent(layer.wrapEl.parentNode), state, msg + ' -> parentNode is ' + (state ? '' : 'not ') + 'exists');
11 years ago
}
test('options', function () {
layer = new Ply();
for (var key in Ply.defaults.overlay) {
11 years ago
var val = layer.overlayBoxEl.style[key] + '';
val = val.replace(/,\s*/g, ', ');
equal(val, Ply.defaults.overlay[key], key);
11 years ago
}
equal(layer.bodyEl, document.body, 'body');
// Body
layer = new Ply({ body: '#playground' });
equal(layer.bodyEl, playground, '#playground');
// Overlay 1
layer = new Ply({ overlay: { opacity: 1 } });
equal(layer.overlayBoxEl.style.opacity, 1, 'opacity: 1');
equal(layer.overlayBoxEl.style.backgroundColor, "", 'backgroundColor: ""');
11 years ago
// Overlay 2
layer = new Ply({ overlay: { opacity: 1, backgroundColor: 'rgb(255, 0, 0)' } });
equal(layer.overlayBoxEl.style.opacity, 1, 'opacity: 1');
11 years ago
equal(layer.overlayBoxEl.style.backgroundColor.replace(/,\s*/g, ', '), 'rgb(255, 0, 0)', 'backgroundColor: red');
11 years ago
// Overlay 3
layer = new Ply({ overlay: null });
notEqual(layer.overlayBoxEl.style.position, 'fixed', 'overlay: null');
11 years ago
// Layer
layer = new Ply({ layer: { textAlign: 'center' } });
11 years ago
equal(layer.layerEl.firstChild.style.textAlign, 'center', 'textAlign: center');
11 years ago
});
test('flags', function () {
layer = new Ply();
for (var key in Ply.defaults.flags) {
equal(layer.options.flags[key], Ply.defaults.flags[key], key);
}
layer = new Ply({ flags: { bodyScroll: true } });
for (var key in Ply.defaults.flags) {
equal(layer.options.flags[key], key == 'bodyScroll' ? true : Ply.defaults.flags[key], key);
}
});
test('content', function () {
var content = document.createElement('b');
11 years ago
content.innerHTML = '<b>!</b>';
11 years ago
11 years ago
equalHtml(new Ply('').contentEl.innerHTML, '', 'conentEl');
equalHtml(new Ply('Wow!').contentEl.innerHTML, 'Wow!', 'contentEl');
equalHtml(new Ply(content).contentEl.innerHTML, '<b>!</b>');
11 years ago
equalHtml(new Ply({ el: 'Wow!' }).contentEl.innerHTML, 'Wow!', 'contentEl');
equalHtml(new Ply({ el: content }).contentEl.innerHTML, '<b>!</b>');
11 years ago
});
11 years ago
promiseTest('open-close', function () {
11 years ago
layer = new Ply({ el: 'Wow!' });
11 years ago
ok(!hasParent(layer.wrapEl), '!parent - open');
11 years ago
ok(!layer.visible, 'visible: false');
return layer.open().then(function () {
var ratio = layer.wrapEl.offsetHeight / (layer.layerEl.offsetTop + layer.layerEl.offsetHeight/2);
11 years ago
ok(hasParent(layer.wrapEl), 'parent - open');
11 years ago
ok(layer.visible, 'visible: true');
ok(layer.wrapEl.offsetWidth > 0, 'offsetWidth > 0');
ok(Math.abs(2 - ratio) < 0.1, ratio, 'delat(' + ratio + ') < 0.1');
layer.close().then(function () {
ok(!layer.visible, 'visible: false - close');
11 years ago
ok(!hasParent(layer.wrapEl), '!parent - close');
11 years ago
});
});
});
promiseTest('closeByEsc', function () {
function open(msg, esc) {
11 years ago
var layer = new Ply({ el: msg, flags: { closeByEsc: esc }, effect: { duration: 1 } });
11 years ago
checkVisiblity(layer, false, msg);
return layer.open().then(function () {
checkVisiblity(layer, true, msg);
return layer;
});
}
return open('1. esc: true', true).then(function (escTrue) {
return open('2. esc: false', false).then(function (escFalse) {
simulateEvent(document, 'keyup', { keyCode: Ply.keys.esc });
checkVisiblity(escTrue, true, '3. esc: true');
checkVisiblity(escFalse, true, '4. esc: false');
return escFalse.close().then(function () {
checkVisiblity(escTrue, true, '5. esc: true');
checkVisiblity(escFalse, false, '6. esc: false');
simulateEvent(document, 'keyup', { keyCode: Ply.keys.esc });
return sleep(function () {
checkVisiblity(escTrue, false, '7. esc: true');
checkVisiblity(escFalse, false, '8. esc: false');
}, 50);
});
});
});
});
promiseTest('closeByOverlay', function () {
11 years ago
function testMe(msg, state, callback) {
var layer = new Ply({ el: msg, flags: { closeByOverlay: state }, effect: { duration: 1 } });
11 years ago
return layer.open().then(function () {
checkVisiblity(layer, true, msg);
simulateEvent(layer.overlayEl, 'click');
11 years ago
return sleep($.noop, 50).then(function () {
return callback(layer, msg);
});
11 years ago
});
}
11 years ago
return testMe('closeByOverlay: true', true, function (layer, msg) {
11 years ago
checkVisiblity(layer, false, msg);
11 years ago
return testMe('closeByOverlay: false', false, function (layer, msg) {
11 years ago
checkVisiblity(layer, true, msg);
return layer.close();
});
})
});
promiseTest('swap', function () {
return new Ply({ el: 1, effect: 'none:1' }).open().then(function (layer) {
return layer.swap({ el: 2 }).then(function () {
equal(layer.contentEl.innerHTML, 2);
return layer.close().then(function () {
return layer.swap({ el: 3 }, 'none:2').then(function () {
equal(layer.contentEl.innerHTML, 3);
});
});
});
});
});
11 years ago
promiseTest('destroy', function () {
11 years ago
return new Ply({ effect: 'none:1' }).open().then(function (layer) {
checkVisiblity(layer, true, '#1');
layer.destroy();
checkVisiblity(layer, false, '#2');
});
});
11 years ago
promiseTest('on-off', function () {
11 years ago
var log = [],
11 years ago
11 years ago
logMe = function (prefix, evt, el) {
log.push(prefix + ':' + el.tagName + '.' + evt.type + '->' + el.getAttribute('data-ply'));
},
11 years ago
11 years ago
barHandle = function (evt, el) {
log.pop();
logMe('bar', evt, el);
}
;
11 years ago
return new Ply('<i data-ply="foo"><em>foo</em></i><b data-ply="bar"><em>bar</em></b>').open().then(function (layer) {
11 years ago
layer.on('click', function (evt, el) {
logMe('layer', evt, el);
});
layer.on('click', 'foo', function (evt, el) {
logMe('foo', evt, el);
});
layer.on('click', 'bar', barHandle);
simulateEvent(layer.contentEl.getElementsByTagName('em')[0], 'click');
simulateEvent(layer.contentEl.getElementsByTagName('em')[1], 'click');
layer.off('click', barHandle);
simulateEvent(layer.contentEl.getElementsByTagName('em')[1], 'click');
layer.off('click', 'bar', barHandle);
simulateEvent(layer.contentEl.getElementsByTagName('em')[1], 'click');
11 years ago
var tmp, expected = [
'layer:DIV.click->:layer',
11 years ago
'foo:I.click->foo',
'bar:B.click->bar',
'bar:B.click->bar',
'layer:DIV.click->:layer'
11 years ago
];
tmp = expected.slice();
$.each(log, function (i, entry) {
i = $.inArray(entry, tmp);
ok(i > -1, 'log.'+entry);
tmp.splice(i, 1);
});
tmp = log.slice();
$.each(expected, function (i, entry) {
i = $.inArray(entry, tmp);
ok(i > -1, 'expected.'+entry);
tmp.splice(i, 1);
});
11 years ago
return layer.close();
});
});
// if (/state=2/.test(location)) {
// promiseTest('Promise', function () {
// expect(0);
//
// return Ply.promise(function (resolve) {
// window.resolveTest = function () {
// resolve();
// };
// document.write('<iframe src="?state=2"></iframe>');
// });
// });
// }
})();