Skip to content

Commit

Permalink
style: prettier formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
elersong committed Jul 23, 2024
1 parent 998b038 commit f39657d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 23 deletions.
20 changes: 15 additions & 5 deletions src/Errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,29 @@ export class CollectionPathNotFoundError extends Error {
}

export class DuplicateSubCollectionError extends Error {
constructor(entityConstructorName: string, subCollectionName: string, parentPropertyKey: string|undefined) {
super(`SubCollection<${entityConstructorName}> with name '${subCollectionName}' and propertyKey '${parentPropertyKey}' has already been registered`);
constructor(
entityConstructorName: string,
subCollectionName: string,
parentPropertyKey: string | undefined
) {
super(
`SubCollection<${entityConstructorName}> with name '${subCollectionName}' and propertyKey '${parentPropertyKey}' has already been registered`
);
}
}

export class DuplicateCollectionError extends Error {
constructor(entityConstructorName: string, collectionName: string) {
super(`Collection<${entityConstructorName}> with name '${collectionName}' has already been registered`);
super(
`Collection<${entityConstructorName}> with name '${collectionName}' has already been registered`
);
}
}

export class CustomRepositoryInheritanceError extends Error {
constructor() {
super('Cannot register a custom repository on a class that does not inherit from BaseFirestoreRepository');
super(
'Cannot register a custom repository on a class that does not inherit from BaseFirestoreRepository'
);
}
}
}
48 changes: 32 additions & 16 deletions src/MetadataStorage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { MetadataStorage, RepositoryMetadata, EnforcedCollectionMetadata, validateRepositoryIndex } from './MetadataStorage';
import {
MetadataStorage,
RepositoryMetadata,
EnforcedCollectionMetadata,
validateRepositoryIndex,
} from './MetadataStorage';
import { BaseFirestoreRepository } from './BaseFirestoreRepository';
import { IRepository, Constructor } from './types';
import { CollectionPathNotFoundError, IncompleteOrInvalidPathError, InvalidRepositoryIndexError } from './Errors';
import {
CollectionPathNotFoundError,
IncompleteOrInvalidPathError,
InvalidRepositoryIndexError,
} from './Errors';

describe('MetadataStorage', () => {
let metadataStorage: MetadataStorage;
Expand Down Expand Up @@ -96,7 +105,9 @@ describe('MetadataStorage', () => {
});

it('should throw error when using invalid collection path', () => {
expect(() => metadataStorage.getCollection('this_is_not_a_path')).toThrow(CollectionPathNotFoundError);
expect(() => metadataStorage.getCollection('this_is_not_a_path')).toThrow(
CollectionPathNotFoundError
);
});

it('should throw error if initialized with an invalid subcollection path', () => {
Expand Down Expand Up @@ -262,7 +273,9 @@ describe('MetadataStorage', () => {
expect(() => validateRepositoryIndex(['string'])).toThrowError(
'Invalid RepositoryIndex: Must be a tuple [string, (string | null)]'
);
expect(() => validateRepositoryIndex(['string', 'string', 'string'])).toThrow(InvalidRepositoryIndexError)
expect(() => validateRepositoryIndex(['string', 'string', 'string'])).toThrow(
InvalidRepositoryIndexError
);
});

it('should not throw error when repository index is valid', () => {
Expand Down Expand Up @@ -297,8 +310,8 @@ describe('MetadataStorage', () => {
parentCollectionName: 'test2',
parentEntityConstructor: TestClass2,
parentPropertyKey: 'test',
}} as EnforcedCollectionMetadata<TestClass>;

},
} as EnforcedCollectionMetadata<TestClass>;

const result = (classInstance as any)['isSubCollectionMetadata'](subCollectioMetadata);
expect(result).toEqual(true);
Expand All @@ -317,17 +330,16 @@ describe('MetadataStorage', () => {
const subCollectioMetadata = {
entityConstructor: TestClass,
name: 'test',
parentProps: null
parentProps: null,
} as EnforcedCollectionMetadata<TestClass>;


const result = (classInstance as any)['isSubCollectionMetadata'](subCollectioMetadata);
expect(result).toEqual(false);
const result2 = (classInstance as any)['isSubCollectionMetadata']({});
expect(result2).toEqual(false);
});
});

describe('isSameCollection', () => {
it('should return true when inputs are equivalent values', () => {
class TestClass {
Expand All @@ -342,12 +354,14 @@ describe('MetadataStorage', () => {
const subCollectioMetadata = {
entityConstructor: TestClass,
name: 'test',
parentProps: null
parentProps: null,
} as EnforcedCollectionMetadata<TestClass>;
const sameCollectionMetadata = subCollectioMetadata;


const result = (classInstance as any)['isSameCollection'](subCollectioMetadata, sameCollectionMetadata);
const result = (classInstance as any)['isSameCollection'](
subCollectioMetadata,
sameCollectionMetadata
);
expect(result).toEqual(true);
});

Expand All @@ -364,13 +378,15 @@ describe('MetadataStorage', () => {
const subCollectioMetadata = {
entityConstructor: TestClass,
name: 'test',
parentProps: null
parentProps: null,
} as EnforcedCollectionMetadata<TestClass>;
const sameCollectionMetadata = {...subCollectioMetadata};
const sameCollectionMetadata = { ...subCollectioMetadata };
sameCollectionMetadata.name = 'test2';


const result = (classInstance as any)['isSameCollection'](subCollectioMetadata, sameCollectionMetadata);
const result = (classInstance as any)['isSameCollection'](
subCollectioMetadata,
sameCollectionMetadata
);
expect(result).toEqual(false);
});
});
Expand Down
15 changes: 13 additions & 2 deletions src/MetadataStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import type {
ParentProperties,
} from './types';
import { arraysAreEqual } from './utils';
import { CollectionPathNotFoundError, CustomRepositoryInheritanceError, DuplicateCollectionError, DuplicateSubCollectionError, IncompleteOrInvalidPathError, InvalidRepositoryIndexError } from './Errors';
import {
CollectionPathNotFoundError,
CustomRepositoryInheritanceError,
DuplicateCollectionError,
DuplicateSubCollectionError,
IncompleteOrInvalidPathError,
InvalidRepositoryIndexError,
} from './Errors';
import { CustomRepository } from './Decorators';

// Unified collection metadata combines the metadata for both collections and subcollections
Expand Down Expand Up @@ -164,7 +171,11 @@ export class MetadataStorage {

if (existing && this.config.throwOnDuplicatedCollection == true) {
if (colIsSubCollection) {
throw new DuplicateSubCollectionError(existing.entityConstructor.name, existing.name, existing.parentProps?.parentPropertyKey);
throw new DuplicateSubCollectionError(
existing.entityConstructor.name,
existing.name,
existing.parentProps?.parentPropertyKey
);
} else {
throw new DuplicateCollectionError(existing.entityConstructor.name, existing.name);
}
Expand Down

0 comments on commit f39657d

Please sign in to comment.