Skip to content

Commit

Permalink
feat: initial values data testing
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 5, 2021
1 parent 128c70a commit 3f5d298
Show file tree
Hide file tree
Showing 85 changed files with 1,080 additions and 265 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@
"name": "jest cc-cli",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/plugins/cc-cli",
"args": ["cli-store"],
"args": ["cli-document-data"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
Expand Down
13 changes: 7 additions & 6 deletions core/core/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ import {
import { BuildProps } from './build';
import { StoryProps } from './common';
import { ActionItems } from './utility';
import { Story, Document } from './document';
import { Story, Document, ExampleControls } from './document';
import { SearchOptions } from './search';
import { TokenOptions } from './tokens';

/**
* render function by framework. By default 'react'
*/
export type FrameworkRenderFn = (
story: Story,
doc?: Document,
options?: any,
) => ReactElement;
export type FrameworkRenderFn = (props: {
story: Story;
doc?: Document;
values?: ExampleControls;
options?: any;
}) => ReactElement;

/**
* story type pages can have multiple tabs with separate page configurations.
Expand Down
7 changes: 1 addition & 6 deletions core/core/src/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,7 @@ export type Story<Props = any> = {
export type DynamicExamples = Story[];

export type ExampleControls = {
[name: string]:
| ComponentControl<ExampleControls>
| string
| string[]
| boolean
| number;
[name: string]: ComponentControl<ExampleControls> | any;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions core/render/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
FrameworkRenderFn,
} from '@component-controls/core';

export const render: FrameworkRenderFn = (story, doc, options: any = {}) => {
export const render: FrameworkRenderFn = ({ story, doc, options }) => {
if (!story) {
throw new Error(`Invalid story`);
}
Expand All @@ -20,7 +20,7 @@ export const render: FrameworkRenderFn = (story, doc, options: any = {}) => {
controls,
...options,
};
const { decorators: globalDecorators = [] } = options;
const { decorators: globalDecorators = [] } = options || {};
const { decorators: storyDecorators = [] } = story;
const decorators = deepMerge<StoryRenderFn[]>(
globalDecorators,
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/src/Component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Component', () => {
describe('main', () => {
const example = main;

let rendered;
let rendered: ReturnType<typeof renderExample> = undefined as any;
act(() => {
rendered = renderExample({
example,
Expand Down
2 changes: 1 addition & 1 deletion plugins/cc-cli/src/cli/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
defaultCliArgs,
} from '@component-controls/webpack-compile/cli';

import { renderers } from '../types';
import { renderers } from '../utils';

export const jestCliArgs: ArgOptions = [
...defaultCliArgs.filter(arg => arg.name === 'config'),
Expand Down
6 changes: 4 additions & 2 deletions plugins/cc-cli/src/cli/cli-store.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CliOptions } from './types';
import { saveTemplate } from './save-template';
import { CliOptions } from './utils';
import { saveTemplate } from './save-test-template';
import { createStoreTemplate } from '../jest-templates/store-template';

/**
Expand All @@ -19,6 +19,7 @@ export const cliStore = async (options: CliOptions): Promise<void> => {
include,
exclude,
ally,
data,
} = options;
const test =
userTest || `component-controls.test.${format === 'ts' ? 'ts' : 'js'}`;
Expand All @@ -35,6 +36,7 @@ export const cliStore = async (options: CliOptions): Promise<void> => {
include,
exclude,
ally,
data,
},
createStoreTemplate,
);
Expand Down
50 changes: 26 additions & 24 deletions plugins/cc-cli/src/cli/cli-story.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import path from 'path';
import { loadConfig, extractDocuments } from '@component-controls/config';
import { loadStore } from '@component-controls/store';
import { StoryTemplateOptions, TeplateFormats } from '../types';
import { CliOptions } from './types';
import { saveTemplate } from './save-template';
import { StoryTemplateOptions, TeplateFormats } from '../utils';
import { CliOptions } from './utils';
import { saveTemplate } from './save-test-template';
import { createStoriesTemplate } from '../jest-templates/stories-template';
import { createDocumentTemplate } from '../jest-templates/document-template';

Expand Down Expand Up @@ -42,6 +42,7 @@ export const cliStory = async (
include,
exclude,
ally,
data,
} = options;
let documents: string[] = [];

Expand All @@ -62,33 +63,34 @@ export const cliStory = async (
const basename = path.basename(name);
const splitName = basename.split('.');
const componentName = splitName[0];

const fileFormat =
format || (path.extname(name).startsWith('.ts') ? 'ts' : 'esm');
const testName =
test || formatExtension(`${componentName}.test.js`, fileFormat);

const docOptions = {
renderer,
format: fileFormat,
include,
exclude,
overwrite,
config,
test: testName,
bundle,
name: options.name || componentName,
storyPath: name,
ally,
data,
output: output
? output
: bundle
? path.dirname(bundle)
: path.dirname(name),
};
const templateFn = generateDoc
? createDocumentTemplate
: createStoriesTemplate;
await saveTemplate<StoryTemplateOptions>(
{
renderer,
format: fileFormat,
include,
exclude,
overwrite,
config,
test: testName,
bundle,
name: options.name || componentName,
storyPath: name,
ally,
output: output
? output
: bundle
? path.dirname(bundle)
: path.dirname(name),
},
templateFn,
);
await saveTemplate<StoryTemplateOptions>(docOptions, templateFn);
}
};
5 changes: 4 additions & 1 deletion plugins/cc-cli/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { cliArgs } from '@component-controls/webpack-compile/cli';
import { jestCliArgs } from './args';
import { cliStore } from './cli-store';
import { cliStory } from './cli-story';
import { CliOptions } from './types';
import { CliOptions } from './utils';

/**
* cc-cli central function. based on the selected g/generate option will call the specific
Expand All @@ -23,6 +23,7 @@ export const run = async (): Promise<void> => {
include,
exclude,
ally,
data,
} = parsedArgs;
switch (parsedArgs.generate) {
case 'store':
Expand All @@ -38,6 +39,7 @@ export const run = async (): Promise<void> => {
include,
exclude,
ally,
data,
} as CliOptions);
case 'story':
case 'doc':
Expand All @@ -54,6 +56,7 @@ export const run = async (): Promise<void> => {
include,
exclude,
ally,
data,
} as CliOptions,
parsedArgs.generate === 'doc',
);
Expand Down
45 changes: 45 additions & 0 deletions plugins/cc-cli/src/cli/save-data-template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'path';
import fs from 'fs';
import { CliOptions, getTestFolder } from './utils';
import { TemplateOptions, DataImportOptions, relativeImport } from '../utils';
import { createDataTemplate } from '../data-templates/data-template';

/**
* save a data template file based on options
* @param options - the rendering and file options
*/
export const saveDataTemplate = async <P extends TemplateOptions>(
options: CliOptions<P>,
): Promise<DataImportOptions | undefined> => {
const testFolder = getTestFolder(options);
if (!testFolder) {
return undefined;
}
const { test, overwrite } = options;
const dataName = test
.split('.')
.map((e, i) => (i === 1 ? 'data' : e))
.join('.');
const filePath = path.resolve(testFolder, dataName);
let existing: Record<string, any> | undefined = undefined;
if (fs.existsSync(filePath)) {
if (overwrite) {
//load existing data file
existing = require(filePath);
} else {
return undefined;
}
}
const dataTemplate = await createDataTemplate(options, existing);
if (dataTemplate) {
if (!fs.existsSync(testFolder)) {
fs.mkdirSync(testFolder);
}
fs.writeFileSync(filePath, dataTemplate.content, 'utf8');
return {
data: dataTemplate.data,
filePath: relativeImport(testFolder, filePath),
};
}
return undefined;
};
Original file line number Diff line number Diff line change
@@ -1,34 +1,25 @@
import path from 'path';
import fs from 'fs';
import { CliOptions } from './types';
import { TemplateFunction, TemplateOptions } from '../types';
import { CliOptions, getTestFolder } from './utils';
import { TemplateFunction, TemplateOptions } from '../utils';
import { saveDataTemplate } from './save-data-template';

/**
* save a template file based on options
* @param options - the rendering and file options
* @param templateFn - a selected templating function
* @param dataImports - the data import parameters - ie file to import and stories that have data values associated
* @returns a promise, since the function is async
*/
export const saveTemplate = async <P extends TemplateOptions>(
options: CliOptions<P>,
templateFn: TemplateFunction<P>,
): Promise<void> => {
const { test, overwrite, output, include, exclude, ...rest } = options;

//check if the test file name is to be included.
const testName = test.split('.')[0];
if (include && !include.includes(testName)) {
return;
}
if (exclude && exclude.includes(testName)) {
return;
}

let testFolder = output as string;

if (!path.isAbsolute(testFolder)) {
testFolder = path.resolve(process.cwd(), testFolder);
const testFolder = getTestFolder(options);
if (!testFolder) {
return undefined;
}
const { test, overwrite, ...rest } = options;

const testFilePath = path.resolve(testFolder, test);

Expand All @@ -38,10 +29,15 @@ export const saveTemplate = async <P extends TemplateOptions>(
);
return;
}
const content = await templateFn(({
output: testFolder,
...rest,
} as unknown) as P);
const dataTemplate = await saveDataTemplate(options);

const content = await templateFn(
({
output: testFolder,
...rest,
} as unknown) as P,
dataTemplate,
);
if (content) {
if (!fs.existsSync(testFolder)) {
fs.mkdirSync(testFolder);
Expand Down
14 changes: 0 additions & 14 deletions plugins/cc-cli/src/cli/types.ts

This file was deleted.

33 changes: 33 additions & 0 deletions plugins/cc-cli/src/cli/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import path from 'path';
import { TemplateOptions } from '../utils';

export type CliOptions<P extends TemplateOptions = TemplateOptions> = {
overwrite: boolean;
test: string;
/**
* components to include
*/
include?: string[];
/**
* components to exclude
*/
exclude?: string[];
} & P;

export const getTestFolder = (options: CliOptions): string | undefined => {
const { test, output, include, exclude } = options;
//check if the test file name is to be included.
const testName = test.split('.')[0];
if (include && !include.includes(testName)) {
return undefined;
}
if (exclude && exclude.includes(testName)) {
return undefined;
}
let testFolder = output as string;

if (!path.isAbsolute(testFolder)) {
testFolder = path.resolve(process.cwd(), testFolder);
}
return testFolder;
};
Loading

0 comments on commit 3f5d298

Please sign in to comment.