Skip to content

Commit 879fe9a

Browse files
committed
test: fix existing tests to be compatible with new features
1 parent b2f65d7 commit 879fe9a

File tree

2 files changed

+89
-16
lines changed

2 files changed

+89
-16
lines changed

__tests__/pkg-usage.test.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('getPackagesUsages()', () => {
3737
expect(
3838
getPackagesUsages({
3939
packages: [pkg],
40-
fileGlobs: `${MOCKS_DIR}/**.ts`,
40+
fileGlobs: `${MOCKS_DIR}/**.tsx`,
4141
packageJsonCWD: MOCKS_DIR_CWD,
4242
})
4343
).toStrictEqual([{ count: 0, files: [], name: pkg, version }]);
@@ -48,21 +48,20 @@ describe('getPackagesUsages()', () => {
4848
it('should return the right package usage', () => {
4949
const { fileName, imports, pkg, version } = mockPackageUsageFile();
5050

51-
expect(
52-
getPackagesUsages({
53-
packages: [pkg],
54-
fileGlobs: `${MOCKS_DIR}/**.ts`,
55-
packageJsonCWD: MOCKS_DIR_CWD,
56-
})
57-
).toStrictEqual([
51+
const result = getPackagesUsages({
52+
packages: [pkg],
53+
fileGlobs: `${MOCKS_DIR}/**.tsx`,
54+
packageJsonCWD: MOCKS_DIR_CWD,
55+
});
56+
57+
expect(result).toStrictEqual([
5858
{
5959
count: 1,
6060
files: [
6161
{
62-
defaultImport: undefined,
63-
filePath: `${MOCKS_DIR_CWD}/${fileName}.ts`,
64-
name: `${fileName}.ts`,
65-
namedImports: imports,
62+
filePath: `${MOCKS_DIR_CWD}/${fileName}.tsx`,
63+
name: `${fileName}.tsx`,
64+
imports,
6665
},
6766
],
6867
name: pkg,

__tests__/utils.ts

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,89 @@ function mockUniqueList<T, U>(
3030
.filter((value, index, self) => self.indexOf(value) === index);
3131
}
3232

33+
function word() {
34+
return f.hacker.noun().replace(/ |-/g, '');
35+
}
36+
37+
function capitalize(word: string) {
38+
return `${word.charAt(0).toUpperCase()}${word.slice(1)}`;
39+
}
40+
41+
function generatePropList(props: string[]) {
42+
return props.map((prop) => `${prop}="${prop}"`).join(' ');
43+
}
44+
45+
function jsxElement(value: string, line: number) {
46+
const props = mockUniqueList(() => word().toLowerCase());
47+
return [
48+
{
49+
name: value,
50+
usages: [
51+
{
52+
line: line + 2,
53+
props,
54+
text: `<${value} ${generatePropList(props)}/>`,
55+
},
56+
],
57+
},
58+
`function ${capitalize(word())}() { return (<${value} ${generatePropList(
59+
props
60+
)}/>)}`,
61+
];
62+
}
63+
64+
function propertyAccess(value: string, line: number) {
65+
const property = word();
66+
return [
67+
{
68+
name: value,
69+
usages: [{ line: line + 2, property, text: `\n${value}.${property}` }],
70+
},
71+
`${value}.${property}`,
72+
];
73+
}
74+
75+
function callExpression(value: string, line: number) {
76+
return [
77+
{ name: value, usages: [{ line: line + 2, text: `\n${value}()` }] },
78+
`${value}()`,
79+
];
80+
}
81+
82+
function valueUsage(value: string, line: number) {
83+
return [
84+
{ name: value, usages: [{ line: line + 2, text: `\n${value};` }] },
85+
`${value};`,
86+
];
87+
}
88+
89+
const possibleUsages = [propertyAccess, callExpression, valueUsage, jsxElement];
90+
91+
function mockTSFile(pkgName: string, imports: string[]) {
92+
const importSection = `import { ${imports.join(', ')} } from '${pkgName}';`;
93+
94+
const usages = imports.map((entry, index) =>
95+
possibleUsages[index % possibleUsages.length](entry, index)
96+
);
97+
98+
const mockedImports = usages.map((data) => data[1]);
99+
100+
return {
101+
file: [importSection, ...mockedImports].join('\n'),
102+
imports: usages.map((data) => data[0]),
103+
};
104+
}
105+
33106
export function mockPackageUsageFile(customData?: string) {
34-
const imports = mockUniqueList(() => f.hacker.noun().replace(/ |-/g, ''));
107+
const importNames = mockUniqueList(() => capitalize(word()));
35108
const fileName = f.datatype.uuid();
36109
const pkg = f.hacker.noun();
37110
const version = f.system.semver();
38111

112+
const { file, imports } = mockTSFile(pkg, importNames);
39113
// Mocked with random data to generate a resilient test
40-
const data = customData ?? `import { ${imports.join(', ')} } from '${pkg}';`;
41-
writeFile(`${fileName}.ts`, data);
114+
const data = customData ?? file;
115+
writeFile(`${fileName}.tsx`, data);
42116

43117
writeFile(
44118
'package.json',
@@ -55,7 +129,7 @@ export function mockPackageUsageFile(customData?: string) {
55129
);
56130

57131
return {
58-
imports,
132+
imports: !customData ? imports : undefined,
59133
fileName,
60134
pkg,
61135
version,

0 commit comments

Comments
 (0)