@workday/canvas-kit-docs
Version:
Documentation components of Canvas Kit components
64 lines (63 loc) • 3.65 kB
JavaScript
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
/* eslint-disable no-param-reassign */
import React from 'react';
import { toId } from '@storybook/csf';
import { Table } from '@workday/canvas-kit-react/table';
import { Hyperlink } from '@workday/canvas-kit-react/button';
import { specifications } from './specs';
import { GithubBranch, GithubUrl, StorybookUrl } from './docs';
function createTableRows(rows, item, index, children, context = { given: [], when: [], then: [], prefix: '' }) {
if (item.type === 'describe') {
if (/^given/i.test(item.name)) {
context = {
...context,
given: context.given.concat(context.prefix ? `${context.prefix} ${item.name}` : item.name),
};
context.prefix = '';
}
else if (/^[wt]hen/i.test(item.name)) {
context = {
...context,
when: context.when.concat(context.prefix ? `${context.prefix} ${item.name}` : item.name),
};
context.prefix = '';
}
else {
context = { ...context, prefix: context.prefix ? `${context.prefix} ${item.name}` : item.name };
}
item.children.reduce((rows, item, index, children) => createTableRows(rows, item, index, children, context), rows);
}
else {
context = {
...context,
then: context.then.concat(context.prefix ? `${context.prefix} ${item.name}` : item.name),
};
rows.push(context);
}
return rows;
}
export const Specifications = ({ file, name }) => {
const storybookBaseUrl = React.useContext(StorybookUrl);
const githubUrl = React.useContext(GithubUrl);
const githubBranch = React.useContext(GithubBranch);
const specFile = specifications.find(f => f.name === file);
if (!specFile) {
return null;
}
const block = name ? specFile.children.find(d => d.name === name) : specFile.children[0];
if (!block) {
return null;
}
const renderGiven = (input) => {
if (!input) {
return `Could not find a "given". Check the spec file.`;
}
const matches = input.match(/(.*)(\[[A-Za-z/\s]+), ([A-Za-z\s]+)\](.*)/);
if (matches == null) {
return input;
}
const [, first, kind, name, last] = matches;
return (_jsxs(_Fragment, { children: [first.replace(/given /i, ''), _jsx(Hyperlink, { href: `${storybookBaseUrl}?path=/story/${toId(kind, name.replace('DefaultStory', 'Default Story'))}`, children: name.replace('DefaultStory', 'Default') }), last] }));
};
return block.type === 'describe' ? (_jsxs(_Fragment, { children: [_jsxs(Table, { children: [_jsx(Table.Head, { children: _jsxs(Table.Row, { children: [_jsx(Table.Header, { children: "Given" }), _jsx(Table.Header, { children: "When" }), _jsx(Table.Header, { children: "Then" })] }) }), _jsx(Table.Body, { children: block.children.reduce(createTableRows, []).map((row, index) => (_jsxs(Table.Row, { children: [_jsx(Table.Cell, { children: renderGiven(row.given[0]) }), _jsx(Table.Cell, { children: _jsx("ul", { children: row.when.map((item, index) => (_jsx("li", { children: (index !== 0 ? 'AND THEN ' : '') + item.replace(/[tw]hen /i, '') }, index))) }) }), _jsx(Table.Cell, { children: _jsx("ul", { children: row.then.map((item, index) => (_jsx("li", { children: (item.indexOf('should') === 0 ? 'it ' : '') + item }, index))) }) })] }, index))) })] }), "Source:", ' ', _jsx(Hyperlink, { href: `${githubUrl}blob/${githubBranch}/cypress/integration/${file}`, children: file })] })) : null;
};