-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add test helper for stubbing services
- Loading branch information
1 parent
96a7459
commit c18a2f9
Showing
9 changed files
with
147 additions
and
29 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
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 @@ | ||
export { default as stubService } from "./stub-service"; |
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,42 @@ | ||
import { getContext } from "@ember/test-helpers"; | ||
import { run } from "@ember/runloop"; | ||
import td from "testdouble"; | ||
|
||
function replace(owner, name) { | ||
const serviceName = `service:${name}`; | ||
const original = owner.lookup(serviceName); | ||
const replacement = td.object(original); | ||
|
||
run(() => { | ||
owner.unregister(serviceName); | ||
owner.register(serviceName, replacement, { instantiate: false }); | ||
}); | ||
|
||
return replacement; | ||
} | ||
|
||
/** | ||
* Replace a service with a stubbed version of itself | ||
* | ||
* Each method on the service will be replaced with a TestDouble function | ||
*/ | ||
export default function stubService() { | ||
if (arguments.length === 2) { | ||
let [hooks, name] = arguments; | ||
|
||
hooks.beforeEach(function() { | ||
replace(this.owner, name); | ||
}); | ||
|
||
hooks.afterEach(function() { | ||
td.reset(); | ||
}); | ||
} else if (arguments.length === 1) { | ||
let [name] = arguments; | ||
let { owner } = getContext(); | ||
|
||
replace(owner, name); | ||
} else { | ||
throw new Error("Unexpected number of arguments"); | ||
} | ||
} |
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,7 @@ | ||
import Service from "@ember/service"; | ||
|
||
export default Service.extend({ | ||
method() { | ||
// This will be stubbed in a test | ||
} | ||
}); |
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,31 @@ | ||
import { module, test } from "qunit"; | ||
import { setupTest } from "ember-qunit"; | ||
import { stubService } from "ember-cli-testdouble/test-support"; | ||
|
||
module("Test Helpers | stub-service", function(hooks) { | ||
setupTest(hooks); | ||
|
||
module("as a `hooks` reciever", function(hooks) { | ||
stubService(hooks, "to-stub"); | ||
|
||
test("it can replace a service", function(assert) { | ||
let service = this.owner.lookup("service:to-stub"); | ||
service.method(); | ||
|
||
assert.verify(service.method()); | ||
}); | ||
}); | ||
|
||
module("invoking without `hooks`", function(hooks) { | ||
hooks.beforeEach(function() { | ||
stubService("to-stub"); | ||
}); | ||
|
||
test("it can replace a service", function(assert) { | ||
let service = this.owner.lookup("service:to-stub"); | ||
service.method(); | ||
|
||
assert.verify(service.method()); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -2071,6 +2071,14 @@ ember-cli-test-loader@^2.2.0: | |
dependencies: | ||
ember-cli-babel "^6.8.1" | ||
|
||
ember-cli-testdouble-qunit@^2.0.2: | ||
version "2.0.2" | ||
resolved "https://registry.yarnpkg.com/ember-cli-testdouble-qunit/-/ember-cli-testdouble-qunit-2.0.2.tgz#4edbe71e71d1abff101cb36e780a24586e8ba829" | ||
dependencies: | ||
broccoli-funnel "^2.0.1" | ||
ember-cli-babel "^6.6.0" | ||
testdouble-qunit "^2.0.2" | ||
|
||
ember-cli-uglify@^2.0.0: | ||
version "2.0.2" | ||
resolved "https://registry.yarnpkg.com/ember-cli-uglify/-/ember-cli-uglify-2.0.2.tgz#12becdaf1a2e6f0cdbd386b1d5f5a2d0540347d3" | ||
|
@@ -5896,6 +5904,10 @@ [email protected]: | |
os-tmpdir "^1.0.0" | ||
rimraf "~2.2.6" | ||
|
||
testdouble-qunit@^2.0.2: | ||
version "2.0.2" | ||
resolved "https://registry.yarnpkg.com/testdouble-qunit/-/testdouble-qunit-2.0.2.tgz#740393c867901b9a814882b50f4c94a9e343fab9" | ||
|
||
testdouble@^3.5.2: | ||
version "3.5.2" | ||
resolved "https://registry.yarnpkg.com/testdouble/-/testdouble-3.5.2.tgz#7ac91d08be05bac3b2acba57c430f6c62d0ad8af" | ||
|