-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add accessibility tests to templates
- Loading branch information
1 parent
47c02f0
commit f58f4ae
Showing
110 changed files
with
2,018 additions
and
538 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"presets": [ | ||
"@babel/preset-typescript", | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"node": "current" | ||
} | ||
} | ||
], | ||
"@babel/preset-react" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dist | ||
test/fixtures/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Atanas Stoyanov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Table of contents | ||
|
||
# Overview | ||
|
||
jest accessibility matching utilities with deque axe. | ||
|
||
# Installation | ||
|
||
This package is usually installed as part of the @component-controls package, but you can also install it standalone: | ||
|
||
```bash | ||
$ npm install @component-controls/jest-axe-matcher --save-dev | ||
``` | ||
|
||
# API | ||
|
||
<react-docgen-typescript path="./src" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
{ | ||
"name": "@component-controls/jest-axe-matcher", | ||
"version": "3.7.0", | ||
"description": "jest accessibility matching utilities with deque axe", | ||
"keywords": [ | ||
"jest", | ||
"axe testing", | ||
"deque" | ||
], | ||
"main": "dist/index.js", | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
"dist/", | ||
"package.json", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"build": "yarn cross-env NODE_ENV=production rollup -c", | ||
"dev": "yarn cross-env NODE_ENV=development rollup -cw", | ||
"docs": "ts-md", | ||
"fix": "yarn lint --fix", | ||
"lint": "yarn eslint . --ext mdx,ts,tsx", | ||
"prepare": "yarn build", | ||
"test": "yarn jest" | ||
}, | ||
"homepage": "https://github.com/ccontrols/component-controls", | ||
"bugs": { | ||
"url": "https://github.com/ccontrols/component-controls/issues" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/ccontrols/component-controls.git", | ||
"directory": "misc/jest-axe-matcher" | ||
}, | ||
"license": "MIT", | ||
"dependencies": { | ||
"axe-core": "^4.1.1", | ||
"jest": "^26.4.2", | ||
"jest-matcher-utils": "^26.6.2", | ||
"ts-jest": "^26.5.5" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.10", | ||
"typescript": "^4.0.5" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"authors": [ | ||
"Atanas Stoyanov" | ||
], | ||
"jest": { | ||
"roots": [ | ||
"./test" | ||
] | ||
}, | ||
"gitHead": "e30d62f101e104711a16261fce04785d58cac1eb" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { config } from '../../rollup-config'; | ||
|
||
export default config({ | ||
input: ['./src/index.ts'], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
import * as extensions from './matchers'; | ||
import { ResultFilers } from './utils'; | ||
|
||
export { extensions }; | ||
|
||
expect.extend(extensions); | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R> { | ||
toHaveNoAxeViolations(filters?: ResultFilers): R; | ||
toHaveNoAxeIncomplete(filters?: ResultFilers): R; | ||
toHaveNoAxeInapplicable(filters?: ResultFilers): R; | ||
toHaveNoAxePasses(filters?: ResultFilers): R; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export { toHaveNoAxeViolations } from './toHaveNoAxeViolations'; | ||
export { toHaveNoAxeIncomplete } from './toHaveNoAxeIncomplete'; | ||
export { toHaveNoAxeInapplicable } from './toHaveNoAxeInapplicable'; | ||
export { toHaveNoAxePasses } from './toHaveNoAxePasses'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AxeResults } from 'axe-core'; | ||
import { matchAxeResuls, ResultFilers } from './utils'; | ||
|
||
export const toHaveNoAxeInapplicable = ( | ||
received: AxeResults, | ||
filters?: ResultFilers, | ||
): jest.CustomMatcherResult => | ||
matchAxeResuls(received.inapplicable, 'inapplicable', filters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AxeResults } from 'axe-core'; | ||
import { matchAxeResuls, ResultFilers } from './utils'; | ||
|
||
export const toHaveNoAxeIncomplete = ( | ||
received: AxeResults, | ||
filters?: ResultFilers, | ||
): jest.CustomMatcherResult => | ||
matchAxeResuls(received.incomplete, 'incomplete', filters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AxeResults } from 'axe-core'; | ||
import { matchAxeResuls, ResultFilers } from './utils'; | ||
|
||
export const toHaveNoAxePasses = ( | ||
received: AxeResults, | ||
filters?: ResultFilers, | ||
): jest.CustomMatcherResult => | ||
matchAxeResuls(received.passes, 'passed', filters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { AxeResults } from 'axe-core'; | ||
import { matchAxeResuls, ResultFilers } from './utils'; | ||
|
||
export const toHaveNoAxeViolations = ( | ||
received: AxeResults, | ||
filters?: ResultFilers, | ||
): jest.CustomMatcherResult => | ||
matchAxeResuls(received.violations, 'violated', filters); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Result } from 'axe-core'; | ||
import { | ||
printReceived, | ||
matcherHint, | ||
DIM_COLOR, | ||
BOLD_WEIGHT, | ||
INVERTED_COLOR, | ||
} from 'jest-matcher-utils'; | ||
|
||
export type ResultFiler = { rule: string; exclude?: boolean } | string; | ||
|
||
export type ResultFilers = { | ||
id?: ResultFiler[]; | ||
impact?: ResultFiler[]; | ||
}; | ||
|
||
const filterResults = (results: Result[], filters?: ResultFilers): Result[] => { | ||
let filtered = results; | ||
if (filters) { | ||
Object.keys(filters).forEach(key => { | ||
const id = key as keyof ResultFilers; | ||
const filter = filters[id] || []; | ||
const normalized = filter.map(f => | ||
typeof f === 'string' ? { rule: f, exclude: false } : f, | ||
); | ||
filtered = filtered.filter(f => | ||
normalized.find(n => (n.exclude ? n.rule !== f[id] : n.rule === f[id])), | ||
); | ||
}); | ||
} | ||
return filtered; | ||
}; | ||
export const matchAxeResuls = ( | ||
results: Result[], | ||
category: string, | ||
filters?: ResultFilers, | ||
): jest.CustomMatcherResult => { | ||
const filered = filterResults(results, filters); | ||
const pass = !filered?.length; | ||
|
||
const message: () => string = () => { | ||
return [ | ||
matcherHint( | ||
`${!pass ? '' : '.not'}.toHaveNoAxeViolations`, | ||
'axeresults', | ||
'', | ||
), | ||
'', | ||
...filered.map(result => { | ||
const v = result.nodes.length > 1 ? 'are' : 'is'; | ||
return ( | ||
`${ | ||
result.impact ? `${INVERTED_COLOR(result.impact)} ` : '' | ||
}${printReceived(result.description)}" (${result.id}) ${ | ||
pass ? `${v} not` : v | ||
} ${category}:` + | ||
`${result.nodes | ||
.map( | ||
node => | ||
`\n${BOLD_WEIGHT(node.target.join(', '))}` + | ||
`\n${INVERTED_COLOR(node.html)}` + | ||
`${ | ||
node.failureSummary | ||
? `\n${DIM_COLOR(node.failureSummary)}` | ||
: '' | ||
}`, | ||
) | ||
.join('\n')}` + | ||
`\nmore info at: ${result.helpUrl}` + | ||
'\n\n' | ||
); | ||
}), | ||
].join('\n'); | ||
}; | ||
return { | ||
message, | ||
pass, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"module": "esnext", | ||
"outDir": "dist", | ||
"rootDir": "src", | ||
"declaration": true, | ||
"types": ["node", "jest"], | ||
"typeRoots": ["../../node_modules/@types", "node_modules/@types"] | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules/**", "test/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
import { TeplateFormats } from './types'; | ||
|
||
export const accessibilityTemplate = ( | ||
format: TeplateFormats, | ||
): { | ||
allyimports: string; | ||
allytest: string; | ||
} => { | ||
const importPath = path.resolve( | ||
__dirname, | ||
`../templates/accessibility/import/import.${format}.js`, | ||
); | ||
const testPath = path.resolve( | ||
__dirname, | ||
`../templates/accessibility/test/test.${format}.js`, | ||
); | ||
|
||
return { | ||
allyimports: fs.readFileSync(importPath, 'utf8'), | ||
allytest: fs.readFileSync(testPath, 'utf8'), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.