Skip to content

Commit c18a2f9

Browse files
committed
feat: add test helper for stubbing services
1 parent 96a7459 commit c18a2f9

File tree

9 files changed

+147
-29
lines changed

9 files changed

+147
-29
lines changed

.eslintrc.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,46 @@ module.exports = {
22
root: true,
33
parserOptions: {
44
ecmaVersion: 2017,
5-
sourceType: 'module'
5+
sourceType: "module"
66
},
7-
plugins: [
8-
'ember'
9-
],
10-
extends: [
11-
'eslint:recommended',
12-
'plugin:ember/recommended'
13-
],
7+
plugins: ["ember"],
8+
extends: ["eslint:recommended", "plugin:ember/recommended"],
149
env: {
1510
browser: true
1611
},
17-
rules: {
18-
},
12+
rules: {},
1913
overrides: [
2014
// node files
2115
{
2216
files: [
23-
'index.js',
24-
'testem.js',
25-
'ember-cli-build.js',
26-
'config/**/*.js',
27-
'tests/dummy/config/**/*.js'
17+
"index.js",
18+
"testem.js",
19+
"ember-cli-build.js",
20+
"config/**/*.js",
21+
"tests/dummy/config/**/*.js"
2822
],
2923
excludedFiles: [
30-
'app/**',
31-
'addon/**',
32-
'tests/dummy/app/**'
24+
"app/**",
25+
"addon/**",
26+
"tests/dummy/app/**",
27+
"addon-test-support/**"
3328
],
3429
parserOptions: {
35-
sourceType: 'script',
30+
sourceType: "script",
3631
ecmaVersion: 2015
3732
},
3833
env: {
3934
browser: false,
4035
node: true
4136
},
42-
plugins: ['node'],
43-
rules: Object.assign({}, require('eslint-plugin-node').configs.recommended.rules, {
44-
// add your custom rules and overrides for node files here
45-
})
37+
plugins: ["node"],
38+
rules: Object.assign(
39+
{},
40+
require("eslint-plugin-node").configs.recommended.rules,
41+
{
42+
// add your custom rules and overrides for node files here
43+
}
44+
)
4645
}
4746
]
4847
};

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,30 @@ Then import as follows:
1414
import td from 'testdouble';
1515
```
1616

17-
License
18-
------------------------------------------------------------------------------
17+
## Test Helpers
18+
19+
### `stubService`
20+
21+
Included is a function that can replace a service with one stubbed using [`td.object()`][td-object].
22+
23+
```js
24+
import td from "testdouble";
25+
import { stubService } from "ember-cli-testdouble/test-support";
26+
27+
test("verifying a method invocation", function(assert) {
28+
assert.expect(0); // Won't actually use `assert` in this test
29+
30+
stubService("some-service");
31+
32+
let someService = this.owner.lookup("service:some-service");
33+
someService.method();
34+
35+
td.verify(someService.method()); // Passes!
36+
});
37+
```
38+
39+
## License
1940

2041
This project is licensed under the [MIT License](LICENSE.md).
42+
43+
[td-object]: https://github.com/testdouble/testdouble.js/blob/master/docs/4-creating-test-doubles.md#tdobject

addon-test-support/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as stubService } from "./stub-service";

addon-test-support/stub-service.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { getContext } from "@ember/test-helpers";
2+
import { run } from "@ember/runloop";
3+
import td from "testdouble";
4+
5+
function replace(owner, name) {
6+
const serviceName = `service:${name}`;
7+
const original = owner.lookup(serviceName);
8+
const replacement = td.object(original);
9+
10+
run(() => {
11+
owner.unregister(serviceName);
12+
owner.register(serviceName, replacement, { instantiate: false });
13+
});
14+
15+
return replacement;
16+
}
17+
18+
/**
19+
* Replace a service with a stubbed version of itself
20+
*
21+
* Each method on the service will be replaced with a TestDouble function
22+
*/
23+
export default function stubService() {
24+
if (arguments.length === 2) {
25+
let [hooks, name] = arguments;
26+
27+
hooks.beforeEach(function() {
28+
replace(this.owner, name);
29+
});
30+
31+
hooks.afterEach(function() {
32+
td.reset();
33+
});
34+
} else if (arguments.length === 1) {
35+
let [name] = arguments;
36+
let { owner } = getContext();
37+
38+
replace(owner, name);
39+
} else {
40+
throw new Error("Unexpected number of arguments");
41+
}
42+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"ember-cli-qunit": "^4.1.1",
4343
"ember-cli-shims": "^1.2.0",
4444
"ember-cli-sri": "^2.1.0",
45+
"ember-cli-testdouble-qunit": "^2.0.2",
4546
"ember-cli-uglify": "^2.0.0",
4647
"ember-disable-prototype-extensions": "^1.1.2",
4748
"ember-export-application-global": "^2.0.0",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Service from "@ember/service";
2+
3+
export default Service.extend({
4+
method() {
5+
// This will be stubbed in a test
6+
}
7+
});

tests/test-helper.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import Application from '../app';
2-
import config from '../config/environment';
3-
import { setApplication } from '@ember/test-helpers';
4-
import { start } from 'ember-qunit';
1+
import Application from "../app";
2+
import config from "../config/environment";
3+
import { setApplication } from "@ember/test-helpers";
4+
import { start } from "ember-qunit";
5+
6+
import "ember-cli-testdouble-qunit";
57

68
setApplication(Application.create(config.APP));
79

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { module, test } from "qunit";
2+
import { setupTest } from "ember-qunit";
3+
import { stubService } from "ember-cli-testdouble/test-support";
4+
5+
module("Test Helpers | stub-service", function(hooks) {
6+
setupTest(hooks);
7+
8+
module("as a `hooks` reciever", function(hooks) {
9+
stubService(hooks, "to-stub");
10+
11+
test("it can replace a service", function(assert) {
12+
let service = this.owner.lookup("service:to-stub");
13+
service.method();
14+
15+
assert.verify(service.method());
16+
});
17+
});
18+
19+
module("invoking without `hooks`", function(hooks) {
20+
hooks.beforeEach(function() {
21+
stubService("to-stub");
22+
});
23+
24+
test("it can replace a service", function(assert) {
25+
let service = this.owner.lookup("service:to-stub");
26+
service.method();
27+
28+
assert.verify(service.method());
29+
});
30+
});
31+
});

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,14 @@ ember-cli-test-loader@^2.2.0:
20712071
dependencies:
20722072
ember-cli-babel "^6.8.1"
20732073

2074+
ember-cli-testdouble-qunit@^2.0.2:
2075+
version "2.0.2"
2076+
resolved "https://registry.yarnpkg.com/ember-cli-testdouble-qunit/-/ember-cli-testdouble-qunit-2.0.2.tgz#4edbe71e71d1abff101cb36e780a24586e8ba829"
2077+
dependencies:
2078+
broccoli-funnel "^2.0.1"
2079+
ember-cli-babel "^6.6.0"
2080+
testdouble-qunit "^2.0.2"
2081+
20742082
ember-cli-uglify@^2.0.0:
20752083
version "2.0.2"
20762084
resolved "https://registry.yarnpkg.com/ember-cli-uglify/-/ember-cli-uglify-2.0.2.tgz#12becdaf1a2e6f0cdbd386b1d5f5a2d0540347d3"
@@ -5896,6 +5904,10 @@ temp@0.8.3:
58965904
os-tmpdir "^1.0.0"
58975905
rimraf "~2.2.6"
58985906

5907+
testdouble-qunit@^2.0.2:
5908+
version "2.0.2"
5909+
resolved "https://registry.yarnpkg.com/testdouble-qunit/-/testdouble-qunit-2.0.2.tgz#740393c867901b9a814882b50f4c94a9e343fab9"
5910+
58995911
testdouble@^3.5.2:
59005912
version "3.5.2"
59015913
resolved "https://registry.yarnpkg.com/testdouble/-/testdouble-3.5.2.tgz#7ac91d08be05bac3b2acba57c430f6c62d0ad8af"

0 commit comments

Comments
 (0)