diff --git a/package.json b/package.json index f9dead9..b059ef4 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "benchmarks": "node ./benchmarks/run", "benchmarks-browserify": "./node_modules/.bin/browserify ./benchmarks/runInBrowser.js >./benchmarks/runInBrowser.bundle.js", "dtslint": "dtslint", - "test": "node --test ./tests/*.js" + "test": "node --test ./tests/*.mjs" }, "keywords": [ "react", diff --git a/tests/bind.js b/tests/bind.js deleted file mode 100644 index 3efe950..0000000 --- a/tests/bind.js +++ /dev/null @@ -1,165 +0,0 @@ -var { describe, it } = require('node:test'); -var assert = require('assert'); -var classNames = require('../bind'); - -var cssModulesMock = { - a: "#a", - b: "#b", - c: "#c", - d: "#d", - e: "#e", - f: "#f" -}; - -var classNamesBound = classNames.bind(cssModulesMock); - -describe('bind', function () { - describe('classNames', function () { - it('keeps object keys with truthy values', function () { - assert.equal(classNames({ - a: true, - b: false, - c: 0, - d: null, - e: undefined, - f: 1 - }), 'a f'); - }); - - it('joins arrays of class names and ignore falsy values', function () { - assert.equal(classNames('a', 0, null, undefined, true, 1, 'b'), 'a 1 b'); - }); - - it('supports heterogenous arguments', function () { - assert.equal(classNames({a: true}, 'b', 0), 'a b'); - }); - - it('should be trimmed', function () { - assert.equal(classNames('', 'b', {}, ''), 'b'); - }); - - it('returns an empty string for an empty configuration', function () { - assert.equal(classNames({}), ''); - }); - - it('supports an array of class names', function () { - assert.equal(classNames(['a', 'b']), 'a b'); - }); - - it('joins array arguments with string arguments', function () { - assert.equal(classNames(['a', 'b'], 'c'), 'a b c'); - assert.equal(classNames('c', ['a', 'b']), 'c a b'); - }); - - it('handles multiple array arguments', function () { - assert.equal(classNames(['a', 'b'], ['c', 'd']), 'a b c d'); - }); - - it('handles arrays that include falsy and true values', function () { - assert.equal(classNames(['a', 0, null, undefined, false, true, 'b']), 'a b'); - }); - - it('handles arrays that include arrays', function () { - assert.equal(classNames(['a', ['b', 'c']]), 'a b c'); - }); - - it('handles arrays that include objects', function () { - assert.equal(classNames(['a', {b: true, c: false}]), 'a b'); - }); - - it('handles deep array recursion', function () { - assert.equal(classNames(['a', ['b', ['c', {d: true}]]]), 'a b c d'); - }); - - it('handles own toString() method defined on object', function () { - assert.equal(classNames({ - toString: function () { return 'classFromMethod'; } - }), 'classFromMethod'); - }); - }); - - describe('classNamesBound', function () { - it('keeps object keys with truthy values', function () { - assert.equal(classNamesBound({ - a: true, - b: false, - c: 0, - d: null, - e: undefined, - f: 1 - }), '#a #f'); - }); - it('keeps class names undefined in bound hash', function () { - assert.equal(classNamesBound({ - a: true, - b: false, - c: 0, - d: null, - e: undefined, - f: 1, - x: true, - y: null, - z: 1 - }), '#a #f x z'); - }) - it('joins arrays of class names and ignore falsy values', function () { - assert.equal(classNamesBound('a', 0, null, undefined, true, 1, 'b'), '#a 1 #b'); - }); - - it('supports heterogenous arguments', function () { - assert.equal(classNamesBound({a: true}, 'b', 0), '#a #b'); - }); - - it('should be trimmed', function () { - assert.equal(classNamesBound('', 'b', {}, ''), '#b'); - }); - - it('returns an empty string for an empty configuration', function () { - assert.equal(classNamesBound({}), ''); - }); - - it('supports an array of class names', function () { - assert.equal(classNamesBound(['a', 'b']), '#a #b'); - }); - - it('joins array arguments with string arguments', function () { - assert.equal(classNamesBound(['a', 'b'], 'c'), '#a #b #c'); - assert.equal(classNamesBound('c', ['a', 'b']), '#c #a #b'); - }); - - it('handles multiple array arguments', function () { - assert.equal(classNamesBound(['a', 'b'], ['c', 'd']), '#a #b #c #d'); - }); - - it('handles arrays that include falsy and true values', function () { - assert.equal(classNamesBound(['a', 0, null, undefined, false, true, 'b']), '#a #b'); - }); - - it('handles arrays that include arrays', function () { - assert.equal(classNamesBound(['a', ['b', 'c']]), '#a #b #c'); - }); - - it('handles arrays that include objects', function () { - assert.equal(classNamesBound(['a', {b: true, c: false}]), '#a #b'); - }); - - it('handles deep array recursion', function () { - assert.equal(classNamesBound(['a', ['b', ['c', {d: true}]]]), '#a #b #c #d'); - }); - - it('handles own toString() method defined on object', function () { - assert.equal(classNamesBound({ - toString: function () { return 'classFromMethod'; } - }), 'classFromMethod'); - }); - - it('handles toString() method defined inherited in object', function () { - var Class1 = function() {}; - var Class2 = function() {}; - Class1.prototype.toString = function() { return 'classFromMethod'; } - Class2.prototype = Object.create(Class1.prototype); - - assert.equal(classNamesBound(new Class2()), 'classFromMethod'); - }); - }); -}) diff --git a/tests/bind.mjs b/tests/bind.mjs new file mode 100644 index 0000000..cf5b84b --- /dev/null +++ b/tests/bind.mjs @@ -0,0 +1,165 @@ +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import classNames from '../bind.js'; + +const cssModulesMock = { + a: '#a', + b: '#b', + c: '#c', + d: '#d', + e: '#e', + f: '#f' +}; + +const classNamesBound = classNames.bind(cssModulesMock); + +describe('bind', () => { + describe('classNames', () => { + it('keeps object keys with truthy values', () => { + assert.equal(classNames({ + a: true, + b: false, + c: 0, + d: null, + e: undefined, + f: 1 + }), 'a f'); + }); + + it('joins arrays of class names and ignore falsy values', () => { + assert.equal(classNames('a', 0, null, undefined, true, 1, 'b'), 'a 1 b'); + }); + + it('supports heterogenous arguments', () => { + assert.equal(classNames({a: true}, 'b', 0), 'a b'); + }); + + it('should be trimmed', () => { + assert.equal(classNames('', 'b', {}, ''), 'b'); + }); + + it('returns an empty string for an empty configuration', () => { + assert.equal(classNames({}), ''); + }); + + it('supports an array of class names', () => { + assert.equal(classNames(['a', 'b']), 'a b'); + }); + + it('joins array arguments with string arguments', () => { + assert.equal(classNames(['a', 'b'], 'c'), 'a b c'); + assert.equal(classNames('c', ['a', 'b']), 'c a b'); + }); + + it('handles multiple array arguments', () => { + assert.equal(classNames(['a', 'b'], ['c', 'd']), 'a b c d'); + }); + + it('handles arrays that include falsy and true values', () => { + assert.equal(classNames(['a', 0, null, undefined, false, true, 'b']), 'a b'); + }); + + it('handles arrays that include arrays', () => { + assert.equal(classNames(['a', ['b', 'c']]), 'a b c'); + }); + + it('handles arrays that include objects', () => { + assert.equal(classNames(['a', {b: true, c: false}]), 'a b'); + }); + + it('handles deep array recursion', () => { + assert.equal(classNames(['a', ['b', ['c', {d: true}]]]), 'a b c d'); + }); + + it('handles own toString() method defined on object', () => { + assert.equal(classNames({ + toString: () => { return 'classFromMethod'; } + }), 'classFromMethod'); + }); + }); + + describe('classNamesBound', () => { + it('keeps object keys with truthy values', () => { + assert.equal(classNamesBound({ + a: true, + b: false, + c: 0, + d: null, + e: undefined, + f: 1 + }), '#a #f'); + }); + + it('keeps class names undefined in bound hash', () => { + assert.equal(classNamesBound({ + a: true, + b: false, + c: 0, + d: null, + e: undefined, + f: 1, + x: true, + y: null, + z: 1 + }), '#a #f x z'); + }); + + it('joins arrays of class names and ignore falsy values', () => { + assert.equal(classNamesBound('a', 0, null, undefined, true, 1, 'b'), '#a 1 #b'); + }); + + it('supports heterogenous arguments', () => { + assert.equal(classNamesBound({a: true}, 'b', 0), '#a #b'); + }); + + it('should be trimmed', () => { + assert.equal(classNamesBound('', 'b', {}, ''), '#b'); + }); + + it('returns an empty string for an empty configuration', () => { + assert.equal(classNamesBound({}), ''); + }); + + it('supports an array of class names', () => { + assert.equal(classNamesBound(['a', 'b']), '#a #b'); + }); + + it('joins array arguments with string arguments', () => { + assert.equal(classNamesBound(['a', 'b'], 'c'), '#a #b #c'); + assert.equal(classNamesBound('c', ['a', 'b']), '#c #a #b'); + }); + + it('handles multiple array arguments', () => { + assert.equal(classNamesBound(['a', 'b'], ['c', 'd']), '#a #b #c #d'); + }); + + it('handles arrays that include falsy and true values', () => { + assert.equal(classNamesBound(['a', 0, null, undefined, false, true, 'b']), '#a #b'); + }); + + it('handles arrays that include arrays', () => { + assert.equal(classNamesBound(['a', ['b', 'c']]), '#a #b #c'); + }); + + it('handles arrays that include objects', () => { + assert.equal(classNamesBound(['a', {b: true, c: false}]), '#a #b'); + }); + + it('handles deep array recursion', () => { + assert.equal(classNamesBound(['a', ['b', ['c', {d: true}]]]), '#a #b #c #d'); + }); + + it('handles own toString() method defined on object', () => { + assert.equal(classNamesBound({ + toString: () => { return 'classFromMethod'; } + }), 'classFromMethod'); + }); + + it('handles toString() method defined inherited in object', () => { + class Class1 { toString() { return 'classFromMethod'; } } + class Class2 extends Class1 {} + + assert.equal(classNamesBound(new Class2()), 'classFromMethod'); + }); + }); +}) diff --git a/tests/dedupe.js b/tests/dedupe.mjs similarity index 55% rename from tests/dedupe.js rename to tests/dedupe.mjs index 78c3a6a..ac7de52 100644 --- a/tests/dedupe.js +++ b/tests/dedupe.mjs @@ -1,9 +1,9 @@ -var { describe, it } = require('node:test'); -var assert = require('assert'); -var dedupe = require('../dedupe'); +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import dedupe from '../dedupe.js'; -describe('dedupe', function () { - it('keeps object keys with truthy values', function () { +describe('dedupe', () => { + it('keeps object keys with truthy values', () => { assert.equal(dedupe({ a: true, b: false, @@ -14,77 +14,75 @@ describe('dedupe', function () { }), 'a f'); }); - it('should dedupe dedupe', function () { + it('should dedupe', () => { assert.equal(dedupe('foo', 'bar', 'foo', 'bar', { foo: true }), 'foo bar'); }); - it('should make sure subsequent objects can remove/add classes', function () { + it('should make sure subsequent objects can remove/add classes', () => { assert.equal(dedupe('foo', { foo: false }, { foo: true, bar: true }), 'foo bar'); }); - it('should make sure object with falsy value wipe out previous classes', function () { + it('should make sure object with falsy value wipe out previous classes', () => { assert.equal(dedupe('foo foo', 0, null, undefined, true, 1, 'b', { 'foo': false }), '1 b'); assert.equal(dedupe('foo', 'foobar', 'bar', { foo: false }), 'foobar bar'); assert.equal(dedupe('foo', 'foo-bar', 'bar', { foo: false }), 'foo-bar bar'); assert.equal(dedupe('foo', '-moz-foo-bar', 'bar', { foo: false }), '-moz-foo-bar bar'); }); - it('joins arrays of class names and ignore falsy values', function () { + it('joins arrays of class names and ignore falsy values', () => { assert.equal(dedupe('a', 0, null, undefined, true, 1, 'b'), '1 a b'); }); - it('supports heterogenous arguments', function () { + it('supports heterogenous arguments', () => { assert.equal(dedupe({a: true}, 'b', 0), 'a b'); }); - it('should be trimmed', function () { + it('should be trimmed', () => { assert.equal(dedupe('', 'b', {}, ''), 'b'); }); - it('returns an empty string for an empty configuration', function () { + it('returns an empty string for an empty configuration', () => { assert.equal(dedupe({}), ''); }); - it('supports an array of class names', function () { + it('supports an array of class names', () => { assert.equal(dedupe(['a', 'b']), 'a b'); }); - it('joins array arguments with string arguments', function () { + it('joins array arguments with string arguments', () => { assert.equal(dedupe(['a', 'b'], 'c'), 'a b c'); assert.equal(dedupe('c', ['a', 'b']), 'c a b'); }); - it('handles multiple array arguments', function () { + it('handles multiple array arguments', () => { assert.equal(dedupe(['a', 'b'], ['c', 'd']), 'a b c d'); }); - it('handles arrays that include falsy and true values', function () { + it('handles arrays that include falsy and true values', () => { assert.equal(dedupe(['a', 0, null, undefined, false, true, 'b']), 'a b'); }); - it('handles arrays that include arrays', function () { + it('handles arrays that include arrays', () => { assert.equal(dedupe(['a', ['b', 'c']]), 'a b c'); }); - it('handles arrays that include objects', function () { + it('handles arrays that include objects', () => { assert.equal(dedupe(['a', {b: true, c: false}]), 'a b'); }); - it('handles deep array recursion', function () { + it('handles deep array recursion', () => { assert.equal(dedupe(['a', ['b', ['c', {d: true}]]]), 'a b c d'); }); - it('handles own toString() method defined on object', function () { + it('handles own toString() method defined on object', () => { assert.equal(dedupe({ - toString: function () { return 'classFromMethod'; } + toString: () => { return 'classFromMethod'; } }), 'classFromMethod'); }); - it('handles toString() method defined inherited in object', function () { - var Class1 = function() {}; - var Class2 = function() {}; - Class1.prototype.toString = function() { return 'classFromMethod'; } - Class2.prototype = Object.create(Class1.prototype); + it('handles toString() method defined inherited in object', () => { + class Class1 { toString() { return 'classFromMethod'; } } + class Class2 extends Class1 {} assert.equal(dedupe(new Class2()), 'classFromMethod'); }); diff --git a/tests/index.js b/tests/index.mjs similarity index 52% rename from tests/index.js rename to tests/index.mjs index 71e0690..260b32c 100644 --- a/tests/index.js +++ b/tests/index.mjs @@ -1,10 +1,10 @@ -var { describe, it } = require('node:test'); -var assert = require('assert'); -var vm = require('vm'); -var classNames = require('../'); +import { describe, it } from 'node:test'; +import assert from 'node:assert/strict'; +import vm from 'node:vm'; +import classNames from '../index.js'; -describe('classNames', function () { - it('keeps object keys with truthy values', function () { +describe('classNames', () => { + it('keeps object keys with truthy values', () => { assert.equal(classNames({ a: true, b: false, @@ -15,64 +15,64 @@ describe('classNames', function () { }), 'a f'); }); - it('joins arrays of class names and ignore falsy values', function () { + it('joins arrays of class names and ignore falsy values', () => { assert.equal(classNames('a', 0, null, undefined, true, 1, 'b'), 'a 1 b'); }); - it('supports heterogenous arguments', function () { + it('supports heterogenous arguments', () => { assert.equal(classNames({a: true}, 'b', 0), 'a b'); }); - it('should be trimmed', function () { + it('should be trimmed', () => { assert.equal(classNames('', 'b', {}, ''), 'b'); }); - it('returns an empty string for an empty configuration', function () { + it('returns an empty string for an empty configuration', () => { assert.equal(classNames({}), ''); }); - it('supports an array of class names', function () { + it('supports an array of class names', () => { assert.equal(classNames(['a', 'b']), 'a b'); }); - it('joins array arguments with string arguments', function () { + it('joins array arguments with string arguments', () => { assert.equal(classNames(['a', 'b'], 'c'), 'a b c'); assert.equal(classNames('c', ['a', 'b']), 'c a b'); }); - it('handles multiple array arguments', function () { + it('handles multiple array arguments', () => { assert.equal(classNames(['a', 'b'], ['c', 'd']), 'a b c d'); }); - it('handles arrays that include falsy and true values', function () { + it('handles arrays that include falsy and true values', () => { assert.equal(classNames(['a', 0, null, undefined, false, true, 'b']), 'a b'); }); - it('handles arrays that include arrays', function () { + it('handles arrays that include arrays', () => { assert.equal(classNames(['a', ['b', 'c']]), 'a b c'); }); - it('handles arrays that include objects', function () { + it('handles arrays that include objects', () => { assert.equal(classNames(['a', {b: true, c: false}]), 'a b'); }); - it('handles deep array recursion', function () { + it('handles deep array recursion', () => { assert.equal(classNames(['a', ['b', ['c', {d: true}]]]), 'a b c d'); }); - it('handles arrays that are empty', function () { + it('handles arrays that are empty', () => { assert.equal(classNames('a', []), 'a'); }); - it('handles nested arrays that have empty nested arrays', function () { + it('handles nested arrays that have empty nested arrays', () => { assert.equal(classNames('a', [[]]), 'a'); }); - it('handles all types of truthy and falsy property values as expected', function () { + it('handles all types of truthy and falsy property values as expected', () => { assert.equal(classNames({ // falsy: null: null, - emptyString: "", + emptyString: '', noNumber: NaN, zero: 0, negativeZero: -0, @@ -80,7 +80,7 @@ describe('classNames', function () { undefined: undefined, // truthy (literally anything else): - nonEmptyString: "foobar", + nonEmptyString: 'foobar', whitespace: ' ', function: Object.prototype.toString, emptyObject: {}, @@ -91,26 +91,24 @@ describe('classNames', function () { }), 'nonEmptyString whitespace function emptyObject nonEmptyObject emptyList nonEmptyList greaterZero'); }); - it('handles toString() method defined on object', function () { + it('handles toString() method defined on object', () => { assert.equal(classNames({ - toString: function () { return 'classFromMethod'; } + toString: () => { return 'classFromMethod'; } }), 'classFromMethod'); }); - it('handles toString() method defined inherited in object', function () { - var Class1 = function() {}; - var Class2 = function() {}; - Class1.prototype.toString = function() { return 'classFromMethod'; } - Class2.prototype = Object.create(Class1.prototype); + it('handles toString() method defined inherited in object', () => { + class Class1 { toString() { return 'classFromMethod'; } } + class Class2 extends Class1 {} assert.equal(classNames(new Class2()), 'classFromMethod'); }); - it('handles objects in a VM', function () { - var context = { classNames, output: undefined }; + it('handles objects in a VM', () => { + const context = { classNames, output: undefined }; vm.createContext(context); - var code = 'output = classNames({ a: true, b: true });'; + const code = 'output = classNames({ a: true, b: true });'; vm.runInContext(code, context); assert.equal(context.output, 'a b');