Skip to content

Commit

Permalink
test: Refactor documentation tests for better navigation (#28590)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Apr 29, 2024
1 parent 644c4a1 commit 3663ba2
Showing 1 changed file with 71 additions and 55 deletions.
126 changes: 71 additions & 55 deletions test/documentation.spec.ts
Expand Up @@ -19,84 +19,100 @@ describe('documentation', () => {
});

describe('website-documentation', () => {
describe('configuration-options', () => {
const doc = fs.readFileSync(
'docs/usage/configuration-options.md',
'utf8',
);

const headers = doc
.match(/\n## (.*?)\n/g)
?.map((match) => match.substring(4, match.length - 1));

const expectedOptions = options
.filter((option) => !option.globalOnly)
.filter((option) => !option.parents)
.filter((option) => !option.autogenerated)
.map((option) => option.name)
.sort();
describe('docs/usage/configuration-options.md', () => {
function getConfigHeaders(file: string): string[] {
const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
const matches = content.match(/\n## (.*?)\n/g) ?? [];
return matches.map((match) => match.substring(4, match.length - 1));
}

function getRequiredConfigOptions(): string[] {
return options
.filter((option) => !option.globalOnly)
.filter((option) => !option.parents)
.filter((option) => !option.autogenerated)
.map((option) => option.name)
.sort();
}

it('has doc headers sorted alphabetically', () => {
expect(headers).toEqual([...headers!].sort());
expect(getConfigHeaders('configuration-options.md')).toEqual(
getConfigHeaders('configuration-options.md').sort(),
);
});

it('has headers for every required option', () => {
expect(headers).toEqual(expectedOptions);
expect(getConfigHeaders('configuration-options.md')).toEqual(
getRequiredConfigOptions(),
);
});

const subHeaders = doc
.match(/\n### (.*?)\n/g)
?.map((match) => match.substring(5, match.length - 1));
subHeaders!.sort();
const expectedSubOptions = options
.filter((option) => option.stage !== 'global')
.filter((option) => !option.globalOnly)
.filter((option) => option.parents)
.map((option) => option.name)
.sort();
expectedSubOptions.sort();
function getConfigSubHeaders(file: string): string[] {
const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
const matches = content.match(/\n### (.*?)\n/g) ?? [];
return matches
.map((match) => match.substring(5, match.length - 1))
.sort();
}

function getRequiredConfigSubOptions(): string[] {
return options
.filter((option) => option.stage !== 'global')
.filter((option) => !option.globalOnly)
.filter((option) => option.parents)
.map((option) => option.name)
.sort();
}

it('has headers for every required sub-option', () => {
expect(subHeaders).toEqual(expectedSubOptions);
expect(getConfigSubHeaders('configuration-options.md')).toEqual(
getRequiredConfigSubOptions(),
);
});
});

describe('self-hosted-configuration', () => {
const doc = fs.readFileSync(
'docs/usage/self-hosted-configuration.md',
'utf8',
);
describe('docs/usage/self-hosted-configuration.md', () => {
function getSelfHostedHeaders(file: string): string[] {
const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
const matches = content.match(/\n## (.*?)\n/g) ?? [];
return matches.map((match) => match.substring(4, match.length - 1));
}

const headers = doc
.match(/\n## (.*?)\n/g)
?.map((match) => match.substring(4, match.length - 1));

const expectedOptions = options
.filter((option) => !!option.globalOnly)
.map((option) => option.name)
.sort();
function getRequiredSelfHostedOptions(): string[] {
return options
.filter((option) => option.globalOnly)
.map((option) => option.name)
.sort();
}

it('has headers sorted alphabetically', () => {
expect(headers).toEqual([...headers!].sort());
expect(getSelfHostedHeaders('self-hosted-configuration.md')).toEqual(
getSelfHostedHeaders('self-hosted-configuration.md').sort(),
);
});

it('has headers for every required option', () => {
expect(headers).toEqual(expectedOptions);
expect(getSelfHostedHeaders('self-hosted-configuration.md')).toEqual(
getRequiredSelfHostedOptions(),
);
});
});

describe('self-hosted-experimental', () => {
const doc = fs.readFileSync(
'docs/usage/self-hosted-experimental.md',
'utf8',
);

const headers = doc
.match(/\n## (.*?)\n/g)
?.map((match) => match.substring(4, match.length - 1));
describe('docs/usage/self-hosted-experimental.md', () => {
function getSelfHostedExperimentalConfigHeaders(file: string): string[] {
const content = fs.readFileSync(`docs/usage/${file}`, 'utf8');
const matches = content.match(/\n## (.*?)\n/g) ?? [];
return matches.map((match) => match.substring(4, match.length - 1));
}

it('has headers sorted alphabetically', () => {
expect(headers).toEqual([...headers!].sort());
expect(
getSelfHostedExperimentalConfigHeaders('self-hosted-experimental.md'),
).toEqual(
getSelfHostedExperimentalConfigHeaders(
'self-hosted-experimental.md',
).sort(),
);
});
});
});
Expand Down

0 comments on commit 3663ba2

Please sign in to comment.