discourse/test/javascripts/widgets/widget-test.js.es6

338 lines
7.4 KiB
Plaintext
Raw Normal View History

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";
import hbs from "discourse/widgets/hbs-compiler";
2018-06-15 11:03:24 -04:00
moduleForWidget("base");
2018-06-15 11:03:24 -04:00
widgetTest("widget attributes are passed in via args", {
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",
template: hbs`Hello {{attrs.name}}`
});
2018-06-15 11:03:24 -04:00
this.set("args", { name: "Robin" });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$(".test").text(), "Hello Robin");
}
});
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", {
template: hbs`<div class='test'>Hello {{attrs.name}}</div>`
});
2018-06-15 11:03:24 -04:00
this.set("args", { name: "Robin" });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$("div.test").text(), "Hello Robin");
}
});
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",
template: hbs`Hello {{attrs.name}}`
});
2018-06-15 11:03:24 -04:00
this.set("args", { name: "Robin" });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$("div.test").text(), "Hello Robin");
}
});
2018-06-15 11:03:24 -04:00
widgetTest("buildClasses", {
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",
buildClasses(attrs) {
2018-06-15 11:03:24 -04:00
return ["static", attrs.dynamic];
}
});
2018-06-15 11:03:24 -04:00
this.set("args", { dynamic: "cool-class" });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(
this.$(".test.static.cool-class").length,
"it has all the classes"
);
}
});
2018-06-15 11:03:24 -04:00
widgetTest("buildAttributes", {
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",
buildAttributes(attrs) {
2018-06-15 11:03:24 -04:00
return { "data-evil": "trout", "aria-label": attrs.label };
}
});
2018-06-15 11:03:24 -04:00
this.set("args", { label: "accessibility" });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(this.$(".test[data-evil=trout]").length);
assert.ok(this.$(".test[aria-label=accessibility]").length);
}
});
2018-06-15 11:03:24 -04:00
widgetTest("buildId", {
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", {
buildId(attrs) {
return `test-${attrs.id}`;
}
});
2018-06-15 11:03:24 -04:00
this.set("args", { id: 1234 });
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(this.$("#test-1234").length);
}
});
2018-06-15 11:03:24 -04:00
widgetTest("widget state", {
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",
buildKey: () => `button-test`,
template: hbs`{{state.clicks}} clicks`,
defaultState() {
return { clicks: 0 };
},
click() {
this.state.clicks++;
}
});
},
async test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(this.$("button.test").length, "it renders the button");
assert.equal(this.$("button.test").text(), "0 clicks");
await click(this.$("button"));
assert.equal(this.$("button.test").text(), "1 clicks");
}
});
2018-06-15 11:03:24 -04:00
widgetTest("widget update with promise", {
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",
template: hbs`
{{#if state.name}}
{{state.name}}
{{else}}
No name
{{/if}}
`,
click() {
return new Ember.RSVP.Promise(resolve => {
Ember.run.next(() => {
this.state.name = "Robin";
resolve();
});
});
}
});
},
async test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(
this.$("button.test")
.text()
.trim(),
"No name"
);
await click(this.$("button"));
assert.equal(
this.$("button.test")
.text()
.trim(),
"Robin"
);
}
});
2018-06-15 11:03:24 -04:00
widgetTest("widget attaching", {
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" });
2018-06-15 11:03:24 -04:00
createWidget("attach-test", {
tagName: "div.container",
template: hbs`{{attach widget="test-embedded" attrs=attrs}}`
});
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(this.$(".container").length, "renders container");
assert.ok(this.$(".container .embedded").length, "renders attached");
}
});
widgetTest("handlebars d-icon", {
template: `{{mount-widget widget="hbs-icon-test" args=args}}`,
beforeEach() {
2018-06-15 11:03:24 -04:00
createWidget("hbs-icon-test", {
template: hbs`{{d-icon "arrow-down"}}`
});
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$("i.fa.fa-arrow-down").length, 1);
}
});
widgetTest("handlebars i18n", {
template: `{{mount-widget widget="hbs-i18n-test" args=args}}`,
beforeEach() {
2018-06-15 11:03:24 -04:00
createWidget("hbs-i18n-test", {
template: hbs`
<span class='string'>{{i18n "hbs_test0"}}</span>
<span class='var'>{{i18n attrs.key}}</span>
<a href title={{i18n "hbs_test0"}}>test</a>
`
});
2018-06-15 11:03:24 -04:00
I18n.extras = [
{
hbs_test0: "evil",
hbs_test1: "trout"
}
];
this.set("args", { key: "hbs_test1" });
},
test(assert) {
// comin up
2018-06-15 11:03:24 -04:00
assert.equal(this.$("span.string").text(), "evil");
assert.equal(this.$("span.var").text(), "trout");
assert.equal(this.$("a").prop("title"), "evil");
}
});
2018-06-15 11:03:24 -04:00
widgetTest("handlebars #each", {
template: `{{mount-widget widget="hbs-each-test" args=args}}`,
beforeEach() {
2018-06-15 11:03:24 -04:00
createWidget("hbs-each-test", {
tagName: "ul",
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"]
});
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$("ul li").length, 3);
assert.equal(this.$("ul li:eq(0)").text(), "one");
}
});
2018-06-15 11:03:24 -04:00
widgetTest("widget decorating", {
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",
template: hbs`main content`
});
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
});
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
});
});
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.ok(this.$(".decorate").length);
assert.equal(this.$(".decorate b").text(), "before");
assert.equal(this.$(".decorate i").text(), "after");
}
});
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",
template: hbs`age is {{settings.age}}`,
settings: { age: 36 }
2016-02-26 14:26:29 -05:00
});
},
test(assert) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$(".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",
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) {
2018-06-15 11:03:24 -04:00
assert.equal(this.$(".settings").text(), "age is 37");
2016-02-26 14:26:29 -05:00
}
});