Skip to content

Commit

Permalink
test: 💍 add test for type file
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Nov 1, 2022
1 parent 56c60e3 commit 99f7029
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
10 changes: 10 additions & 0 deletions svg-generator/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,13 @@ export const groupTwoIcons = [childOneIcon, childTwoIcon];
},
]
`;
exports[`createTree should create the type file 1`] = `
"export {};
declare global {
interface SvgIcons {
icons: 'foo' | 'bar' | 'foo-bar';
}
}
"
`;
12 changes: 7 additions & 5 deletions svg-generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function generateSVGIcons(config: Config | null) {
removeOldIcons(resolve(mergedConfig.outputPath));

const virtualTree = createTree(mergedConfig.srcPath, mergedConfig.outputPath, mergedConfig);
const names: string[] = [];
let names: string[] = [];

if (mergedConfig.rootBarrelFile) {
const allExports = virtualTree
Expand All @@ -27,16 +27,18 @@ export function generateSVGIcons(config: Config | null) {
outputFileSync(join(mergedConfig.outputPath, `${mergedConfig.rootBarrelFileName}.ts`), allExports, {
encoding: 'utf-8',
});

names = virtualTree.filter(({ name }) => name !== INDEX).map(({ name }) => name);
} else {
virtualTree.forEach(({ path, content, name }) => {
name !== INDEX && names.push(name);
outputFileSync(path, content, { encoding: 'utf-8' });
});

outputFileSync(`${mergedConfig.typesPath}/types/svg.d.ts`, createTypeFile(names), {
encoding: 'utf-8',
});
}

outputFileSync(`${mergedConfig.typesPath}/types/svg.d.ts`, createTypeFile(names), {
encoding: 'utf-8',
});
}

function removeOldIcons(outputPath: string) {
Expand Down
5 changes: 4 additions & 1 deletion svg-generator/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import mock from 'mock-fs';
import { createTypeFile } from './create-types';
import { createTree } from './tree';
import { defaults } from './types';

const srcPath = `src/assets/svg`;

describe('createTree', () => {

it('should create the correct tree', () => {
mock({
[`${srcPath}/one.svg`]: '<svg>one</svg>',
Expand All @@ -27,4 +27,7 @@ describe('createTree', () => {
expect(result).toMatchSnapshot();
});

it('should create the type file', () => {
expect(createTypeFile(['foo', 'bar', 'foo-bar'])).toMatchSnapshot();
});
});

0 comments on commit 99f7029

Please sign in to comment.