Skip to content

Commit

Permalink
test: Add testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan DiMascio committed Jul 18, 2020
1 parent f6f9e1c commit 2d8b317
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .ava.browser.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/no-anonymous-default-export */

export default {
files: ["src/__tests__/index.test.js"],
verbose: true,
ignoredByWatcher: ["{coverage,examples/**"],
require: ["esm", "./src/__tests__/_setup-browser-env.js"],
nodeArguments: ["--experimental-modules"],
};
9 changes: 9 additions & 0 deletions .ava.node.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable import/no-anonymous-default-export */

export default {
files: ["src/__tests__/index.node.test.js"],
verbose: true,
ignoredByWatcher: ["{coverage,examples/**"],
require: ["esm"],
nodeArguments: ["--experimental-modules"],
};
11 changes: 0 additions & 11 deletions ava.config.js

This file was deleted.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
"all": "npm-run-all --sequential build lint cover",
"lint": "xo",
"lint:fix": "xo --fix",
"test": "ava",
"cover": "nyc npm run test",
"test:node": "ava --config .ava.node.config.js",
"test:browser": "ava --config .ava.browser.config.js",
"cover": "npm-run-all --sequential cover:browser cover:node cover:report",
"cover:all": "npm-run-all --sequential cover:browser cover:node",
"cover:node": "nyc --no-clean npm run test:node",
"cover:browser": "nyc npm run test:browser",
"cover:report": "nyc report --reporter=text-lcov | coveralls",
"build": "babel --plugins '@babel/plugin-transform-modules-umd' src --out-file ./dist/index.js --no-comments"
},
"browserslist": [
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/_setup-browser-env.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const browserEnv = require('browser-env');
browserEnv(['window', 'document']);
browserEnv(['window', 'document', 'DOMParser']);
25 changes: 25 additions & 0 deletions src/__tests__/index.node.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import test from 'ava';
import getAttributes from '..';

test('`getAttributes.parse()` parses attributes from a string', t => {
const string = '<div id="ava" data-brand-icon="ava" data-brand-color="dark"></div>';
const attrs = getAttributes.parse(string);

t.deepEqual(attrs, {
'data-brand-icon': 'ava',
'data-brand-color': 'dark',
id: 'ava'
});
});

test('`getAttributes.parse()` handles empty attributes from a string', t => {
const string = '<div id="ava" empty-attribute data-brand-icon="ava" data-brand-color="dark"></div>';
const attrs = getAttributes.parse(string);

t.deepEqual(attrs, {
'data-brand-icon': 'ava',
'data-brand-color': 'dark',
'empty-attribute': '',
id: 'ava'
});
});
85 changes: 85 additions & 0 deletions src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* eslint-disable no-undef */

import test from 'ava';
import getAttributes from '..';

test('`getAttributes.parse()` parses attributes from a DOM element', t => {
const div = document.createElement('div');
div.dataset.brandIcon = 'ava';
div.dataset.brandColor = 'dark';
div.id = 'ava';

document.body.append(div);

const attrs = getAttributes.parse(document.querySelector('#ava'));

t.deepEqual(attrs, {
'data-brand-icon': 'ava',
'data-brand-color': 'dark',
id: 'ava'
});
});

test('`getAttributes.parse()` parses attributes from a string', t => {
const string = '<div id="ava" data-brand-icon="ava" data-brand-color="dark"></div>';
const attrs = getAttributes.parse(string);

t.deepEqual(attrs, {
'data-brand-icon': 'ava',
'data-brand-color': 'dark',
id: 'ava'
});
});

test('`getAttributes.parse()` fails when given an empty string', t => {
const error = t.throws(() => {
getAttributes.parse('');
}, {instanceOf: ReferenceError});

t.is(error.message, 'The string given to `getAttributes.parse()` is empty.');
});

test('`getAttributes.parse()` fails when given a type other than string or node', t => {
const types = [[], {}, true, 3];

// Two assertions per item: `t.throws()` and `t.is()`.
t.plan(types.length * 2);

types.forEach(type => {
const error = t.throws(() => {
getAttributes.parse(type);
}, {instanceOf: TypeError});

t.is(error.message, '`getAttributes.parse()` only accepts strings and nodes. An ' + typeof type + ' was given.');
});
});

test('`getAttributes.stringify()` returns a string when given a string', t => {
const attrString = getAttributes.stringify('Wow cool test');

t.is(attrString, 'Wow cool test');
});

test('`getAttributes.stringify()` returns a string when given array', t => {
const arrayToString = getAttributes.stringify(['Wow', 'cool', 'test']);

t.is(arrayToString, 'Wow cool test');
});

test('`getAttributes.stringify()` returns a string when given object', t => {
const objectToString = getAttributes.stringify({
'data-brand-icon': 'ava',
'data-brand-color': 'dark',
'empty-attribute': ''
});

t.is(objectToString, 'data-brand-icon="ava" data-brand-color="dark" empty-attribute');
});

test('`getAttributes.stringify()` fails when given an empty string', t => {
const error = t.throws(() => {
getAttributes.stringify('');
}, {instanceOf: ReferenceError});

t.is(error.message, 'Cannot stringify undefined.');
});

0 comments on commit 2d8b317

Please sign in to comment.