-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make service providers accept a 'config' parameter in their…
… constructor
- Loading branch information
1 parent
55f0e53
commit fc84e1e
Showing
5 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
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
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 @@ | ||
/* eslint-env node, mocha */ | ||
|
||
const { chai } = require("../lib/test-helper"); | ||
const AppServiceProvider = require("./app-service-provider"); | ||
|
||
let expect; | ||
let sp; | ||
const config = { | ||
app: { | ||
name: "Simple Framework", | ||
version: "1.0.0", | ||
}, | ||
}; | ||
|
||
module.exports = { | ||
constructor() { | ||
describe("constructor(config)", function() { | ||
before(async function() { | ||
expect = (await chai()).expect; | ||
sp = new AppServiceProvider(config); | ||
}); | ||
|
||
it("should initialize and return an object", function() { | ||
expect(sp).to.be.an("object"); | ||
}); | ||
|
||
describe("provider object", function() { | ||
it("should have a 'container' method that returns the service container", function() { | ||
expect(sp).to.be.an("object"); | ||
expect(sp).to.have.property("container").to.be.a("function"); | ||
|
||
for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) { | ||
expect(sp.container()).to.have.property(method).to.be.a("function"); | ||
} | ||
}); | ||
|
||
it("should have a 'config' method that returns the passed 'config'", function() { | ||
expect(sp).to.be.an("object"); | ||
expect(sp).to.have.property("config").to.be.a("function"); | ||
expect(sp.config()).to.deep.equal(config); | ||
}); | ||
|
||
it("should have a 'register' method that binds values into the container", function() { | ||
expect(sp).to.be.an("object"); | ||
expect(sp).to.have.property("register").to.be.a("function"); | ||
}); | ||
|
||
describe(".register()", function() { | ||
it("should bind the 'config' and 'registry' into the service container", function() { | ||
//expect(() => sp.container().resolve("config")).to.throw(/Could not resolve 'config'/); | ||
//expect(() => sp.container().resolve("registry")).to.throw(/Could not resolve 'registry'/); | ||
|
||
sp.register(); | ||
|
||
expect(sp.container().resolve("config")).to.deep.equal(config); | ||
|
||
const registry = sp.container().resolve("registry"); | ||
const registryMethods = ["add", "contains", "get", "remove"]; | ||
|
||
for(const method of registryMethods) { | ||
expect(registry).to.have.property(method).to.be.a("function"); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |
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
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,17 @@ | ||
const FrameworkServiceProvider = require("../framework/component/service-provider"); | ||
|
||
class ServiceProvider extends FrameworkServiceProvider { | ||
#config; | ||
|
||
constructor(config) { | ||
super(); | ||
|
||
this.#config = config; | ||
} | ||
|
||
config() { | ||
return this.#config; | ||
} | ||
} | ||
|
||
module.exports = ServiceProvider; |
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,46 @@ | ||
/* eslint-env node, mocha */ | ||
|
||
const { chai } = require("../lib/test-helper"); | ||
const ServiceProvider = require("./service-provider"); | ||
|
||
let expect; | ||
let sp; | ||
const config = { | ||
app: { | ||
name: "Simple Framework", | ||
version: "1.0.0", | ||
}, | ||
}; | ||
|
||
|
||
module.exports = { | ||
constructor() { | ||
describe("constructor(config)", function() { | ||
before(async function() { | ||
expect = (await chai()).expect; | ||
sp = new ServiceProvider(config); | ||
}); | ||
|
||
it("should initialize and return an object", function() { | ||
expect(sp).to.be.an("object"); | ||
}); | ||
|
||
describe("provider object", function() { | ||
it("should have a 'container' method that returns the service container", function() { | ||
expect(sp).to.be.an("object"); | ||
expect(sp).to.have.property("container").to.be.a("function"); | ||
|
||
for(const method of ["bindWithClass", "bindWithFunction", "resolve"]) { | ||
expect(sp.container()).to.have.property(method).to.be.a("function"); | ||
} | ||
}); | ||
|
||
it("should have a 'config' method that returns the passed 'config'", function() { | ||
expect(sp).to.be.an("object"); | ||
expect(sp).to.have.property("config").to.be.a("function"); | ||
expect(sp.config()).to.deep.equal(config); | ||
}); | ||
}); | ||
}); | ||
} | ||
}; |