Skip to content

Commit

Permalink
New tests for multiple test attributes; formatting; fix Dependabot 4;…
Browse files Browse the repository at this point in the history
… Github Actions for unit testing (#21)

* new tests for multiple testAttributes

* add Prettier config; formatting (trivial change)

* regenerate package-lock (fixes Dependabot 4)

* Github Actions for unit testing
  • Loading branch information
davidcalhoun authored Aug 20, 2023
1 parent 026f1c5 commit 662e773
Show file tree
Hide file tree
Showing 23 changed files with 260 additions and 221 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/units-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: unit testing
on:
push:
pull_request:
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
# Checks out code from Github.
- name: Checkout repo
uses: actions/checkout@v3
# Restore cache if available.
- name: Restore cached dependencies
id: dep-cache
uses: actions/cache@v3
env:
cache-name: jstoxml-cache
with:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
# Fully install from scratch when no cache is available.
- name: Install dependencies from scratch (cache miss only)
if: steps.dep-cache.outputs.cache-hit != 'true'
run: npm i
- name: Unit tests
run: npm test
shell: bash
11 changes: 11 additions & 0 deletions .prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Config for Prettier, to automatically format code. See https://prettier.io/
*/

module.exports = {
semi: true,
trailingComma: 'none',
singleQuote: true,
printWidth: 120,
tabWidth: 4
};
40 changes: 20 additions & 20 deletions lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ module.exports = {
},
defaultRuleSchema: [
{
"enum": ["always", "never"]
enum: ['always', 'never']
},
{
"type": "object",
"properties": {
"testAttribute": {
"oneOf": [
{
"type": "string",
},
{
"type": "array",
"items": {
"type": "string",
type: 'object',
properties: {
testAttribute: {
oneOf: [
{
type: 'string'
},
},
],
},
"ignoreDisabled": {
"type": "boolean"
{
type: 'array',
items: {
type: 'string'
}
}
]
},
"ignoreReadonly": {
"type": "boolean"
ignoreDisabled: {
type: 'boolean'
},
ignoreReadonly: {
type: 'boolean'
}
},
"additionalProperties": false
additionalProperties: false
}
],
errors: {
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* @fileoverview Makes sure test attributes (e.g. data-test-id) are added to interactive DOM elements.
* @author David Calhoun
*/
"use strict";
'use strict';

const requireIndex = require("requireindex");
const requireIndex = require('requireindex');

module.exports = {
rules: requireIndex(`${ __dirname }/rules`),
rules: requireIndex(`${__dirname}/rules`),
configs: {
recommended: {
parserOptions: {
Expand Down
13 changes: 3 additions & 10 deletions lib/rules/anchor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attributes on anchors.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
13 changes: 3 additions & 10 deletions lib/rules/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attributes on buttons.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -33,7 +26,7 @@ module.exports = {
}
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
13 changes: 3 additions & 10 deletions lib/rules/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attributes on inputs.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
13 changes: 3 additions & 10 deletions lib/rules/onChange.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attribute data-test-id on elements with the onChange property.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
23 changes: 7 additions & 16 deletions lib/rules/onClick.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attribute data-test-id on elements with the onClick property.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand All @@ -47,15 +40,13 @@ module.exports = {
const { nanoid } = require('nanoid');
const suggestedId = nanoid();
const namePositionEnd = node.name.range[1];
const attributeText = `${ testAttribute }="${ suggestedId }"`;
const singleTestAttribute = Array.isArray(testAttribute) ? testAttribute[0] : testAttribute;
const attributeText = `${singleTestAttribute}="${suggestedId}"`;
const start = namePositionEnd - 1;
const end = start + 1;

return fixer.insertTextAfterRange(
[start, end],
` ${ attributeText }`
);
},
return fixer.insertTextAfterRange([start, end], ` ${attributeText}`);
}
});
}
};
Expand Down
13 changes: 3 additions & 10 deletions lib/rules/onKeyDown.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attribute data-test-id on elements with the onKeyDown property.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
13 changes: 3 additions & 10 deletions lib/rules/onKeyUp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@
* @fileoverview Requires test attribute data-test-id on elements with the onKeyUp property.
* @author David Calhoun
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -25,7 +18,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
13 changes: 3 additions & 10 deletions lib/rules/onSubmit.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
/**
* @fileoverview Requires test attribute data-test-id on elements with the onSubmit property.
*/
const {
errors,
defaultRuleSchema,
defaults
} = require('../constants');
const { errors, defaultRuleSchema, defaults } = require('../constants');

const {
getError,
shouldBypass
} = require('../utils');
const { getError, shouldBypass } = require('../utils');

module.exports = {
meta: {
Expand All @@ -24,7 +17,7 @@ module.exports = {
schema: defaultRuleSchema
},

create: function(context) {
create: function (context) {
const options = context.options[1] || {};
const testAttribute = options.testAttribute || defaults.testAttribute;

Expand Down
Loading

0 comments on commit 662e773

Please sign in to comment.