Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Mar 30, 2024
1 parent d460d46 commit 39575fc
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
10 changes: 7 additions & 3 deletions test/generators/object/ObjectFromSchema.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {
DeclarativeFieldDefinition,
Fabricator,
getValueGenerator,
ObjectFalsum,
SchemaInput,
SchemaInput
} from '../../../src';

const generatorName = 'object-from-schema';
Expand All @@ -23,13 +24,16 @@ const schema: SchemaInput = {

describe('ObjectFromSchema generator', () => {
it('should be found by name', () => {

const config = (schema.fields.nested as DeclarativeFieldDefinition).config;

expect(() =>
getValueGenerator(generatorName, schema.fields.nested?.config),
getValueGenerator(generatorName, config),
).not.toThrow();

const generator = getValueGenerator(
generatorName,
schema.fields.nested?.config,
config,
);

expect(generator).not.toBeUndefined();
Expand Down
29 changes: 28 additions & 1 deletion test/schema/Schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IntegerGenerator, Schema, SchemaInput } from '../../src';
import { IntegerGenerator, Schema, SchemaInput, UUIDGenerator } from '../../src';

describe('Schema constructor', () => {
it('should create a schema from ValueGenerators', () => {
Expand Down Expand Up @@ -64,4 +64,31 @@ describe('Schema constructor', () => {
expect(gen1!.constructor!.name).toBe(IntegerGenerator.name);
expect(gen2!.constructor!.name).toBe(IntegerGenerator.name);
});

it('should be able to consume the standard generator without error', () => {
const schemaInput: SchemaInput = {
fields: {
id: 'uuid',
},
};

expect(() => new Schema(schemaInput)).not.toThrow()
});

it('should be able to consume and create the standard generator', () => {
const schemaInput: SchemaInput = {
fields: {
id: 'uuid',
},
};

const schema = new Schema(schemaInput)

expect(Object.keys(schema.fields).length).toBe(1)
expect(Object.keys(schema.fields)[0]).toBe('id')

const generator = schema.fields['id'];

expect(generator!.constructor.name).toBe(UUIDGenerator.name)
});
});
27 changes: 23 additions & 4 deletions test/standards/UUIDGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
import { getStandard } from '../../src';

const generatorName = 'uuid';
const generatorNameUpper = 'UUID'

describe('UUIDGenerator', () => {
it('should be able to be found using name', () => {
it('should be able to be found using name (lower)', () => {
expect(() => getStandard(generatorName)).not.toThrow();
});

it('should return a string', () => {
it('should be able to be found using name (upper)', () => {
expect(() => getStandard(generatorNameUpper)).not.toThrow();
});

it('should return a string (lower)', () => {
const generator = getStandard(generatorName);

expect(typeof generator.generate()).toBe('string');
});

it('should return a string matching the regular expression', () => {
it('should return a string (upper)', () => {
const generator = getStandard(generatorNameUpper);

expect(typeof generator.generate()).toBe('string');
});

it('should return a string matching the regular expression (lower)', () => {
const regex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
const generator = getStandard(generatorName);

expect(regex.test(generator.generate() as string)).toBe(true);
});

it('should return a string matching the regular expression (upper)', () => {
const regex =
/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/;
const generator = getStandard(generatorNameUpper);

expect(regex.test(generator.generate() as string)).toBe(true);
});
});

0 comments on commit 39575fc

Please sign in to comment.