Skip to content
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

test(ui-library): #565 automate a11y tests #1062

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"build:ui-library": "yarn tokens:generate && yarn compile:icons && yarn workspace @boiler/ui-library build && yarn workspace @boiler/ui-library compile",
"lint": "yarn workspace @boiler/ui-library lint",
"format": "yarn workspace @boiler/ui-library format:eslint && yarn workspace @boiler/ui-library format:prettier",
"test": "yarn build:ui-library && yarn workspace @boiler/ui-library test",
"test:ui-library": "yarn build:ui-library && yarn workspace @boiler/ui-library test",
"test:ui-library:a11y": "yarn build:ui-library && yarn workspace @boiler/ui-library test:a11y",
"doc:install": "deven-documentation-skeleton install",
"doc:check": "deven-documentation-skeleton check",
"doc:update": "deven-documentation-skeleton update",
Expand Down
4 changes: 3 additions & 1 deletion packages/ui-library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
"format:eslint": "eslint 'src/**' --ext .ts,.tsx --fix",
"lint:prettier": "prettier \"**/*.{ts,tsx,cjs,mjs,js}\" --check",
"format:prettier": "prettier \"**/*.{ts,tsx,cjs,mjs,js}\" --write",
"test": "wtr --config web-test-runner.config.mjs"
"test": "wtr --config web-test-runner.config.mjs",
"test:a11y": "wtr --config web-test-runner-a11y.config.mjs"
},
"author": "",
"license": "MIT",
"devDependencies": {
"@boiler/eslint-config-boiler": "1.0.0",
"@boiler/figma-design-tokens": "0.0.1",
"@faker-js/faker": "^8.4.1",
"@storybook/addon-designs": "^7.0.7"
},
"dependencies": {
Expand Down
22 changes: 22 additions & 0 deletions packages/ui-library/src/components/counter/index.test.a11y.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import '@boiler/ui-library';

import { BlrCounterRenderFunction } from './renderFunction';
import type { BlrCounterType } from '.';

import { combineProperties } from '../../utils/test/combine-properties';
import { runA11yCombinationTests } from '../../utils/test/run-combination-test';

const propertyCombinations = combineProperties<BlrCounterType>({
maxValue: [0, 100],
sizeVariant: ['sm', 'md', 'lg', undefined],
theme: ['Dark', 'Light'],
value: [0, 50, 100],
variant: ['error', 'neutral', 'warn'],
});

describe('blr-counter', () => {
runA11yCombinationTests({
combinationsList: propertyCombinations,
componentRenderer: BlrCounterRenderFunction,
});
});
1 change: 0 additions & 1 deletion packages/ui-library/src/components/pa11y-custom.config.js

This file was deleted.

57 changes: 57 additions & 0 deletions packages/ui-library/src/utils/test/combine-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Generates a flat list of possible property combinations for the provided record of input values.
*
* **Does not act deeply.**
*
* @example
* ```typescript
* combineProperties({
* color: [undefined, 'red'],
* sizeVariant: ['md', 'lg'],
* })
* ```
*
* yields:
*
* ```typescript
* [{
* color: undefined,
* sizeVariant: 'md',
* }, {
* color: 'red',
* sizeVariant: 'md',
* }, {
* color: undefined,
* sizeVariant: 'lg',
* }, {
* color: 'red',
* sizeVariant: 'lg',
* }]
* ```
*
* @param valuesRecord The record of possible values a property can take.
* @returns A flat list of possible property combinations in shape of the {@linkcode valuesRecord}.
*
* - Search terms: combinations, permutations, combine
*/
export function combineProperties<TObject extends Record<string | number | symbol, unknown>>(
valuesRecord: ValuesRecord<TObject>
): TObject[] {
let combos: TObject[] = [];
for (const key in valuesRecord) {
const values = valuesRecord[key];
const all = [];
for (let i = 0; i < values.length; i++) {
for (let j = 0; j < (combos.length || 1); j++) {
const newCombo = { ...combos[j], [key]: values[i] };
all.push(newCombo);
}
}
combos = all;
}
return combos;
}

export type ValuesRecord<TObject extends Record<string | number | symbol, unknown>> = {
[PropertyName in keyof TObject]-?: TObject[PropertyName][];
};
37 changes: 37 additions & 0 deletions packages/ui-library/src/utils/test/run-combination-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { fixture, expect } from '@open-wc/testing';
import type { TaggedComponentRenderFunction } from '../typesafe-generic-component-renderer.js';

export type RunCombinationTestsOptions<T> = {
combinationList: T[];
subject: string;
test: (parameters: T) => Mocha.Func | Mocha.AsyncFunc;
};

export function runCombinationTests<TObject extends Record<string | number | symbol, unknown>>(
options: RunCombinationTestsOptions<TObject>
): void {
options.combinationList.forEach((parameters) => {
it(
`passes ${options.subject} test for property combination:\n\n${JSON.stringify(parameters, undefined, 2)}\n`,
options.test(parameters)
);
});
}

export type RunA11yCombinationTestsOptions<T extends Record<string | number | symbol, unknown>> = {
combinationsList: T[];
componentRenderer: TaggedComponentRenderFunction<T>;
};

export function runA11yCombinationTests<TObject extends Record<string | number | symbol, unknown>>(
options: RunA11yCombinationTestsOptions<TObject>
): void {
runCombinationTests({
combinationList: options.combinationsList,
subject: 'a11y',
test: (parameters) => async () => {
const el = await fixture(options.componentRenderer(parameters));
await expect(el).to.be.accessible();
},
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { TemplateResult, html, nothing } from 'lit';
import { camelCaseToKebabCase } from './lit/decorators.js';

export type TaggedComponentRenderFunction<TComponentType extends Record<string | number | symbol, unknown>> = (
props: Parameters<typeof genericBlrComponentRenderer<TComponentType>>[1],
children?: Parameters<typeof genericBlrComponentRenderer<TComponentType>>[2],
htmlAttributes?: Parameters<typeof genericBlrComponentRenderer<TComponentType>>[3]
) => ReturnType<typeof genericBlrComponentRenderer<TComponentType>>;

export const genericBlrComponentRenderer = <ComponentType extends { [s: string]: unknown } | ArrayLike<unknown>>(
tagName: string,
props: ComponentType,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui-library/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
]
},
"include": ["src"],
"exclude": ["src/**/*.test.ts", "src/**/*.stories.ts"]
"exclude": ["src/**/*.test.*", "src/**/*.stories.ts"]
}
10 changes: 10 additions & 0 deletions packages/ui-library/web-test-runner-a11y.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { esbuildPlugin } from '@web/dev-server-esbuild';

/**
* @type {import('@web/dev-server').DevServerConfig}
*/
export default {
nodeResolve: true,
files: ['src/components/**/*.test.a11y.ts'],
plugins: [esbuildPlugin({ ts: true })],
};
3 changes: 3 additions & 0 deletions packages/ui-library/web-test-runner.config.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { esbuildPlugin } from '@web/dev-server-esbuild';

/**
* @type {import('@web/dev-server').DevServerConfig}
*/
export default {
nodeResolve: true,
files: ['src/components/**/*.test.ts'],
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,7 @@ __metadata:
"@boiler/eslint-config-boiler": "npm:1.0.0"
"@boiler/figma-design-tokens": "npm:0.0.1"
"@boiler/icons": "npm:0.0.1"
"@faker-js/faker": "npm:^8.4.1"
"@floating-ui/dom": "npm:^1.6.3"
"@lit-labs/react": "npm:^1.1.1"
"@storybook/addon-designs": "npm:^7.0.7"
Expand Down Expand Up @@ -2320,6 +2321,13 @@ __metadata:
languageName: node
linkType: hard

"@faker-js/faker@npm:^8.4.1":
version: 8.4.1
resolution: "@faker-js/faker@npm:8.4.1"
checksum: 10c0/4f2aecddcfdc2cc8b50b2d15d1e37302a7c7a5bbd184ae910a9d271bc11248533ca74dcdd4a9ccbe20410553e7af0f6a4d334c5b955635e09a32ddf4a64942d4
languageName: node
linkType: hard

"@fal-works/esbuild-plugin-global-externals@npm:^2.1.2":
version: 2.1.2
resolution: "@fal-works/esbuild-plugin-global-externals@npm:2.1.2"
Expand Down
Loading