Skip to content

Commit 18fb8c8

Browse files
authored
feat(configure): add axeVersion property that checks compatibility of axe.version (#1793)
* feature(configure): add ver property that checks compatibility axe.version * docs * change to axeVersion * add support for canary versions * validate version
1 parent bb6591b commit 18fb8c8

File tree

4 files changed

+150
-1
lines changed

4 files changed

+150
-1
lines changed

axe.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ declare namespace axe {
124124
checks?: Check[];
125125
rules?: Rule[];
126126
locale?: Locale;
127+
axeVersion?: string;
127128
}
128129
interface Check {
129130
id: string;

doc/API.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ axe.configure({
164164
reporter: 'option',
165165
checks: [Object],
166166
rules: [Object],
167-
locale: Object
167+
locale: Object,
168+
axeVersion: String
168169
});
169170
```
170171

@@ -203,6 +204,7 @@ axe.configure({
203204
- `matches` - string(optional, default `*`). A filtering [CSS selector](./developer-guide.md#supported-css-selectors) that will exclude elements that do not match the CSS selector.
204205
- `disableOtherRules` - Disables all rules not included in the `rules` property.
205206
- `locale` - A locale object to apply (at runtime) to all rules and checks, in the same shape as `/locales/*.json`.
207+
- `axeVersion` - Set the compatible version of a custom rule with the current axe version. Compatible versions are all patch and minor updates that are the same as, or newer than those of the `axeVersion` property.
206208

207209
**Returns:** Nothing
208210

lib/core/public/configure.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ function configureChecksRulesAndBranding(spec) {
88
throw new Error('No audit configured');
99
}
1010

11+
if (spec.axeVersion) {
12+
if (!/^\d+\.\d+\.\d+(-canary)?/.test(spec.axeVersion)) {
13+
throw new Error(`Invalid configured version ${spec.axeVersion}`);
14+
}
15+
16+
let [version, canary] = spec.axeVersion.split('-');
17+
let [major, minor, patch] = version.split('.').map(Number);
18+
19+
let [axeVersion, axeCanary] = axe.version.split('-');
20+
let [axeMajor, axeMinor, axePatch] = axeVersion.split('.').map(Number);
21+
22+
if (
23+
major !== axeMajor ||
24+
axeMinor < minor ||
25+
(axeMinor === minor && axePatch < patch) ||
26+
(major === axeMajor &&
27+
minor === axeMinor &&
28+
patch === axePatch &&
29+
((canary && axeCanary) || (canary && !axeCanary)))
30+
) {
31+
throw new Error(
32+
`Configured version ${spec.axeVersion} is not compatible with current axe version ${axe.version}`
33+
);
34+
}
35+
}
36+
1137
if (
1238
spec.reporter &&
1339
(typeof spec.reporter === 'function' || reporters[spec.reporter])

test/core/public/configure.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
describe('axe.configure', function() {
33
'use strict';
44
var fixture = document.getElementById('fixture');
5+
var axeVersion = axe.version;
56

67
afterEach(function() {
78
fixture.innerHTML = '';
9+
axe.version = axeVersion;
810
});
911

1012
beforeEach(function() {
@@ -571,4 +573,122 @@ describe('axe.configure', function() {
571573
});
572574
});
573575
});
576+
577+
describe('given an axeVersion property', function() {
578+
beforeEach(function() {
579+
axe._load({});
580+
axe.version = '1.2.3';
581+
});
582+
583+
it('should not throw if version matches axe.version', function() {
584+
assert.doesNotThrow(function fn() {
585+
axe.configure({
586+
axeVersion: '1.2.3'
587+
});
588+
});
589+
});
590+
591+
it('should not throw if patch version is less than axe.version', function() {
592+
assert.doesNotThrow(function fn() {
593+
axe.configure({
594+
axeVersion: '1.2.0'
595+
});
596+
});
597+
});
598+
599+
it('should not throw if minor version is less than axe.version', function() {
600+
assert.doesNotThrow(function fn() {
601+
axe.configure({
602+
axeVersion: '1.1.9'
603+
});
604+
});
605+
});
606+
607+
it('should not throw if versions match and axe has a canary version', function() {
608+
axe.version = '1.2.3-canary.2664bae';
609+
assert.doesNotThrow(function fn() {
610+
axe.configure({
611+
axeVersion: '1.2.3'
612+
});
613+
});
614+
});
615+
616+
it('should throw if invalid version', function() {
617+
assert.throws(function fn() {
618+
axe.configure({
619+
axeVersion: '2'
620+
});
621+
}, 'Invalid configured version 2');
622+
623+
assert.throws(function fn() {
624+
axe.configure({
625+
axeVersion: '2..'
626+
});
627+
}, 'Invalid configured version 2..');
628+
});
629+
630+
it('should throw if major version is different than axe.version', function() {
631+
assert.throws(function fn() {
632+
axe.configure(
633+
{
634+
axeVersion: '2.0.0'
635+
},
636+
/^Configured version/
637+
);
638+
});
639+
assert.throws(function fn() {
640+
axe.configure(
641+
{
642+
axeVersion: '0.1.2'
643+
},
644+
/^Configured version/
645+
);
646+
});
647+
});
648+
649+
it('should throw if minor version is greater than axe.version', function() {
650+
assert.throws(function fn() {
651+
axe.configure(
652+
{
653+
axeVersion: '1.3.0'
654+
},
655+
/^Configured version/
656+
);
657+
});
658+
});
659+
660+
it('should throw if patch version is greater than axe.version', function() {
661+
assert.throws(function fn() {
662+
axe.configure(
663+
{
664+
axeVersion: '1.2.9'
665+
},
666+
/^Configured version/
667+
);
668+
});
669+
});
670+
671+
it('should throw if versions match and axeVersion has a canary version', function() {
672+
assert.throws(function fn() {
673+
axe.configure(
674+
{
675+
axeVersion: '1.2.3-canary.2664bae'
676+
},
677+
/^Configured version/
678+
);
679+
});
680+
});
681+
682+
it('should throw if versions match and both have a canary version', function() {
683+
axe.version = '1.2.3-canary.2664bae';
684+
assert.throws(function fn() {
685+
axe.configure(
686+
{
687+
axeVersion: '1.2.3-canary.a5d727c'
688+
},
689+
/^Configured version/
690+
);
691+
});
692+
});
693+
});
574694
});

0 commit comments

Comments
 (0)