Skip to content

Commit

Permalink
fixes #12
Browse files Browse the repository at this point in the history
preparation for 0.2.3
  • Loading branch information
ernestmarcinko committed Oct 3, 2023
1 parent e582234 commit 3f5349a
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dist/domini-core.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/domini.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "domini",
"version": "0.2.2",
"version": "0.2.3",
"description": "Minimalistic DOM manipulation tool",
"main": "dist/domini.js",
"scripts": {
Expand Down
6 changes: 4 additions & 2 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ if ( typeof window.DoMini == 'undefined' ) {
if ( typeof node != "undefined" ) {
if ( node instanceof DoMini ) {
return node.find(s);
} else if ( node instanceof Element || typeof node == 'string' ) {
} else if ( this.isValidNode(node) || typeof node == 'string' ) {
return DoMini(node).find(s);
}
} else {
Expand All @@ -49,7 +49,7 @@ if ( typeof window.DoMini == 'undefined' ) {
} else {
if ( s instanceof DoMini ) {
return s;
} else if ( s instanceof Element ) {
} else if ( this.isValidNode(s) ) {
this.push(s)
}
}
Expand All @@ -70,6 +70,8 @@ if ( typeof window.DoMini == 'undefined' ) {
return [...document.querySelectorAll(s)];
},

isValidNode: (node)=>node instanceof Element || node instanceof Document || node instanceof Window,

// This makes it act more like an array
push: Array.prototype.push,
pop: Array.prototype.pop,
Expand Down
3 changes: 2 additions & 1 deletion src/core/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ DoMini.fn.find = function (s) {
if ( typeof s == 'string' ) {
let found = [];
this.get().forEach(function(el){
found = found.concat( Array.from(el.querySelectorAll(s)) );
const matches = el.querySelectorAll?.(s) ?? [];
found = found.concat( Array.from(matches) );
});
if ( found.length > 0 ) {
newDomini.add(found);
Expand Down
5 changes: 4 additions & 1 deletion test/core/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ QUnit.module("base.js tests", function(hooks) {
let $title = $('#title'),
title = document.getElementById('title');

assert.expect(40);
assert.expect(42);

assert.equal(typeof DoMini, 'function', "typeof DoMini = 'function'");
assert.equal(typeof $title, 'object', "typeof $('#title') = 'object'");
Expand All @@ -19,6 +19,9 @@ QUnit.module("base.js tests", function(hooks) {
assert.equal($([]).length, 0, "$([]).length = 0");
assert.equal($(undefined).length, 0, "$(undefined).length = 0");

assert.equal($(document).length, 1, "$(document).length = 1");
assert.equal($(window).length, 1, "$(window).length = 1");


assert.true($title instanceof DoMini, "$('#title') instanceof DoMini");
assert.true($title instanceof Object, "$('#title') instanceof Object");
Expand Down
175 changes: 172 additions & 3 deletions test/core/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ QUnit.module("events.js tests", function(hooks) {
f.innerHTML = fixture;
});

QUnit.test('on("event") regular', function(assert) {
QUnit.test('on("event") regular Element', function(assert) {
$("#title").on('click', cb);

$("#title").trigger('click');
Expand All @@ -28,7 +28,41 @@ QUnit.module("events.js tests", function(hooks) {
assert.equal(x, 4, 'final click trigger');
});

QUnit.test('on("event") dynamic', function(assert) {
QUnit.test('on("event") regular Window', function(assert) {
$(window).on('click', cb);

$(window).trigger('click');
assert.equal(x, 1, 'click trigger 1');
$(window).trigger('click');
assert.equal(x, 2, 'click trigger 2');
$(window).off('keypress').trigger('click');
assert.equal(x, 3, 'wrong event trigger');
$(window).off('click', nocb).trigger('click');
assert.equal(x, 4, 'wrong callback off() trigger');
$(window).off('click', cb).trigger('click');
assert.equal(x, 4, 'correct callback off() trigger');
$(window).trigger('click');
assert.equal(x, 4, 'final click trigger');
});

QUnit.test('on("event") regular Document', function(assert) {
$(document).on('click', cb);

$(document).trigger('click');
assert.equal(x, 1, 'click trigger 1');
$(document).trigger('click');
assert.equal(x, 2, 'click trigger 2');
$(document).off('keypress').trigger('click');
assert.equal(x, 3, 'wrong event trigger');
$(document).off('click', nocb).trigger('click');
assert.equal(x, 4, 'wrong callback off() trigger');
$(document).off('click', cb).trigger('click');
assert.equal(x, 4, 'correct callback off() trigger');
$(document).trigger('click');
assert.equal(x, 4, 'final click trigger');
});

QUnit.test('on("event") dynamic Element', function(assert) {
// The list has 3 items
$("#list-container").on('click', 'li', cb);

Expand Down Expand Up @@ -62,7 +96,42 @@ QUnit.module("events.js tests", function(hooks) {
assert.equal(x, 0, 'All callback removal');
});

QUnit.test('on("event") multiple', function(assert) {

QUnit.test('on("event") dynamic Document', function(assert) {
// The list has 3 items
$(document).find('#list-container').on('click', 'li', cb);

// Trigger will execute 3 times, x=3
$("#list-container li").trigger('click');
assert.equal(x, 3, 'click trigger 1');

// Add 1 more list item
$("#list-container ul").append(
$("#list-container li:last-child").clone()
);

// Trigger will execute 4 times, x=4
x = 0;
$("#list-container li").trigger('click');
assert.equal(x, 4, 'click trigger 2');

// Fake callback removal
x = 0;
$(document).find('#list-container').off('click', nocb).trigger('click');
assert.equal(x, 1, 'Fake callback removal');

// Real callback removal
x = 0;
$(document).find('#list-container').off('click', cb).trigger('click');
assert.equal(x, 0, 'Real callback removal');

// All callback removal
x = 0;
$(document).find('#list-container').off('click').trigger('click');
assert.equal(x, 0, 'All callback removal');
});

QUnit.test('on("event") multiple on Element', function(assert) {
$("#title").on('click keypress keyup custom_event', cb);

$("#title").trigger('click').trigger('keyup');
Expand Down Expand Up @@ -111,4 +180,104 @@ QUnit.module("events.js tests", function(hooks) {
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'Trigger all 4, double register');
});

QUnit.test('on("event") multiple on Window', function(assert) {
$(window).on('click keypress keyup custom_event', cb);

$(window).trigger('click').trigger('keyup');
assert.equal(x, 2, 'multiple trigger 1');

x = 0;
//Trigger all
$(window)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'trigger all of registered');

x = 0;
// Fake and real trigger
$(window).trigger('fake').trigger('keyup');
assert.equal(x, 1, 'multiple trigger 1');

x = 0;
// Remove one and trigger
$(window).off('click', cb).trigger('click');
assert.equal(x, 0, 'remove and trigger same');

x = 0;
// Remove two more and trigger
$(window).off('keyup', cb).off('keypress');
$(window)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 1, 'one remaining trigger "custom_event"');

x = 0;
// Add back all & trigger all
$(window).on('click keypress keyup custom_event', cb);
$(window)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'Trigger all 4 after re-adding');

x = 0;
// Try registering multiple times the same, still should
// only register once for each event.
$(window).on('click keypress keyup custom_event', cb);
$(window).on('click keypress keyup custom_event', cb);
$(window)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'Trigger all 4, double register');
});

QUnit.test('on("event") multiple on Document', function(assert) {
$(document).on('click keypress keyup custom_event', cb);

$(document).trigger('click').trigger('keyup');
assert.equal(x, 2, 'multiple trigger 1');

x = 0;
//Trigger all
$(document)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'trigger all of registered');

x = 0;
// Fake and real trigger
$(document).trigger('fake').trigger('keyup');
assert.equal(x, 1, 'multiple trigger 1');

x = 0;
// Remove one and trigger
$(document).off('click', cb).trigger('click');
assert.equal(x, 0, 'remove and trigger same');

x = 0;
// Remove two more and trigger
$(document).off('keyup', cb).off('keypress');
$(document)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 1, 'one remaining trigger "custom_event"');

x = 0;
// Add back all & trigger all
$(document).on('click keypress keyup custom_event', cb);
$(document)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'Trigger all 4 after re-adding');

x = 0;
// Try registering multiple times the same, still should
// only register once for each event.
$(document).on('click keypress keyup custom_event', cb);
$(document).on('click keypress keyup custom_event', cb);
$(document)
.trigger('click').trigger('keypress')
.trigger('keyup').trigger('custom_event');
assert.equal(x, 4, 'Trigger all 4, double register');
});
});
7 changes: 6 additions & 1 deletion test/core/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ QUnit.module("selector.js tests", function(hooks) {
);
});

QUnit.test('.find()', function(assert) {
QUnit.test('.find() Element', function(assert) {
let nativeListLi = document.querySelectorAll("#list-container li");
assert.true($().find().length == 0, 'on empty');
assert.true($(null).find().length == 0, 'on null');
Expand Down Expand Up @@ -289,4 +289,9 @@ QUnit.module("selector.js tests", function(hooks) {
);
});
});

QUnit.test('.find() Window', function(assert) {
assert.equal($(window).find("div").length, 0, 'window does not have querySelector');
assert.equal($(window).find("li").length, 0, 'window does not have querySelector');
});
});

0 comments on commit 3f5349a

Please sign in to comment.