Skip to content

Commit

Permalink
feat(repository): overload app.repository
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Feb 8, 2018
1 parent 0b6e0e5 commit 2884dda
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/repository/src/repository-mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {
* app.repository(NoteRepo);
* ```
*/
repository(repo: Class<Repository<any>>) {
const repoKey = `repositories.${repo.name}`;
this.bind(repoKey).toClass(repo);
repository(repo: Class<Repository<any>>): void;
repository(repos: Class<Repository<any>>[]): void;
repository(repo: Class<Repository<any>> | Class<Repository<any>>[]) {
if (!Array.isArray(repo)) {
const repoKey = `repositories.${repo.name}`;
this.bind(repoKey).toClass(repo);
} else {
repo.map(r => this.repository(r));
}
}

/**
Expand All @@ -77,9 +83,15 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {
* app.component(ProductComponent);
* ```
*/
public component(component: Class<any>) {
super.component(component);
this.mountComponentRepository(component);
public component(component: Class<any>): void;
public component(components: Class<any>[]): void;
public component(component: Class<any> | Class<any>[]) {
if (!Array.isArray(component)) {
super.component(component);
this.mountComponentRepository(component);
} else {
component.map(comp => this.component(comp));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand All @@ -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<juggler.PersistedModel> {
Expand Down Expand Up @@ -85,6 +109,8 @@ describe('RepositoryMixin', () => {
}
}

class MoreNoteRepo extends NoteRepo {}

class TestComponent {
repositories = [NoteRepo];
}
Expand All @@ -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();
Expand Down

0 comments on commit 2884dda

Please sign in to comment.