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
Raymond Feng
committed
Jun 20, 2017
1 parent
23e37a4
commit af80701
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
packages/repository/test/unit/decorator/repository-with-di.ts
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright IBM Corp. 2013,2017. All Rights Reserved. | ||
// Node module: @loopback/repository | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {expect} from '@loopback/testlab'; | ||
import {Context, inject} from '@loopback/context'; | ||
|
||
import { | ||
Repository, | ||
Entity, | ||
repository, | ||
jugglerModule, | ||
bindModel, | ||
DataSourceConstructor, | ||
juggler, | ||
DefaultCrudRepository, | ||
} from '../../../'; | ||
|
||
class MyController { | ||
constructor( | ||
@repository('noteRepo') public noteRepo: Repository<juggler.PersistedModel>, | ||
) {} | ||
} | ||
|
||
describe('repository class', () => { | ||
let ctx: Context; | ||
|
||
before(function() { | ||
const ds: juggler.DataSource = new DataSourceConstructor({ | ||
name: 'db', | ||
connector: 'memory', | ||
}); | ||
|
||
/* tslint:disable-next-line:variable-name */ | ||
const Note = ds.createModel<typeof juggler.PersistedModel>( | ||
'note', | ||
{title: 'string', content: 'string'}, | ||
{}, | ||
); | ||
|
||
class MyRepository extends DefaultCrudRepository<Entity, string> { | ||
constructor( | ||
@inject('models.Note') myModel: typeof juggler.PersistedModel, | ||
// FIXME For some reason ts-node fails by complaining that | ||
// juggler is undefined if the following is used: | ||
// @inject('dataSources.memory') dataSource: juggler.DataSource | ||
// tslint:disable-next-line:no-any | ||
@inject('dataSources.memory') dataSource: any) { | ||
super(myModel, dataSource); | ||
} | ||
} | ||
ctx = new Context(); | ||
ctx.bind('models.Note').to(Note); | ||
ctx.bind('dataSources.memory').to(ds); | ||
ctx.bind('repositories:noteRepo').toClass(MyRepository); | ||
ctx.bind('controllers:MyController').toClass(MyController); | ||
}); | ||
|
||
// tslint:disable-next-line:max-line-length | ||
it('supports referencing predefined repository by name via constructor', async () => { | ||
const myController: MyController = await ctx.get( | ||
'controllers:MyController', | ||
); | ||
expect(myController.noteRepo instanceof DefaultCrudRepository).to.be.true(); | ||
}); | ||
|
||
}); |
File renamed without changes.