2020-05-14 10:04:08 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { next } from "@ember/runloop";
|
2018-06-15 11:03:24 -04:00
|
|
|
import { moduleForWidget, widgetTest } from "helpers/widget-test";
|
|
|
|
import { createWidget } from "discourse/widgets/widget";
|
|
|
|
import { withPluginApi } from "discourse/lib/plugin-api";
|
2020-02-05 11:14:42 -05:00
|
|
|
import { Promise } from "rsvp";
|
2018-06-15 11:03:24 -04:00
|
|
|
import hbs from "discourse/widgets/hbs-compiler";
|
2016-02-26 14:01:20 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
moduleForWidget("base");
|
2016-02-26 14:01:20 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget attributes are passed in via args", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="hello-test" args=args}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hello-test", {
|
|
|
|
tagName: "div.test",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`Hello {{attrs.name}}`
|
2016-02-26 14:01:20 -05:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { name: "Robin" });
|
2016-02-26 14:01:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find(".test").text(), "Hello Robin");
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-29 16:22:19 -04:00
|
|
|
widgetTest("hbs template - no tagName", {
|
|
|
|
template: `{{mount-widget widget="hbs-test" args=args}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hbs-test", {
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`<div class='test'>Hello {{attrs.name}}</div>`
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { name: "Robin" });
|
2017-06-29 16:22:19 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find("div.test").text(), "Hello Robin");
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
widgetTest("hbs template - with tagName", {
|
|
|
|
template: `{{mount-widget widget="hbs-test" args=args}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hbs-test", {
|
|
|
|
tagName: "div.test",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`Hello {{attrs.name}}`
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { name: "Robin" });
|
2017-06-29 16:22:19 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find("div.test").text(), "Hello Robin");
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("buildClasses", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="classname-test" args=args}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("classname-test", {
|
|
|
|
tagName: "div.test",
|
2016-02-26 14:01:20 -05:00
|
|
|
|
|
|
|
buildClasses(attrs) {
|
2018-06-15 11:03:24 -04:00
|
|
|
return ["static", attrs.dynamic];
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { dynamic: "cool-class" });
|
2016-02-26 14:01:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find(".test.static.cool-class").length, "it has all the classes");
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("buildAttributes", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="attributes-test" args=args}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("attributes-test", {
|
|
|
|
tagName: "div.test",
|
2016-02-26 14:01:20 -05:00
|
|
|
|
|
|
|
buildAttributes(attrs) {
|
2018-06-15 11:03:24 -04:00
|
|
|
return { "data-evil": "trout", "aria-label": attrs.label };
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { label: "accessibility" });
|
2016-02-26 14:01:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find(".test[data-evil=trout]").length);
|
|
|
|
assert.ok(find(".test[aria-label=accessibility]").length);
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("buildId", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="id-test" args=args}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("id-test", {
|
2016-02-26 14:01:20 -05:00
|
|
|
buildId(attrs) {
|
|
|
|
return `test-${attrs.id}`;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { id: 1234 });
|
2016-02-26 14:01:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find("#test-1234").length);
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget state", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="state-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("state-test", {
|
|
|
|
tagName: "button.test",
|
2016-04-14 15:23:05 -04:00
|
|
|
buildKey: () => `button-test`,
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`{{state.clicks}} clicks`,
|
2016-02-26 14:01:20 -05:00
|
|
|
|
|
|
|
defaultState() {
|
|
|
|
return { clicks: 0 };
|
|
|
|
},
|
|
|
|
|
|
|
|
click() {
|
|
|
|
this.state.clicks++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-24 11:49:42 -04:00
|
|
|
async test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find("button.test").length, "it renders the button");
|
|
|
|
assert.equal(find("button.test").text(), "0 clicks");
|
2016-02-26 14:01:20 -05:00
|
|
|
|
2019-02-25 10:04:55 -05:00
|
|
|
await click(find("button"));
|
|
|
|
assert.equal(find("button.test").text(), "1 clicks");
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget update with promise", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="promise-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("promise-test", {
|
|
|
|
tagName: "button.test",
|
|
|
|
buildKey: () => "promise-test",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`
|
|
|
|
{{#if state.name}}
|
|
|
|
{{state.name}}
|
|
|
|
{{else}}
|
|
|
|
No name
|
|
|
|
{{/if}}
|
|
|
|
`,
|
2016-02-26 14:01:20 -05:00
|
|
|
|
|
|
|
click() {
|
2020-02-05 11:14:42 -05:00
|
|
|
return new Promise(resolve => {
|
2019-10-30 09:48:24 -04:00
|
|
|
next(() => {
|
2016-02-26 14:01:20 -05:00
|
|
|
this.state.name = "Robin";
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-07-24 11:49:42 -04:00
|
|
|
async test(assert) {
|
2018-06-15 11:03:24 -04:00
|
|
|
assert.equal(
|
2019-02-25 10:04:55 -05:00
|
|
|
find("button.test")
|
2018-06-15 11:03:24 -04:00
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
"No name"
|
|
|
|
);
|
|
|
|
|
2019-02-25 10:04:55 -05:00
|
|
|
await click(find("button"));
|
2018-07-24 11:49:42 -04:00
|
|
|
assert.equal(
|
2019-02-25 10:04:55 -05:00
|
|
|
find("button.test")
|
2018-07-24 11:49:42 -04:00
|
|
|
.text()
|
|
|
|
.trim(),
|
|
|
|
"Robin"
|
|
|
|
);
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget attaching", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="attach-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("test-embedded", { tagName: "div.embedded" });
|
2016-02-26 14:01:20 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("attach-test", {
|
|
|
|
tagName: "div.container",
|
2017-09-28 16:08:14 -04:00
|
|
|
template: hbs`{{attach widget="test-embedded" attrs=attrs}}`
|
2016-02-26 14:01:20 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find(".container").length, "renders container");
|
|
|
|
assert.ok(find(".container .embedded").length, "renders attached");
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-05-01 18:31:01 -04:00
|
|
|
widgetTest("magic attaching by name", {
|
|
|
|
template: `{{mount-widget widget="attach-test"}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
createWidget("test-embedded", { tagName: "div.embedded" });
|
|
|
|
|
|
|
|
createWidget("attach-test", {
|
|
|
|
tagName: "div.container",
|
|
|
|
template: hbs`{{test-embedded attrs=attrs}}`
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
|
|
|
assert.ok(find(".container").length, "renders container");
|
|
|
|
assert.ok(find(".container .embedded").length, "renders attached");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
widgetTest("custom attrs to a magic attached widget", {
|
|
|
|
template: `{{mount-widget widget="attach-test"}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
createWidget("testing", {
|
|
|
|
tagName: "span.value",
|
|
|
|
template: hbs`{{attrs.value}}`
|
|
|
|
});
|
|
|
|
|
|
|
|
createWidget("attach-test", {
|
|
|
|
tagName: "div.container",
|
|
|
|
template: hbs`{{testing value=(concat "hello" " " "world")}}`
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
|
|
|
assert.ok(find(".container").length, "renders container");
|
|
|
|
assert.equal(find(".container .value").text(), "hello world");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-09-14 11:20:19 -04:00
|
|
|
widgetTest("handlebars d-icon", {
|
2017-06-29 16:22:19 -04:00
|
|
|
template: `{{mount-widget widget="hbs-icon-test" args=args}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hbs-icon-test", {
|
2017-09-14 11:20:19 -04:00
|
|
|
template: hbs`{{d-icon "arrow-down"}}`
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find(".d-icon-arrow-down").length, 1);
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
widgetTest("handlebars i18n", {
|
2019-05-23 15:23:31 -04:00
|
|
|
_translations: I18n.translations,
|
|
|
|
|
2017-06-29 16:22:19 -04:00
|
|
|
template: `{{mount-widget widget="hbs-i18n-test" args=args}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hbs-i18n-test", {
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`
|
|
|
|
<span class='string'>{{i18n "hbs_test0"}}</span>
|
|
|
|
<span class='var'>{{i18n attrs.key}}</span>
|
|
|
|
<a href title={{i18n "hbs_test0"}}>test</a>
|
|
|
|
`
|
|
|
|
});
|
2019-05-23 15:23:31 -04:00
|
|
|
I18n.translations = {
|
|
|
|
en: {
|
|
|
|
js: {
|
|
|
|
hbs_test0: "evil",
|
|
|
|
hbs_test1: "trout"
|
|
|
|
}
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
2019-05-23 15:23:31 -04:00
|
|
|
};
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", { key: "hbs_test1" });
|
2017-06-29 16:22:19 -04:00
|
|
|
},
|
|
|
|
|
2019-05-23 15:23:31 -04:00
|
|
|
afterEach() {
|
|
|
|
I18n.translations = this._translations;
|
|
|
|
},
|
|
|
|
|
2017-06-29 16:22:19 -04:00
|
|
|
test(assert) {
|
|
|
|
// comin up
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find("span.string").text(), "evil");
|
|
|
|
assert.equal(find("span.var").text(), "trout");
|
|
|
|
assert.equal(find("a").prop("title"), "evil");
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("handlebars #each", {
|
2017-06-29 16:22:19 -04:00
|
|
|
template: `{{mount-widget widget="hbs-each-test" args=args}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("hbs-each-test", {
|
|
|
|
tagName: "ul",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`
|
|
|
|
{{#each attrs.items as |item|}}
|
|
|
|
<li>{{item}}</li>
|
|
|
|
{{/each}}
|
|
|
|
`
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
this.set("args", {
|
|
|
|
items: ["one", "two", "three"]
|
2017-06-29 16:22:19 -04:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find("ul li").length, 3);
|
|
|
|
assert.equal(find("ul li:eq(0)").text(), "one");
|
2017-06-29 16:22:19 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget decorating", {
|
2016-02-26 14:01:20 -05:00
|
|
|
template: `{{mount-widget widget="decorate-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("decorate-test", {
|
|
|
|
tagName: "div.decorate",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`main content`
|
2016-02-26 14:01:20 -05:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
withPluginApi("0.1", api => {
|
|
|
|
api.decorateWidget("decorate-test:before", dec => {
|
|
|
|
return dec.h("b", "before");
|
2016-02-26 14:26:29 -05:00
|
|
|
});
|
2016-02-26 14:01:20 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
api.decorateWidget("decorate-test:after", dec => {
|
|
|
|
return dec.h("i", "after");
|
2016-02-26 14:26:29 -05:00
|
|
|
});
|
2016-02-26 14:01:20 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.ok(find(".decorate").length);
|
|
|
|
assert.equal(find(".decorate b").text(), "before");
|
|
|
|
assert.equal(find(".decorate i").text(), "after");
|
2016-02-26 14:01:20 -05:00
|
|
|
}
|
|
|
|
});
|
2016-02-26 14:26:29 -05:00
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("widget settings", {
|
2016-02-26 14:26:29 -05:00
|
|
|
template: `{{mount-widget widget="settings-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("settings-test", {
|
|
|
|
tagName: "div.settings",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`age is {{settings.age}}`,
|
|
|
|
settings: { age: 36 }
|
2016-02-26 14:26:29 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find(".settings").text(), "age is 36");
|
2016-02-26 14:26:29 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
widgetTest("override settings", {
|
2016-02-26 14:26:29 -05:00
|
|
|
template: `{{mount-widget widget="ov-settings-test"}}`,
|
|
|
|
|
2017-06-14 13:57:58 -04:00
|
|
|
beforeEach() {
|
2018-06-15 11:03:24 -04:00
|
|
|
createWidget("ov-settings-test", {
|
|
|
|
tagName: "div.settings",
|
2017-06-29 16:22:19 -04:00
|
|
|
template: hbs`age is {{settings.age}}`,
|
2018-06-15 11:03:24 -04:00
|
|
|
settings: { age: 36 }
|
2016-02-26 14:26:29 -05:00
|
|
|
});
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
withPluginApi("0.1", api => {
|
|
|
|
api.changeWidgetSetting("ov-settings-test", "age", 37);
|
2016-02-26 14:26:29 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
2019-02-25 10:04:55 -05:00
|
|
|
assert.equal(find(".settings").text(), "age is 37");
|
2016-02-26 14:26:29 -05:00
|
|
|
}
|
|
|
|
});
|
2019-12-17 12:39:51 -05:00
|
|
|
|
|
|
|
widgetTest("get accessor", {
|
|
|
|
template: `{{mount-widget widget="get-accessor-test"}}`,
|
|
|
|
|
|
|
|
beforeEach() {
|
|
|
|
createWidget("get-accessor-test", {
|
|
|
|
tagName: "div.test",
|
|
|
|
template: hbs`Hello {{transformed.name}}`,
|
|
|
|
transform() {
|
|
|
|
return {
|
|
|
|
name: this.get("currentUser.username")
|
|
|
|
};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
test(assert) {
|
|
|
|
assert.equal(find("div.test").text(), "Hello eviltrout");
|
|
|
|
}
|
|
|
|
});
|