forked from decahedronio/entity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,30 @@ | ||
import { BlogPost } from './blog'; | ||
import Comment from './comment'; | ||
import { EntityBuilder } from '../../src/EntityBuilder'; | ||
import { BlogPost } from "./blog"; | ||
import Comment from "./comment"; | ||
import { EntityBuilder } from "../../src"; | ||
|
||
describe('Entity with circular dependency', () => { | ||
it('decodes circularly depended annotated entity', () => { | ||
const blog = EntityBuilder.buildOne(BlogPost, { | ||
title: 'Decahedron/Entity gets circdep', | ||
body: 'hooray!', | ||
comments: [ | ||
{ body: 'Yay!' }, | ||
], | ||
}); | ||
|
||
expect(blog.comments).toBeDefined(); | ||
expect(blog.comments[0]).toBeDefined(); | ||
expect(blog.comments[0].body).toEqual('Yay!'); | ||
describe("Entity with circular dependency", () => { | ||
it("decodes circularly depended annotated entity", () => { | ||
const blog = EntityBuilder.buildOne(BlogPost, { | ||
title: "Decahedron/Entity gets circdep", | ||
body: "hooray!", | ||
comments: [{ body: "Yay!" }], | ||
}); | ||
|
||
it('decodes circularly depended annotated entity other way around', () => { | ||
const comment = EntityBuilder.buildOne(Comment, { | ||
body: 'hooray!', | ||
blog: { | ||
title: 'Decahedron/Entity gets circdep', | ||
body: 'hooray!', | ||
}, | ||
}); | ||
expect(blog.comments).toBeDefined(); | ||
expect(blog.comments[0]).toBeDefined(); | ||
expect(blog.comments[0].body).toEqual("Yay!"); | ||
}); | ||
|
||
expect(comment.blog).toBeDefined(); | ||
expect(comment.blog.body).toEqual('hooray!'); | ||
it("decodes circularly depended annotated entity other way around", () => { | ||
const comment = EntityBuilder.buildOne(Comment, { | ||
body: "hooray!", | ||
blog: { | ||
title: "Decahedron/Entity gets circdep", | ||
body: "hooray!", | ||
}, | ||
}); | ||
|
||
expect(comment.blog).toBeDefined(); | ||
expect(comment.blog.body).toEqual("hooray!"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { Entity } from '../../src/Entity'; | ||
import { Type } from '../../src/support/Type'; | ||
import Comment from './comment'; | ||
import { Entity } from "../../src"; | ||
import { Type } from "../../src"; | ||
import Comment from "./comment"; | ||
|
||
export class BlogPost extends Entity { | ||
public title: string = null; | ||
public body: string = null; | ||
public title!: string; | ||
public body!: string; | ||
|
||
@Type(() => require('./comment')) | ||
public comments: Comment[] = []; | ||
@Type(() => Comment) | ||
public comments: Comment[] = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Entity } from '../../src/Entity'; | ||
import { Type } from '../../src/support/Type'; | ||
import { BlogPost } from './blog'; | ||
import { Entity } from "../../src"; | ||
import { Type } from "../../src"; | ||
import { BlogPost } from "./blog"; | ||
|
||
export default class Comment extends Entity { | ||
public body: string = null; | ||
public body!: string; | ||
|
||
@Type(() => require('./blog').BlogPost) | ||
public blog: BlogPost = null; | ||
@Type(() => BlogPost) | ||
public blog!: BlogPost; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,37 @@ | ||
import { Type } from '../src/support/Type'; | ||
import { defaultMetadataStorage } from '../src/support/storage'; | ||
import { Entity } from '../src/Entity'; | ||
|
||
class Decorated extends Entity { | ||
|
||
} | ||
|
||
describe('Decorators - Type', () => { | ||
describe('Inferred attribute names', () => { | ||
it('Stores the target type, attribute name and infers the source attribute name', () => { | ||
let decorator = Type(Decorated); | ||
let fn = (): null => null; | ||
decorator(fn as unknown as Entity, 'attribute'); | ||
|
||
let storedMetadata = defaultMetadataStorage.findTypeMetadata(fn.constructor, 'attribute'); | ||
expect(storedMetadata).not.toBeUndefined(); | ||
expect(storedMetadata.propertyName).toEqual('attribute'); | ||
expect(storedMetadata.sourcePropertyName).toEqual('attribute'); | ||
expect(storedMetadata.type).toEqual(Decorated); | ||
}); | ||
|
||
it('Infers that the source name should be snake_case', () => { | ||
let decorator = Type(Decorated); | ||
let fn = (): null => null; | ||
decorator(fn as unknown as Entity, 'camelAttribute'); | ||
|
||
let storedMetadata = defaultMetadataStorage.findTypeMetadata(fn.constructor, 'camel_attribute'); | ||
expect(storedMetadata).not.toBeUndefined(); | ||
expect(storedMetadata.propertyName).toEqual('camelAttribute'); | ||
expect(storedMetadata.sourcePropertyName).toEqual('camel_attribute'); | ||
expect(storedMetadata.type).toEqual(Decorated); | ||
}); | ||
import { Type } from "../src"; | ||
import { defaultMetadataStorage } from "../src/support/storage"; | ||
import { Entity } from "../src"; | ||
|
||
class Decorated extends Entity {} | ||
|
||
describe("Decorators - Type", () => { | ||
describe("Inferred attribute names", () => { | ||
it("Stores the target type, attribute name and infers the source attribute name", () => { | ||
let decorator = Type(Decorated); | ||
let fn = (): null => null; | ||
decorator(fn as unknown as Entity, "attribute"); | ||
|
||
let storedMetadata = defaultMetadataStorage.findTypeMetadata( | ||
fn.constructor, | ||
"attribute", | ||
); | ||
expect(storedMetadata).not.toBeUndefined(); | ||
expect(storedMetadata.propertyName).toEqual("attribute"); | ||
expect(storedMetadata.type).toEqual(Decorated); | ||
}); | ||
|
||
it('Allows manually overriding the source attribute name', () => { | ||
let decorator = Type(Decorated, 'camelAttribute'); | ||
let fn = (): null => null; | ||
decorator(fn as unknown as Entity, 'camelAttribute'); | ||
|
||
let storedMetadata = defaultMetadataStorage.findTypeMetadata(fn.constructor, 'camelAttribute'); | ||
expect(storedMetadata).not.toBeUndefined(); | ||
expect(storedMetadata.propertyName).toEqual('camelAttribute'); | ||
expect(storedMetadata.sourcePropertyName).toEqual('camelAttribute'); | ||
expect(storedMetadata.type).toEqual(Decorated); | ||
it("Infers that the source name should be snake_case", () => { | ||
let decorator = Type(Decorated); | ||
let fn = (): null => null; | ||
decorator(fn as unknown as Entity, "camelAttribute"); | ||
|
||
let storedMetadata = defaultMetadataStorage.findTypeMetadata( | ||
fn.constructor, | ||
"camel_attribute", | ||
); | ||
expect(storedMetadata).not.toBeUndefined(); | ||
expect(storedMetadata.propertyName).toEqual("camel_attribute"); | ||
expect(storedMetadata.type).toEqual(Decorated); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.