Skip to content

Commit

Permalink
fix: load existing data files
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 7, 2021
1 parent d029ef7 commit cbdeaa3
Show file tree
Hide file tree
Showing 29 changed files with 867 additions and 199 deletions.
15 changes: 13 additions & 2 deletions core/core/src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ export const nakedFileName = (filePath: string): string => {
return baseName.substr(0, baseName.lastIndexOf('.'));
};

const esmRequire = (filePath: string): any => {
const result = require('esm')(module)(filePath);
if (
!result ||
(typeof result === 'object' && Object.keys(result).length === 0)
) {
return require(filePath);
}
return result;
};
export const dynamicRequire = (filePath: string): any => {
const ext =
filePath
Expand Down Expand Up @@ -55,12 +65,13 @@ export const dynamicRequire = (filePath: string): any => {
}
ts.sys.writeFile(fileName, data);
});
return require('esm')(module)(jsFilePath);
const result = esmRequire(jsFilePath);
return result;
} finally {
fs.rmdirSync(tmpDir.name, { recursive: true });
// tmpDir.removeCallback();
}
}

return require('esm')(module)(filePath);
return esmRequire(filePath);
};
54 changes: 54 additions & 0 deletions core/jest-extract/test/fixtures/story/VariantButton.data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
module.exports = {
overview: {
'0': {
text: 'Gisselle Mohr',
icon: 'Kaden Powlowski',
fontSize: 12,
},
'1': {
text: 'Barbara Brakus',
icon: 'Miss Dolly Ferry',
fontSize: 22,
},
'2': {
text: 'Adah Nolan',
icon: 'Anais Pagac',
fontSize: 22,
},
'3': {
text: 'Jon Will',
icon: 'Geraldine Metz',
fontSize: 20,
},
'4': {
text: 'Mr. Dulce Rice',
icon: 'Karianne Bins',
fontSize: 16,
},
'5': {
text: 'Mrs. Jermey Jacobson',
icon: 'Mikayla Gusikowski',
fontSize: 29,
},
'6': {
text: 'Clark Hickle',
icon: 'Marielle Durgan',
fontSize: 16,
},
'7': {
text: 'Ibrahim Purdy',
icon: 'Cullen Heller',
fontSize: 27,
},
'8': {
text: 'Jody Legros',
icon: 'Dedric Smitham',
fontSize: 16,
},
'9': {
text: 'Ford Kunze',
icon: 'Trinity Hickle',
fontSize: 12,
},
},
};
47 changes: 47 additions & 0 deletions core/jest-extract/test/fixtures/story/VariantButton.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const path = require('path');
const { run } = require('axe-core');
const { reactRunDOM } = require('@component-controls/test-renderers');
require('@component-controls/jest-axe-matcher');

const { loadConfigurations } = require('@component-controls/config');
const { renderDocument } = require('@component-controls/test-renderers');
const { render, act } = require('@testing-library/react');
const { renderErr } = require('@component-controls/test-renderers');

const examples = require('./VariantButton.docs');
const data = require('./VariantButton.data');

describe('VariantButton', () => {
const configPath = path.resolve(
__dirname,
'../../../../../plugins/cc-cli/test/.config',
);
const config = loadConfigurations(configPath);
let renderedExamples = [];
act(() => {
renderedExamples = renderDocument(examples, config, data);
});
if (!renderedExamples) {
renderErr();
return;
}
renderedExamples.forEach(({ name, rendered, dataId, values }) => {
describe(name, () => {
const runTests = () => {
it('snapshot', () => {
const { asFragment } = render(rendered);
expect(asFragment()).toMatchSnapshot();
});
it('accessibility', async () => {
const axeResults = await reactRunDOM(rendered, run);
expect(axeResults).toHaveNoAxeViolations();
});
};
if (values) {
describe(dataId, runTests);
} else {
runTests();
}
});
});
});
38 changes: 38 additions & 0 deletions core/jest-extract/test/fixtures/story/VariantButton.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as path from 'path';
import { run, AxeResults } from 'axe-core';
import { reactRunDOM } from '@component-controls/test-renderers';
import '@component-controls/jest-axe-matcher';
import { loadConfigurations } from '@component-controls/config';
import { renderDocument } from '@component-controls/test-renderers';
import { render, act } from '@testing-library/react';
import { renderErr } from '@component-controls/test-renderers';

import * as examples from './VariantButton.docs';

describe('VariantButton', () => {
const configPath = path.resolve(
__dirname,
'../../../../../plugins/cc-cli/test/.config',
);
const config = loadConfigurations(configPath);
let renderedExamples: ReturnType<typeof renderDocument> = [];
act(() => {
renderedExamples = renderDocument(examples, config);
});
if (!renderedExamples) {
renderErr();
return;
}
renderedExamples.forEach(({ name, rendered }) => {
describe(name, () => {
it('snapshot', () => {
const { asFragment } = render(rendered);
expect(asFragment()).toMatchSnapshot();
});
it('accessibility', async () => {
const axeResults = await reactRunDOM<AxeResults>(rendered, run);
expect(axeResults).toHaveNoAxeViolations();
});
});
});
});
Loading

0 comments on commit cbdeaa3

Please sign in to comment.