forked from bevacqua/dragula
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more tests, moved class manipulation to own module
- Loading branch information
Showing
7 changed files
with
143 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict'; | ||
|
||
var cache = {}; | ||
var start = '(?:^|\\s)'; | ||
var end = '(?:\\s|$)'; | ||
|
||
function lookupClass (className) { | ||
var cached = cache[className]; | ||
if (cached) { | ||
cached.lastIndex = 0; | ||
} else { | ||
cache[className] = cached = new RegExp(start + className + end, 'g'); | ||
} | ||
return cached; | ||
} | ||
|
||
function addClass (el, className) { | ||
var current = el.className; | ||
if (!current.length) { | ||
el.className = className; | ||
} else if (!lookupClass(className).test(current)) { | ||
el.className += ' ' + className; | ||
} | ||
} | ||
|
||
function rmClass (el, className) { | ||
el.className = el.className.replace(lookupClass(className), ' ').trim(); | ||
} | ||
|
||
module.exports = { | ||
add: addClass, | ||
rm: rmClass | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use strict'; | ||
|
||
var test = require('tape'); | ||
var classes = require('../classes'); | ||
|
||
test('classes exports the expected api', function (t) { | ||
t.equal(typeof classes.add, 'function', 'classes.add is a method'); | ||
t.equal(typeof classes.rm, 'function', 'classes.rm is a method'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can add a class', function (t) { | ||
var el = document.createElement('div'); | ||
classes.add(el, 'gu-foo'); | ||
t.equal(el.className, 'gu-foo', 'setting a class works'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can add a class to an element that already has classes', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'bar'; | ||
classes.add(el, 'gu-foo'); | ||
t.equal(el.className, 'bar gu-foo', 'appending a class works'); | ||
t.end(); | ||
}); | ||
|
||
test('classes.add is a no-op if class already is in element', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'gu-foo'; | ||
classes.add(el, 'gu-foo'); | ||
t.equal(el.className, 'gu-foo', 'no-op as expected'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can remove a class', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'gu-foo'; | ||
classes.rm(el, 'gu-foo'); | ||
t.equal(el.className, '', 'removing a class works'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can remove a class from a list on the right', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'bar gu-foo'; | ||
classes.rm(el, 'gu-foo'); | ||
t.equal(el.className, 'bar', 'removing a class from the list works to the right'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can remove a class from a list on the left', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'gu-foo bar'; | ||
classes.rm(el, 'gu-foo'); | ||
t.equal(el.className, 'bar', 'removing a class from the list works to the left'); | ||
t.end(); | ||
}); | ||
|
||
test('classes can remove a class from a list on the middle', function (t) { | ||
var el = document.createElement('div'); | ||
el.className = 'foo gu-foo bar'; | ||
classes.rm(el, 'gu-foo'); | ||
t.equal(el.className, 'foo bar', 'removing a class from the list works to the middle'); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
var test = require('tape'); | ||
var dragula = require('..'); | ||
|
||
test('drake has sensible default options', function (t) { | ||
var options = {}; | ||
var drake = dragula(options); | ||
t.equal(typeof options.moves, 'function', 'options.moves defaults to a method'); | ||
t.equal(typeof options.accepts, 'function', 'options.accepts defaults to a method'); | ||
t.equal(typeof options.invalid, 'function', 'options.invalid defaults to a method'); | ||
t.equal(typeof options.isContainer, 'function', 'options.isContainer defaults to a method'); | ||
t.equal(options.copy, false, 'options.copy defaults to false'); | ||
t.equal(options.revertOnSpill, false, 'options.revertOnSpill defaults to false'); | ||
t.equal(options.removeOnSpill, false, 'options.removeOnSpill defaults to false'); | ||
t.equal(options.delay, false, 'options.delay defaults to false'); | ||
t.equal(options.direction, 'vertical', 'options.direction defaults to \'vertical\''); | ||
t.end(); | ||
}); | ||
|
||
test('delay: true means 300ms', function (t) { | ||
var options = { delay: true }; | ||
var drake = dragula(options); | ||
t.equal(options.delay, 300, 'options.delay=true gets casted to 300'); | ||
t.end(); | ||
}); |