Skip to content

Commit

Permalink
test(Button): code review
Browse files Browse the repository at this point in the history
  • Loading branch information
itwillwork committed Aug 1, 2024
1 parent 9568734 commit 09a9879
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 98 deletions.
14 changes: 7 additions & 7 deletions src/components/Button/__tests__/cases.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import type {Cases} from '../../../stories/tests-factory/models';
import type {CasesWithName} from '../../../stories/tests-factory/models';
import type {ButtonProps} from '../Button';

export const defaultProps: ButtonProps = {
children: 'Text',
};

export const sizeCases: Cases<ButtonProps['size']> = [
export const sizeCases: CasesWithName<ButtonProps['size']> = [
['xs', 'xs'],
['s', 's'],
['m', 'm'],
['l', 'l'],
['xl', 'xl'],
];

export const selectedCases: Cases<ButtonProps['selected']> = [['selected', true]];
export const selectedCases: CasesWithName<ButtonProps['selected']> = [['selected', true]];

export const disabledCases: Cases<ButtonProps['disabled']> = [['disabled', true]];
export const disabledCases: CasesWithName<ButtonProps['disabled']> = [['disabled', true]];

export const loadingCases: Cases<ButtonProps['loading']> = [['loading', true]];
export const loadingCases: CasesWithName<ButtonProps['loading']> = [['loading', true]];

export const viewsCases: Cases<ButtonProps['view']> = [
export const viewsCases: CasesWithName<ButtonProps['view']> = [
['normal', 'normal'],
['action', 'action'],
['outlined', 'outlined'],
Expand All @@ -43,7 +43,7 @@ export const viewsCases: Cases<ButtonProps['view']> = [
['flat-contrast', 'flat-contrast'],
];

export const pinsCases: Cases<ButtonProps['pin']> = [
export const pinsCases: CasesWithName<ButtonProps['pin']> = [
['round-round', 'round-round'],
['brick-brick', 'brick-brick'],
['clear-clear', 'clear-clear'],
Expand Down
75 changes: 0 additions & 75 deletions src/stories/tests-factory/create-regression-scenarios.ts

This file was deleted.

138 changes: 138 additions & 0 deletions src/stories/tests-factory/create-smoke-scenarios.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {createSmokeScenarios} from './create-smoke-scenarios';

test('regular', () => {
const smokeScenarious = createSmokeScenarios(
{
theme: 'theme-1',
label: 'label-1',
},
{
theme: ['theme-2', 'theme-3'],
label: ['label-2', 'label-3'],
},
);

expect(smokeScenarious).toEqual([
[
'smoke',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-1',
},
],
[
'smoke-theme-theme-2',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-2',
},
],
[
'smoke-theme-theme-3',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-3',
},
],
[
'smoke-label-label-2',
{tag: ['@smoke']},
{
label: 'label-2',
theme: 'theme-1',
},
],
[
'smoke-label-label-3',
{tag: ['@smoke']},
{
label: 'label-3',
theme: 'theme-1',
},
],
]);
});

test('with scenario name', () => {
const smokeScenarious = createSmokeScenarios(
{
theme: 'theme-1',
label: 'label-1',
},
{
theme: [
['name-theme-2', 'theme-2'],
['name-theme-3', 'theme-3'],
],
label: [
['name-label-2', 'label-2'],
['name-label-3', 'label-3'],
],
},
);

expect(smokeScenarious).toEqual([
[
'smoke',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-1',
},
],
[
'smoke-theme-name-theme-2',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-2',
},
],
[
'smoke-theme-name-theme-3',
{tag: ['@smoke']},
{
label: 'label-1',
theme: 'theme-3',
},
],
[
'smoke-label-name-label-2',
{tag: ['@smoke']},
{
label: 'label-2',
theme: 'theme-1',
},
],
[
'smoke-label-name-label-3',
{tag: ['@smoke']},
{
label: 'label-3',
theme: 'theme-1',
},
],
]);
});

test('with additionalTags option', () => {
const smokeScenarious = createSmokeScenarios(
{
theme: 'theme-1',
},
{
theme: [['name-theme-2', 'theme-2']],
},
{
additionalTags: ['@custom-tag-1'],
},
);

expect(smokeScenarious).toEqual([
['smoke', {tag: ['@smoke', '@custom-tag-1']}, {theme: 'theme-1'}],
['smoke-theme-name-theme-2', {tag: ['@smoke', '@custom-tag-1']}, {theme: 'theme-2'}],
]);
});
56 changes: 41 additions & 15 deletions src/stories/tests-factory/create-smoke-scenarios.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import type {Cases, Scenario, ScenarioDetails} from './models';
import type {Cases, CasesWithName, Scenario, ScenarioDetails} from './models';

interface Options {
additionalTags?: Array<string>;
}

function checkIsCasesWithName<T>(cases: CasesWithName<T> | Cases<T>): cases is CasesWithName<T> {
const firstCase = cases[0] || null;
return Array.isArray(firstCase) && firstCase.length === 2;
}

export const createSmokeScenarios = <Props extends {}>(
baseProps: Props,
propsCases: {
[K in keyof Props]: Cases<Props[K]>;
[K in Partial<keyof Props>]: CasesWithName<Props[K]> | Cases<Props[K]>;
},
options?: Options,
) => {
Expand All @@ -27,19 +32,40 @@ export const createSmokeScenarios = <Props extends {}>(

const propNames = Object.keys(propsCases) as Array<keyof Props>;
propNames.forEach((propName) => {
const propCases: Cases<Props[typeof propName]> = propsCases[propName] || [];
propCases.forEach((propCase) => {
const [caseName, caseProps] = propCase;

scenarios.push([
`smoke-${propName as string}-${caseName}`,
scenarioDetails,
{
...baseProps,
[propName]: caseProps,
},
]);
});
const propCases = propsCases[propName];

if (checkIsCasesWithName(propCases)) {
propCases.forEach((propCase) => {
const [caseName, caseProps] = propCase;

scenarios.push([
`smoke-${propName as string}-${caseName}`,
scenarioDetails,
{
...baseProps,
[propName]: caseProps,
},
]);
});
} else {
propCases.forEach((propCase) => {
const hasStringifyMethod = (propCase as any)?.toString;
if (!hasStringifyMethod) {
throw new Error(
'The case value does not have a method "toString", use case with name.',
);
}

scenarios.push([
`smoke-${propName as string}-${(propCase as any)?.toString()}`,
scenarioDetails,
{
...baseProps,
[propName]: propCase,
},
]);
});
}
});

return scenarios;
Expand Down
3 changes: 2 additions & 1 deletion src/stories/tests-factory/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type CaseName = string;
export type Cases<T> = Array<[CaseName, T]>;
export type Cases<T> = Array<T>;
export type CasesWithName<T> = Array<[CaseName, T]>;

export type ScenarioName = string;
export type ScenarioDetails = {tag: Array<string>};
Expand Down

0 comments on commit 09a9879

Please sign in to comment.