1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-09 00:18:00 +01:00

refactor: add tests for the schemas list (#1705)

* refactor: move schemas list to the top

* refactor: add tests for the schemas list
This commit is contained in:
olav 2022-06-13 11:23:36 +02:00 committed by GitHub
parent 2c5f70d420
commit 0a53d67859
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 27 deletions

View File

@ -3,7 +3,34 @@ import {
createRequestSchema,
createResponseSchema,
removeJsonSchemaProps,
schemas,
} from './index';
import fs from 'fs';
import path from 'path';
test('all schema files should be added to the schemas object', () => {
const schemaFileNames = fs
.readdirSync(path.join(__dirname, 'spec'))
.filter((fileName) => fileName.endsWith('-schema.ts'));
const expectedSchemaNames = schemaFileNames.map((fileName) => {
return fileName
.replace(/\.ts$/, '')
.replace(/-./g, (x) => x[1].toUpperCase());
});
const addedSchemaNames = Object.keys(schemas);
expect(expectedSchemaNames.sort()).toEqual(addedSchemaNames.sort());
});
test('all schema $id attributes should have the expected format', () => {
const schemaIds = Object.values(schemas).map((schema) => schema.$id);
const schemaIdRegExp = new RegExp(`^#/components/schemas/[a-z][a-zA-Z]+$`);
schemaIds.forEach((schemaId) => {
expect(schemaId).toMatch(schemaIdRegExp);
});
});
test('createRequestSchema', () => {
expect(createRequestSchema('schemaName')).toMatchInlineSnapshot(`

View File

@ -4,6 +4,7 @@ import { constraintSchema } from './spec/constraint-schema';
import { createFeatureSchema } from './spec/create-feature-schema';
import { createStrategySchema } from './spec/create-strategy-schema';
import { environmentSchema } from './spec/environment-schema';
import { environmentsSchema } from './spec/environments-schema';
import { featureEnvironmentSchema } from './spec/feature-environment-schema';
import { featureSchema } from './spec/feature-schema';
import { featureStrategySchema } from './spec/feature-strategy-schema';
@ -22,6 +23,8 @@ import { patchesSchema } from './spec/patches-schema';
import { projectEnvironmentSchema } from './spec/project-environment-schema';
import { projectSchema } from './spec/project-schema';
import { projectsSchema } from './spec/projects-schema';
import { sortOrderSchema } from './spec/sort-order-schema';
import { splashSchema } from './spec/splash-schema';
import { strategySchema } from './spec/strategy-schema';
import { tagSchema } from './spec/tag-schema';
import { tagsSchema } from './spec/tags-schema';
@ -31,34 +34,8 @@ import { updateStrategySchema } from './spec/update-strategy-schema';
import { variantSchema } from './spec/variant-schema';
import { variantsSchema } from './spec/variants-schema';
import { versionSchema } from './spec/version-schema';
import { environmentsSchema } from './spec/environments-schema';
import { sortOrderSchema } from './spec/sort-order-schema';
import { splashSchema } from './spec/splash-schema';
// Schemas must have $id property on the form "#/components/schemas/mySchema".
export type SchemaId = typeof schemas[keyof typeof schemas]['$id'];
// Schemas must list all $ref schemas in "components", including nested schemas.
export type SchemaRef = typeof schemas[keyof typeof schemas]['components'];
// JSON schema properties that should not be included in the OpenAPI spec.
export interface JsonSchemaProps {
$id: string;
components: object;
}
export interface AdminApiOperation
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['admin'];
}
export interface ClientApiOperation
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['client'];
}
// All schemas in `openapi/spec` should be listed here.
export const schemas = {
cloneFeatureSchema,
constraintSchema,
@ -95,6 +72,30 @@ export const schemas = {
versionSchema,
};
// Schemas must have an $id property on the form "#/components/schemas/mySchema".
export type SchemaId = typeof schemas[keyof typeof schemas]['$id'];
// Schemas must list all their $refs in `components`, including nested schemas.
export type SchemaRef = typeof schemas[keyof typeof schemas]['components'];
// JSON schema properties that should not be included in the OpenAPI spec.
export interface JsonSchemaProps {
$id: string;
components: object;
}
export interface AdminApiOperation
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['admin'];
}
export interface ClientApiOperation
extends Omit<OpenAPIV3.OperationObject, 'tags'> {
operationId: string;
tags: ['client'];
}
export const createRequestSchema = (
schemaName: string,
): OpenAPIV3.RequestBodyObject => {