Skip to content

Commit

Permalink
test: add test for new packageName HB function
Browse files Browse the repository at this point in the history
Non-standard templates need non-standard template tests :)
  • Loading branch information
DukeFerdinand committed Feb 6, 2025
1 parent 7ef9b61 commit ed1a151
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions scripts/utils/__tests__/generateDocs/auroTemplateFiller.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const handlebarsTemplate = `{{Name}} Documentation | Installing {{ withAuroNames

const legacyTemplate = `[Name] Documentation | Installing [namespace]-[name]`;

const nonStandardTemplate = `{{packageName}} test | {{namespace}}-{{name}}`;

describe('AuroTemplateFiller', () => {

/* @type {AuroTemplateFiller} */
Expand Down Expand Up @@ -59,8 +61,50 @@ describe('AuroTemplateFiller', () => {
};

const result = filler.replaceTemplateValues(handlebarsTemplate);
const packageNameResult = filler.replaceTemplateValues(nonStandardTemplate);

expect(result.trim()).toBe('Button Documentation | Installing auro-button');
expect(packageNameResult.trim()).toBe('auro-button test | auro-button');
});

it('should replace non-standard repository template values correctly', async () => {
const mockPackageJson = JSON.stringify({
name: '@aurodesignsystem/wc-generator',
version: '1.0.0',
peerDependencies: {
'@aurodesignsystem/design-tokens': '^2.0.0',
'@aurodesignsystem/webcorestylesheets': '^3.0.0'
}
});

fs.readFile.mockResolvedValue(mockPackageJson);

await filler.extractNames();

const result = filler.replaceTemplateValues(nonStandardTemplate);

expect(result.trim()).toBe('wc-generator test | wc-generator');
});

it('should throw an error when given a malformed package name', async () => {
const mockPackageJson = JSON.stringify({
name: '@aurodesignsystem/nunyabusiness',
version: '1.0.0',
peerDependencies: {
'@aurodesignsystem/design-tokens': '^2.0.0',
'@aurodesignsystem/webcorestylesheets': '^3.0.0'
}
});

async function shouldThrowAnErrorFunction() {
fs.readFile.mockResolvedValue(mockPackageJson);

await filler.extractNames();

filler.replaceTemplateValues(nonStandardTemplate);
}

await expect(() => shouldThrowAnErrorFunction()).rejects.toThrow(/No name can be derived/gu);
});

it('should replace legacy template values correctly', () => {
Expand Down
4 changes: 4 additions & 0 deletions scripts/utils/auroTemplateFiller.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export class AuroTemplateFiller {
const nameStart = pName.indexOf('-');
const packageNamespace = pName.substring(namespaceStart + 1, nameStart);

if (nameStart === -1) {
throw new Error(`No name can be derived from package.json "name" field: '${pName}'. Expected pattern with \`-\` split like [\`@aurodesignsystem/auro-component\` or \`@aurodesignsystem/eslint-config\`, etc.]`);
}

this.values = {
'npm': pName.substring(npmStart, namespaceStart),
'namespace': packageNamespace,
Expand Down

0 comments on commit ed1a151

Please sign in to comment.