Skip to content

Commit

Permalink
trigger initialize() method in Service and Factory with constructor…
Browse files Browse the repository at this point in the history
… options. (#35)
  • Loading branch information
fahad19 authored Oct 27, 2016
1 parent 74514e5 commit c983747
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/api/createService.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ you can use the lifecycle method `initialize()`.
import { createService } from 'frint';

export default createService({
initialize() {
initialize(options = {}) {
// `options` contains all constructor options
this.myProp = 'something';
},
myMethod() {
Expand Down
2 changes: 1 addition & 1 deletion src/createService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function createService(extend = {}) {
.forEach((prop) => (this[prop] = this[prop].bind(this)));

if (typeof this.initialize === 'function') {
this.initialize();
this.initialize(options);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions test/createFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,22 @@ describe('createFactory', () => {
expect('myCustomFunction' in myFactoryInstance).to.be.equal(true);
expect(myFactoryInstance.myCustomFunction()).to.be.equal('value');
});

it('triggers initialize method with constructor options', () => {
const TestFactory = createFactory({
initialize(options) {
this.storedOptions = options;
}
});

const testFactoryInstance = new TestFactory({
app: true,
foo: 'bar'
});

expect(testFactoryInstance.storedOptions).to.deep.equal({
app: true,
foo: 'bar'
});
});
});
18 changes: 18 additions & 0 deletions test/createService.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ describe('createService', () => {
it('must bind methods to the service instance', () => {
expect(myServiceInstance.returnsThis()).to.be.deep.equal(myServiceInstance);
});

it('triggers initialize method with constructor options', () => {
const TestService = createService({
initialize(options) {
this.storedOptions = options;
}
});

const testServiceInstance = new TestService({
app: true,
foo: 'bar'
});

expect(testServiceInstance.storedOptions).to.deep.equal({
app: true,
foo: 'bar'
});
});
});

0 comments on commit c983747

Please sign in to comment.