Using arrow functions changes `this` context, which is undesired in tests, e.g. it makes it impossible to setup things like pretender (`this.server`) in `beforeEach` hooks. Ember guides always use classic functions in examples (e.g. https://guides.emberjs.com/release/testing/test-types/), and that's what it uses in its own test suite, as do various addons and ember apps. It was also already used in Discourse where `this` was required. Moving forward, it will be needed in more places as we migrate toward ember-cli. (I might later add a custom rule to eslint-discourse-ember to enforce this)
96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
import { queryAll } from "discourse/tests/helpers/qunit-helpers";
|
|
import { visit, click } from "@ember/test-helpers";
|
|
import { test } from "qunit";
|
|
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
|
|
import { extraConnectorClass } from "discourse/lib/plugin-connectors";
|
|
import { action } from "@ember/object";
|
|
|
|
const PREFIX = "javascripts/single-test/connectors";
|
|
|
|
acceptance("Plugin Outlet - Connector Class", function (needs) {
|
|
needs.hooks.beforeEach(() => {
|
|
extraConnectorClass("user-profile-primary/hello", {
|
|
actions: {
|
|
sayHello() {
|
|
this.set("hello", "hello!");
|
|
},
|
|
},
|
|
});
|
|
|
|
extraConnectorClass("user-profile-primary/hi", {
|
|
setupComponent() {
|
|
this.appEvents.on("hi:sayHi", this, this.say);
|
|
},
|
|
|
|
teardownComponent() {
|
|
this.appEvents.off("hi:sayHi", this, this.say);
|
|
},
|
|
|
|
@action
|
|
say() {
|
|
this.set("hi", "hi!");
|
|
},
|
|
|
|
@action
|
|
sayHi() {
|
|
this.appEvents.trigger("hi:sayHi");
|
|
},
|
|
});
|
|
|
|
extraConnectorClass("user-profile-primary/dont-render", {
|
|
shouldRender(args) {
|
|
return args.model.get("username") !== "eviltrout";
|
|
},
|
|
});
|
|
|
|
Ember.TEMPLATES[
|
|
`${PREFIX}/user-profile-primary/hello`
|
|
] = Ember.HTMLBars.compile(
|
|
`<span class='hello-username'>{{model.username}}</span>
|
|
<button class='say-hello' {{action "sayHello"}}></button>
|
|
<span class='hello-result'>{{hello}}</span>`
|
|
);
|
|
Ember.TEMPLATES[
|
|
`${PREFIX}/user-profile-primary/hi`
|
|
] = Ember.HTMLBars.compile(
|
|
`<button class='say-hi' {{action "sayHi"}}></button>
|
|
<span class='hi-result'>{{hi}}</span>`
|
|
);
|
|
Ember.TEMPLATES[
|
|
`${PREFIX}/user-profile-primary/dont-render`
|
|
] = Ember.HTMLBars.compile(`I'm not rendered!`);
|
|
});
|
|
|
|
needs.hooks.afterEach(() => {
|
|
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/hello`];
|
|
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/hi`];
|
|
delete Ember.TEMPLATES[`${PREFIX}/user-profile-primary/dont-render`];
|
|
});
|
|
|
|
test("Renders a template into the outlet", async function (assert) {
|
|
await visit("/u/eviltrout");
|
|
assert.ok(
|
|
queryAll(".user-profile-primary-outlet.hello").length === 1,
|
|
"it has class names"
|
|
);
|
|
assert.ok(
|
|
!queryAll(".user-profile-primary-outlet.dont-render").length,
|
|
"doesn't render"
|
|
);
|
|
|
|
await click(".say-hello");
|
|
assert.equal(
|
|
queryAll(".hello-result").text(),
|
|
"hello!",
|
|
"actions delegate properly"
|
|
);
|
|
|
|
await click(".say-hi");
|
|
assert.equal(
|
|
queryAll(".hi-result").text(),
|
|
"hi!",
|
|
"actions delegate properly"
|
|
);
|
|
});
|
|
});
|