From 2884dda9b32c667e8d9ae9f0033f8b665cae57d6 Mon Sep 17 00:00:00 2001 From: shimks Date: Thu, 8 Feb 2018 12:22:27 -0500 Subject: [PATCH] feat(repository): overload app.repository --- packages/repository/src/repository-mixin.ts | 24 ++++++++++---- .../repository-mixin/repository-mixin.test.ts | 33 +++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/packages/repository/src/repository-mixin.ts b/packages/repository/src/repository-mixin.ts index 2994f519d911..1aca3b5f3ef1 100644 --- a/packages/repository/src/repository-mixin.ts +++ b/packages/repository/src/repository-mixin.ts @@ -52,9 +52,15 @@ export function RepositoryMixin>(superClass: T) { * app.repository(NoteRepo); * ``` */ - repository(repo: Class>) { - const repoKey = `repositories.${repo.name}`; - this.bind(repoKey).toClass(repo); + repository(repo: Class>): void; + repository(repos: Class>[]): void; + repository(repo: Class> | Class>[]) { + if (!Array.isArray(repo)) { + const repoKey = `repositories.${repo.name}`; + this.bind(repoKey).toClass(repo); + } else { + repo.map(r => this.repository(r)); + } } /** @@ -77,9 +83,15 @@ export function RepositoryMixin>(superClass: T) { * app.component(ProductComponent); * ``` */ - public component(component: Class) { - super.component(component); - this.mountComponentRepository(component); + public component(component: Class): void; + public component(components: Class[]): void; + public component(component: Class | Class[]) { + if (!Array.isArray(component)) { + super.component(component); + this.mountComponentRepository(component); + } else { + component.map(comp => this.component(comp)); + } } /** diff --git a/packages/repository/test/unit/repository-mixin/repository-mixin.test.ts b/packages/repository/test/unit/repository-mixin/repository-mixin.test.ts index 05031151522f..05f9db4ea3d1 100644 --- a/packages/repository/test/unit/repository-mixin/repository-mixin.test.ts +++ b/packages/repository/test/unit/repository-mixin/repository-mixin.test.ts @@ -34,6 +34,15 @@ describe('RepositoryMixin', () => { expectNoteRepoToBeBound(myApp); }); + it('binds an array of repositories from app.repository()', () => { + const myApp = new AppWithRepoMixin(); + + expectNoteRepoToNotBeBound(myApp); + myApp.repository([NoteRepo, MoreNoteRepo]); + expectNoteRepoToBeBound(myApp); + expectMoreNoteRepoToBeBound(myApp); + }); + it('binds user defined component without repository', () => { class EmptyTestComponent {} @@ -56,6 +65,21 @@ describe('RepositoryMixin', () => { expectNoteRepoToBeBound(myApp); }); + it('binds an array of components and sets up their repositories', () => { + class EmptyTestComponent {} + + const myApp = new AppWithRepoMixin(); + const boundComponentsBefore = myApp.find('components.*').map(b => b.key); + expect(boundComponentsBefore).to.be.empty(); + expectNoteRepoToNotBeBound(myApp); + + myApp.component([EmptyTestComponent, TestComponent]); + + expectComponentToBeBound(myApp, EmptyTestComponent); + expectComponentToBeBound(myApp, TestComponent); + expectNoteRepoToBeBound(myApp); + }); + class AppWithRepoMixin extends RepositoryMixin(Application) {} class NoteRepo implements Repository { @@ -85,6 +109,8 @@ describe('RepositoryMixin', () => { } } + class MoreNoteRepo extends NoteRepo {} + class TestComponent { repositories = [NoteRepo]; } @@ -96,6 +122,13 @@ describe('RepositoryMixin', () => { expect(repoInstance).to.be.instanceOf(NoteRepo); } + function expectMoreNoteRepoToBeBound(myApp: Application) { + const boundRepositories = myApp.find('repositories.*').map(b => b.key); + expect(boundRepositories).to.containEql('repositories.MoreNoteRepo'); + const repoInstance = myApp.getSync('repositories.MoreNoteRepo'); + expect(repoInstance).to.be.instanceOf(MoreNoteRepo); + } + function expectNoteRepoToNotBeBound(myApp: Application) { const boundRepos = myApp.find('repositories.*').map(b => b.key); expect(boundRepos).to.be.empty();