-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Axe context #98
Axe context #98
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import type {RunOptions, Spec} from 'axe-core'; | ||
import {z as zod} from 'zod'; | ||
import type {Context} from './browser/AxePage'; | ||
import type {StorybookStory} from './browser/StorybookPage'; | ||
|
||
type Params = { | ||
disabledRules: string[]; | ||
mode: 'off' | 'warn' | 'error'; | ||
runOptions?: RunOptions; | ||
context?: Context; | ||
config?: Spec; | ||
skip: boolean; | ||
timeout: number; | ||
|
@@ -42,6 +44,9 @@ export default class ProcessedStory { | |
rawStory.parameters?.axe?.runOptions, | ||
rawStory, | ||
), | ||
context: normalizeContext(rawStory.parameters?.axe?.context, rawStory) as | ||
| Context | ||
| undefined, | ||
config: normalizeConfig(rawStory.parameters?.axe?.config, rawStory), | ||
}; | ||
} | ||
|
@@ -75,6 +80,14 @@ export default class ProcessedStory { | |
return this.parameters.runOptions; | ||
} | ||
|
||
/** | ||
* Context passed to `axe.run`. | ||
* @see https://www.deque.com/axe/core-documentation/api-documentation/#context-parameter | ||
*/ | ||
get context() { | ||
return this.parameters.context; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is checked elsewhere, but is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it's initialized in this object's constructor |
||
} | ||
|
||
/** | ||
* All optional config used to configure axe-core. Passed to `axe.configure`. | ||
* @see https://www.deque.com/axe/core-documentation/api-documentation/#api-name-axeconfigure | ||
|
@@ -138,6 +151,14 @@ const runOptionsSchema = zod.optional( | |
}), | ||
); | ||
|
||
const contextSchema = zod | ||
.union([ | ||
zod.string(), | ||
zod.array(zod.string()), | ||
zod.record(zod.string(), zod.any()), | ||
]) | ||
.optional(); | ||
|
||
const configSchema = zod.object({}).passthrough().optional(); | ||
|
||
function normalizeSkip(skip: unknown, rawStory: StorybookStory) { | ||
|
@@ -175,6 +196,14 @@ function normalizeRunOptions(runOptions: unknown, rawStory: StorybookStory) { | |
); | ||
} | ||
|
||
function normalizeContext(config: unknown, rawStory: StorybookStory) { | ||
return parseWithFriendlyError( | ||
() => contextSchema.parse(config), | ||
rawStory, | ||
'context', | ||
); | ||
} | ||
|
||
function normalizeConfig(config: unknown, rawStory: StorybookStory) { | ||
return parseWithFriendlyError( | ||
() => configSchema.parse(config), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note the
as
. I didn't feel like creating a Zod schema for all the permutations the context object can take, so left it just checking the general shape.