Skip to content

Commit d802170

Browse files
committed
fix: jsdocs tests
1 parent d533ec6 commit d802170

5 files changed

Lines changed: 76 additions & 7 deletions

File tree

src/utils/get-default-ref-date.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,27 @@ import type { FakerCore } from '../faker-core';
99
*
1010
* @returns The newly created default reference date.
1111
*
12+
* @example
13+
* fakerCore.randomizer.seed(1234);
14+
*
15+
* // Default behavior
16+
* // setDefaultRefDate(fakerCore);
17+
* past(fakerCore); // Changes based on the current date/time
18+
*
19+
* // Use a static ref date
20+
* setDefaultRefDate(fakerCore, new Date('2020-01-01'));
21+
* past(fakerCore); // Reproducible '2019-07-03T08:27:58.118Z'
22+
*
23+
* // Use a ref date that changes every time it is used
24+
* let clock = new Date("2020-01-01").getTime();
25+
* setDefaultRefDate(fakerCore, () => {
26+
* clock += 1000; // +1s
27+
* return new Date(clock);
28+
* });
29+
*
30+
* getDefaultRefDate(fakerCore) // 2020-01-01T00:00:01Z
31+
* getDefaultRefDate(fakerCore) // 2020-01-01T00:00:02Z
32+
*
1233
* @since 10.4.0
1334
*/
1435
export function getDefaultRefDate(fakerCore: FakerCore): Date {

src/utils/resolve-locale-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type LocaleProxyEntry<T> = unknown extends T ? T : Readonly<NonNullable<T>>;
3030
* @throws {FakerError} If the category or entry is not defined in the locale data.
3131
*
3232
* @example
33-
* arrayElements(fakerCore, resolveLocaleData(fakerCore, 'date', 'weekday')); // 'Sunday'
33+
* arrayElements(fakerCore, resolveLocaleData(fakerCore, 'animal', 'cat')); // 'Bengal'
3434
*
3535
* @since 10.4.0
3636
*/

src/utils/set-default-ref-date.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type { FakerCore } from '../faker-core';
1212
* @see faker.seed(): For generating reproducible values.
1313
*
1414
* @example
15-
* seed(fakerCore, 1234);
15+
* fakerCore.randomizer.seed(1234);
1616
*
1717
* // Default behavior
1818
* // setDefaultRefDate(fakerCore);
@@ -29,8 +29,8 @@ import type { FakerCore } from '../faker-core';
2929
* return new Date(clock);
3030
* });
3131
*
32-
* defaultRefDate(fakerCore) // 2020-01-01T00:00:01Z
33-
* defaultRefDate(fakerCore) // 2020-01-01T00:00:02Z
32+
* getDefaultRefDate(fakerCore) // 2020-01-01T00:00:01Z
33+
* getDefaultRefDate(fakerCore) // 2020-01-01T00:00:02Z
3434
*
3535
* @since 10.4.0
3636
*/

test/scripts/apidocs/__snapshots__/verify-jsdoc-tags.spec.ts.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ exports[`check docs completeness > all modules and methods are present 1`] = `
3131
[
3232
"generateMersenne32Randomizer",
3333
"generateMersenne53Randomizer",
34+
"getDefaultRefDate",
3435
"mergeLocales",
36+
"resolveLocaleData",
37+
"setDefaultRefDate",
3538
],
3639
],
3740
[

test/scripts/apidocs/verify-jsdoc-tags.spec.ts

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ afterAll(() => {
2424
});
2525

2626
const modules = processComponents(getProject());
27+
const moduleByMethod = new Map(
28+
modules.flatMap((m) => m.methods.map((method) => [method.name, m.camelTitle]))
29+
);
2730

2831
function resolveDirToModule(moduleName: string): string {
2932
return resolve(tempDir, moduleName);
@@ -38,6 +41,13 @@ function resolvePathToMethodFile(
3841
return resolve(dir, `${methodName}_${signature}.ts`);
3942
}
4043

44+
function toKebabCase(str: string): string {
45+
return str
46+
.replaceAll(/([a-z])([A-Z])/g, '$1-$2')
47+
.replaceAll(/[\s_]+/g, '-')
48+
.toLowerCase();
49+
}
50+
4151
const allowedReferences = new Set(
4252
modules.flatMap(({ camelTitle, methods, category }) => {
4353
return methods.map(({ name }) =>
@@ -94,6 +104,10 @@ describe('verify JSDoc tags', () => {
94104
});
95105
});
96106

107+
const thisModuleByMethod = new Map(
108+
module.methods.map((method) => [method.name, moduleName])
109+
);
110+
97111
describe.each(module.methods.map((m) => [m.name, m]))(
98112
'%s',
99113
(methodName, method) => {
@@ -145,10 +159,41 @@ ${examples}`;
145159
];
146160

147161
if (imports.length > 0) {
148-
examples = `import { ${imports.join(
149-
', '
150-
)} } from '${relativeImportPath}';\n\n${examples}`;
162+
examples = `// imports
163+
import { ${imports.join(', ')} } from '${relativeImportPath}';
164+
165+
${examples}`;
166+
}
167+
168+
const functionImports = [
169+
...new Set(examples.match(/\b(\w+)\(fakerCore/g)),
170+
]
171+
.map((s) => s.replace('(fakerCore', ''))
172+
.map((functionName) => [
173+
functionName,
174+
(
175+
thisModuleByMethod.get(functionName) ??
176+
moduleByMethod.get(functionName) ??
177+
'..'
178+
)?.replace(
179+
'utils',
180+
moduleName === 'utils' ? '../utils' : 'utils'
181+
),
182+
]);
183+
184+
if (examples.includes('fakerCore')) {
185+
examples = `// fakerCore
186+
import { base, en } from '${relativeImportPath}';
187+
import { createFakerCore } from '${relativeImportPath}/faker-core';
188+
const fakerCore = createFakerCore({ definitions: [en, base] });
189+
190+
${examples.replace("import { fakerCore } from '../../../../../src';", '')}`;
151191
}
192+
193+
examples = `// functionImports
194+
${functionImports.map(([functionName, moduleName]) => `import { ${functionName} } from '${relativeImportPath}/modules/${moduleName}/${toKebabCase(functionName)}';`).join('\n')}
195+
196+
${examples}`;
152197
}
153198

154199
writeFileSync(path, examples);

0 commit comments

Comments
 (0)