Skip to content

Commit 662e773

Browse files
authored
New tests for multiple test attributes; formatting; fix Dependabot 4; 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
1 parent 026f1c5 commit 662e773

23 files changed

+260
-221
lines changed

.github/workflows/units-tests.yaml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: unit testing
2+
on:
3+
push:
4+
pull_request:
5+
jobs:
6+
build-and-test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
# Checks out code from Github.
10+
- name: Checkout repo
11+
uses: actions/checkout@v3
12+
# Restore cache if available.
13+
- name: Restore cached dependencies
14+
id: dep-cache
15+
uses: actions/cache@v3
16+
env:
17+
cache-name: jstoxml-cache
18+
with:
19+
path: node_modules
20+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
21+
restore-keys: |
22+
${{ runner.os }}-build-${{ env.cache-name }}-
23+
${{ runner.os }}-build-
24+
${{ runner.os }}-
25+
# Fully install from scratch when no cache is available.
26+
- name: Install dependencies from scratch (cache miss only)
27+
if: steps.dep-cache.outputs.cache-hit != 'true'
28+
run: npm i
29+
- name: Unit tests
30+
run: npm test
31+
shell: bash

.prettierrc.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Config for Prettier, to automatically format code. See https://prettier.io/
3+
*/
4+
5+
module.exports = {
6+
semi: true,
7+
trailingComma: 'none',
8+
singleQuote: true,
9+
printWidth: 120,
10+
tabWidth: 4
11+
};

lib/constants.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@ module.exports = {
66
},
77
defaultRuleSchema: [
88
{
9-
"enum": ["always", "never"]
9+
enum: ['always', 'never']
1010
},
1111
{
12-
"type": "object",
13-
"properties": {
14-
"testAttribute": {
15-
"oneOf": [
16-
{
17-
"type": "string",
18-
},
19-
{
20-
"type": "array",
21-
"items": {
22-
"type": "string",
12+
type: 'object',
13+
properties: {
14+
testAttribute: {
15+
oneOf: [
16+
{
17+
type: 'string'
2318
},
24-
},
25-
],
26-
},
27-
"ignoreDisabled": {
28-
"type": "boolean"
19+
{
20+
type: 'array',
21+
items: {
22+
type: 'string'
23+
}
24+
}
25+
]
2926
},
30-
"ignoreReadonly": {
31-
"type": "boolean"
27+
ignoreDisabled: {
28+
type: 'boolean'
29+
},
30+
ignoreReadonly: {
31+
type: 'boolean'
3232
}
3333
},
34-
"additionalProperties": false
34+
additionalProperties: false
3535
}
3636
],
3737
errors: {

lib/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* @fileoverview Makes sure test attributes (e.g. data-test-id) are added to interactive DOM elements.
33
* @author David Calhoun
44
*/
5-
"use strict";
5+
'use strict';
66

7-
const requireIndex = require("requireindex");
7+
const requireIndex = require('requireindex');
88

99
module.exports = {
10-
rules: requireIndex(`${ __dirname }/rules`),
10+
rules: requireIndex(`${__dirname}/rules`),
1111
configs: {
1212
recommended: {
1313
parserOptions: {

lib/rules/anchor.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attributes on anchors.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -25,7 +18,7 @@ module.exports = {
2518
schema: defaultRuleSchema
2619
},
2720

28-
create: function(context) {
21+
create: function (context) {
2922
const options = context.options[1] || {};
3023
const testAttribute = options.testAttribute || defaults.testAttribute;
3124

lib/rules/button.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attributes on buttons.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -33,7 +26,7 @@ module.exports = {
3326
}
3427
},
3528

36-
create: function(context) {
29+
create: function (context) {
3730
const options = context.options[1] || {};
3831
const testAttribute = options.testAttribute || defaults.testAttribute;
3932

lib/rules/input.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attributes on inputs.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -25,7 +18,7 @@ module.exports = {
2518
schema: defaultRuleSchema
2619
},
2720

28-
create: function(context) {
21+
create: function (context) {
2922
const options = context.options[1] || {};
3023
const testAttribute = options.testAttribute || defaults.testAttribute;
3124

lib/rules/onChange.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attribute data-test-id on elements with the onChange property.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -25,7 +18,7 @@ module.exports = {
2518
schema: defaultRuleSchema
2619
},
2720

28-
create: function(context) {
21+
create: function (context) {
2922
const options = context.options[1] || {};
3023
const testAttribute = options.testAttribute || defaults.testAttribute;
3124

lib/rules/onClick.js

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attribute data-test-id on elements with the onClick property.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -25,7 +18,7 @@ module.exports = {
2518
schema: defaultRuleSchema
2619
},
2720

28-
create: function(context) {
21+
create: function (context) {
2922
const options = context.options[1] || {};
3023
const testAttribute = options.testAttribute || defaults.testAttribute;
3124

@@ -47,15 +40,13 @@ module.exports = {
4740
const { nanoid } = require('nanoid');
4841
const suggestedId = nanoid();
4942
const namePositionEnd = node.name.range[1];
50-
const attributeText = `${ testAttribute }="${ suggestedId }"`;
43+
const singleTestAttribute = Array.isArray(testAttribute) ? testAttribute[0] : testAttribute;
44+
const attributeText = `${singleTestAttribute}="${suggestedId}"`;
5145
const start = namePositionEnd - 1;
5246
const end = start + 1;
5347

54-
return fixer.insertTextAfterRange(
55-
[start, end],
56-
` ${ attributeText }`
57-
);
58-
},
48+
return fixer.insertTextAfterRange([start, end], ` ${attributeText}`);
49+
}
5950
});
6051
}
6152
};

lib/rules/onKeyDown.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
* @fileoverview Requires test attribute data-test-id on elements with the onKeyDown property.
33
* @author David Calhoun
44
*/
5-
const {
6-
errors,
7-
defaultRuleSchema,
8-
defaults
9-
} = require('../constants');
5+
const { errors, defaultRuleSchema, defaults } = require('../constants');
106

11-
const {
12-
getError,
13-
shouldBypass
14-
} = require('../utils');
7+
const { getError, shouldBypass } = require('../utils');
158

169
module.exports = {
1710
meta: {
@@ -25,7 +18,7 @@ module.exports = {
2518
schema: defaultRuleSchema
2619
},
2720

28-
create: function(context) {
21+
create: function (context) {
2922
const options = context.options[1] || {};
3023
const testAttribute = options.testAttribute || defaults.testAttribute;
3124

0 commit comments

Comments
 (0)