Skip to content

Commit

Permalink
tests(react-binding): add tests for connectEntityAtField
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Jul 1, 2024
1 parent 7e9558e commit ab28a29
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,49 @@ describe('entity operations', () => {
entity.getAccessor().getField('fooField').updateValue(null)
expect(entity.unpersistedChangesCount).eq(0)
})

it('fails when relation not defined in static render', () => {
const { treeStore, environment } = createBinding({
node: (<>
<EntitySubTree entity="Article(id = 'cfb8d0ae-c892-4047-acfb-a89adab2371d')" alias="article">
</EntitySubTree>
<EntitySubTree entity="Category(id = '89560cfa-f874-42b6-ace3-35a8ebcbba15')" alias="category">
</EntitySubTree>
</>),
schema: convertModelToAdminSchema(createSchema(ModelWithRelation).model),
})
const article = treeStore.getSubTreeState('entity', undefined, 'article', environment)
const category = treeStore.getSubTreeState('entity', undefined, 'category', environment)
expect(() => {
article.getAccessor().connectEntityAtField('category', category.getAccessor())
}).toThrowError('Cannot connect at field \'category\' as it wasn\'t registered during static render.')
})

it('ok when relation defined in static render', () => {
const { treeStore, environment } = createBinding({
node: (<>
<EntitySubTree entity="Article(id = 'cfb8d0ae-c892-4047-acfb-a89adab2371d')" alias="article">
<HasOne field="category" />
</EntitySubTree>
<EntitySubTree entity="Category(id = '89560cfa-f874-42b6-ace3-35a8ebcbba15')" alias="category">
</EntitySubTree>
</>),
schema: convertModelToAdminSchema(createSchema(ModelWithRelation).model),
})
const article = treeStore.getSubTreeState('entity', undefined, 'article', environment)
const category = treeStore.getSubTreeState('entity', undefined, 'category', environment)
article.getAccessor().connectEntityAtField('category', category.getAccessor())
})
})



namespace ModelWithRelation {
export class Category {
articles = c.oneHasMany(Article, 'category')
}

export class Article {
category = c.manyHasOne(Category, 'articles')
}
}

0 comments on commit ab28a29

Please sign in to comment.