DEV: migrate components tests to async/await

This commit is contained in:
Maja Komel 2018-07-24 20:12:09 +02:00
parent c0992a4d31
commit d16a2c776a
10 changed files with 619 additions and 615 deletions

View File

@ -7,8 +7,7 @@ moduleForComponent("admin-report", {
componentTest("default", {
template: "{{admin-report dataSourceName='signups'}}",
test(assert) {
andThen(() => {
async test(assert) {
assert.ok(exists(".admin-report.signups"));
assert.ok(
@ -62,10 +61,8 @@ componentTest("default", {
);
assert.ok(exists(".totals-sample-table"), "it has totals");
});
click(".admin-report-table-header.y .sort-button");
andThen(() => {
await click(".admin-report-table-header.y .sort-button");
assert.equal(
find(".report-body .report-table tbody tr:nth-child(1) td:nth-child(2)")
.text()
@ -73,7 +70,6 @@ componentTest("default", {
"7",
"it can sort rows"
);
});
}
});
@ -90,7 +86,6 @@ componentTest("options", {
},
test(assert) {
andThen(() => {
assert.ok(exists(".pagination"), "it paginates the results");
assert.equal(
find(".pagination button").length,
@ -99,23 +94,17 @@ componentTest("options", {
);
assert.notOk(exists(".totals-sample-table"), "it hides totals");
});
}
});
componentTest("switch modes", {
template: "{{admin-report dataSourceName='signups'}}",
test(assert) {
click(".mode-button.chart");
async test(assert) {
await click(".mode-button.chart");
andThen(() => {
assert.notOk(
exists(".admin-report.table.signups"),
"it removes the table"
);
assert.notOk(exists(".admin-report.table.signups"), "it removes the table");
assert.ok(exists(".admin-report.chart.signups"), "it shows the chart");
});
}
});
@ -123,8 +112,6 @@ componentTest("timeout", {
template: "{{admin-report dataSourceName='signups_timeout'}}",
test(assert) {
andThen(() => {
assert.ok(exists(".alert-error"), "it displays a timeout error");
});
}
});

View File

@ -4,16 +4,14 @@ moduleForComponent("categories-admin-dropdown", { integration: true });
componentTest("default", {
template: "{{categories-admin-dropdown}}",
test(assert) {
async test(assert) {
const subject = selectKit();
assert.equal(subject.el().find(".d-icon-bars").length, 1);
assert.equal(subject.el().find(".d-icon-caret-down").length, 1);
subject.expand();
await subject.expandAwait();
andThen(() => {
assert.equal(subject.rowByValue("create").name(), "New Category");
});
}
});

View File

@ -11,7 +11,6 @@ componentTest("with value", {
template: "{{category-chooser value=2}}",
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -24,22 +23,19 @@ componentTest("with value", {
.title(),
"feature"
);
});
}
});
componentTest("with excludeCategoryId", {
template: "{{category-chooser excludeCategoryId=2}}",
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() =>
assert.notOk(
this.get("subject")
.rowByValue(2)
.exists()
)
);
}
});
@ -47,10 +43,9 @@ componentTest("with excludeCategoryId", {
componentTest("with scopedCategoryId", {
template: "{{category-chooser scopedCategoryId=2}}",
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.rowByIndex(0)
@ -76,7 +71,6 @@ componentTest("with scopedCategoryId", {
26
);
assert.equal(this.get("subject").rows().length, 2);
});
}
});
@ -88,7 +82,6 @@ componentTest("with allowUncategorized=null", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -101,7 +94,6 @@ componentTest("with allowUncategorized=null", {
.title(),
"category"
);
});
}
});
@ -113,7 +105,6 @@ componentTest("with allowUncategorized=null rootNone=true", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -126,7 +117,6 @@ componentTest("with allowUncategorized=null rootNone=true", {
.title(),
"category"
);
});
}
});
@ -140,7 +130,6 @@ componentTest("with disallowed uncategorized, rootNone and rootNoneLabel", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -153,7 +142,6 @@ componentTest("with disallowed uncategorized, rootNone and rootNoneLabel", {
.title(),
"category"
);
});
}
});
@ -165,7 +153,6 @@ componentTest("with allowed uncategorized", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -178,7 +165,6 @@ componentTest("with allowed uncategorized", {
.title(),
"uncategorized"
);
});
}
});
@ -190,7 +176,6 @@ componentTest("with allowed uncategorized and rootNone", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -203,7 +188,6 @@ componentTest("with allowed uncategorized and rootNone", {
.title(),
"(no category)"
);
});
}
});
@ -217,7 +201,6 @@ componentTest("with allowed uncategorized rootNone and rootNoneLabel", {
},
test(assert) {
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -230,6 +213,5 @@ componentTest("with allowed uncategorized rootNone and rootNoneLabel", {
.title(),
"root none label"
);
});
}
});

View File

@ -12,10 +12,9 @@ componentTest("default", {
this.set("items", [{ id: 1, name: "hello" }, { id: 2, name: "world" }]);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -34,7 +33,6 @@ componentTest("default", {
.name(),
"world"
);
});
}
});
@ -47,10 +45,9 @@ componentTest("with valueAttribute", {
]);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.rowByValue(0)
@ -63,7 +60,6 @@ componentTest("with valueAttribute", {
.name(),
"world"
);
});
}
});
@ -73,10 +69,9 @@ componentTest("with nameProperty", {
this.set("items", [{ id: 0, text: "hello" }, { id: 1, text: "world" }]);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.rowByValue(0)
@ -89,7 +84,6 @@ componentTest("with nameProperty", {
.name(),
"world"
);
});
}
});
@ -99,10 +93,9 @@ componentTest("with an array as content", {
this.set("items", ["evil", "trout", "hat"]);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.rowByValue("evil")
@ -115,7 +108,6 @@ componentTest("with an array as content", {
.name(),
"trout"
);
});
}
});
@ -127,10 +119,9 @@ componentTest("with value and none as a string", {
this.set("value", "trout");
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.noneRow()
@ -156,13 +147,10 @@ componentTest("with value and none as a string", {
"trout"
);
assert.equal(this.get("value"), "trout");
});
this.get("subject").selectNoneRow();
await this.get("subject").selectNoneRowAwait();
andThen(() => {
assert.equal(this.get("value"), null);
});
}
});
@ -174,10 +162,9 @@ componentTest("with value and none as an object", {
this.set("value", "evil");
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expand();
andThen(() => {
assert.equal(
this.get("subject")
.noneRow()
@ -203,13 +190,10 @@ componentTest("with value and none as an object", {
"evil"
);
assert.equal(this.get("value"), "evil");
});
this.get("subject").selectNoneRow();
await this.get("subject").selectNoneRowAwait();
andThen(() => {
assert.equal(this.get("value"), null);
});
}
});
@ -222,17 +206,15 @@ componentTest("with no value and none as an object", {
this.set("value", null);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
.name(),
"none"
);
});
}
});
@ -245,17 +227,15 @@ componentTest("with no value and none string", {
this.set("value", null);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
.name(),
"none"
);
});
}
});
@ -266,10 +246,9 @@ componentTest("with no value and no none", {
this.set("value", null);
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -277,7 +256,6 @@ componentTest("with no value and no none", {
"evil",
"it sets the first row as value"
);
});
}
});
@ -288,10 +266,9 @@ componentTest("with empty string as value", {
this.set("value", "");
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expand();
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -299,7 +276,6 @@ componentTest("with empty string as value", {
"evil",
"it sets the first row as value"
);
});
}
});
@ -312,10 +288,9 @@ componentTest("with noneLabel", {
this.set("noneLabel", "test.none");
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -323,6 +298,5 @@ componentTest("with noneLabel", {
"none",
"it displays noneLabel as the header name"
);
});
}
});

View File

@ -26,25 +26,21 @@ componentTest("updating the content refreshes the list", {
this.set("pinned", true);
},
test(assert) {
andThen(() => {
async test(assert) {
assert.equal(
this.get("subject")
.header()
.name(),
"pinned"
);
});
andThen(() => this.set("pinned", false));
await this.set("pinned", false);
andThen(() => {
assert.equal(
this.get("subject")
.header()
.name(),
"unpinned"
);
});
}
});

View File

@ -21,10 +21,9 @@ componentTest("default", {
this.set("topic", buildTopic());
},
test(assert) {
this.get("subject").expand();
async test(assert) {
await this.get("subject").expandAwait();
andThen(() => {
assert.equal(
this.get("subject")
.header()
@ -55,12 +54,9 @@ componentTest("default", {
.exists(),
"it doesn’t preselect first row"
);
});
this.get("subject").selectRowByValue("share");
await this.get("subject").selectRowByValueAwait("share");
andThen(() => {
assert.equal(this.get("value"), null, "it resets the value");
});
}
});

View File

@ -31,8 +31,7 @@ componentTest("the header has a localized title", {
this.set("topic", buildTopic(1));
},
test(assert) {
andThen(() => {
async test(assert) {
assert.equal(
selectKit()
.header()
@ -40,11 +39,9 @@ componentTest("the header has a localized title", {
"Normal",
"it has the correct title"
);
});
this.set("topic", buildTopic(2));
await this.set("topic", buildTopic(2));
andThen(() => {
assert.equal(
selectKit()
.header()
@ -52,7 +49,6 @@ componentTest("the header has a localized title", {
"Tracking",
"it correctly changes the title"
);
});
}
});
@ -66,7 +62,6 @@ componentTest("the header has a localized title", {
},
test(assert) {
andThen(() => {
assert.equal(
selectKit()
.header()
@ -74,6 +69,5 @@ componentTest("the header has a localized title", {
`${originalTranslation} PM`,
"it has the correct title for PMs"
);
});
}
});

View File

@ -32,11 +32,10 @@ componentTest("regular topic notification level descriptions", {
template:
"{{topic-notifications-options value=topic.details.notification_level topic=topic}}",
test(assert) {
selectKit().expand();
this.set("topic", buildTopic("regular"));
async test(assert) {
await selectKit().expandAwait();
await this.set("topic", buildTopic("regular"));
andThen(() => {
const uiTexts = extractDescs(selectKit().rows());
const descriptions = getTranslations();
@ -52,7 +51,6 @@ componentTest("regular topic notification level descriptions", {
"it has the correct copy"
);
});
});
}
});
@ -60,11 +58,10 @@ componentTest("PM topic notification level descriptions", {
template:
"{{topic-notifications-options value=topic.details.notification_level topic=topic}}",
test(assert) {
selectKit().expand();
this.set("topic", buildTopic("private_message"));
async test(assert) {
await selectKit().expandAwait();
await this.set("topic", buildTopic("private_message"));
andThen(() => {
const uiTexts = extractDescs(selectKit().rows());
const descriptions = getTranslations("_pm");
@ -80,6 +77,5 @@ componentTest("PM topic notification level descriptions", {
"it has the correct copy"
);
});
});
}
});

View File

@ -3,7 +3,7 @@ moduleForComponent("value-list", { integration: true });
componentTest("functionality", {
template: '{{value-list values=values inputType="array"}}',
test(assert) {
async test(assert) {
assert.ok(this.$(".values .value").length === 0, "it has no values");
assert.ok(this.$("input").length, "it renders the input");
assert.ok(
@ -11,29 +11,20 @@ componentTest("functionality", {
"it is disabled with no value"
);
fillIn("input", "eviltrout");
andThen(() => {
await fillIn("input", "eviltrout");
assert.ok(
!this.$(".btn-primary[disabled]").length,
"it isn't disabled anymore"
);
});
click(".btn-primary");
andThen(() => {
await click(".btn-primary");
assert.equal(this.$(".values .value").length, 1, "it adds the value");
assert.equal(this.$("input").val(), "", "it clears the input");
assert.ok(
this.$(".btn-primary[disabled]").length,
"it is disabled again"
);
assert.ok(this.$(".btn-primary[disabled]").length, "it is disabled again");
assert.equal(this.get("values"), "eviltrout", "it appends the value");
});
click(".value .btn-small");
andThen(() => {
await click(".value .btn-small");
assert.ok(this.$(".values .value").length === 0, "it removes the value");
});
}
});
@ -43,16 +34,14 @@ componentTest("with string delimited values", {
this.set("valueString", "hello\nworld");
},
test(assert) {
async test(assert) {
assert.equal(this.$(".values .value").length, 2);
fillIn("input", "eviltrout");
click(".btn-primary");
await fillIn("input", "eviltrout");
await click(".btn-primary");
andThen(() => {
assert.equal(this.$(".values .value").length, 3);
assert.equal(this.get("valueString"), "hello\nworld\neviltrout");
});
}
});
@ -62,15 +51,13 @@ componentTest("with array values", {
this.set("valueArray", ["abc", "def"]);
},
test(assert) {
async test(assert) {
assert.equal(this.$(".values .value").length, 2);
fillIn("input", "eviltrout");
click(".btn-primary");
await fillIn("input", "eviltrout");
await click(".btn-primary");
andThen(() => {
assert.equal(this.$(".values .value").length, 3);
assert.deepEqual(this.get("valueArray"), ["abc", "def", "eviltrout"]);
});
}
});

View File

@ -1,80 +1,134 @@
function checkSelectKitIsNotExpanded(selector) {
if (find(selector).hasClass('is-expanded')) {
throw new Error('You expected select-kit to be collapsed but it is expanded.');
if (find(selector).hasClass("is-expanded")) {
throw new Error(
"You expected select-kit to be collapsed but it is expanded."
);
}
}
function checkSelectKitIsNotCollapsed(selector) {
if (!find(selector).hasClass('is-expanded')) {
throw new Error('You expected select-kit to be expanded but it is collapsed.');
if (!find(selector).hasClass("is-expanded")) {
throw new Error(
"You expected select-kit to be expanded but it is collapsed."
);
}
}
Ember.Test.registerAsyncHelper('expandSelectKit', function(app, selector) {
Ember.Test.registerAsyncHelper("expandSelectKit", function(app, selector) {
checkSelectKitIsNotExpanded(selector);
click(selector + ' .select-kit-header');
click(selector + " .select-kit-header");
});
Ember.Test.registerAsyncHelper('collapseSelectKit', function(app, selector) {
Ember.Test.registerAsyncHelper("collapseSelectKit", function(app, selector) {
checkSelectKitIsNotCollapsed(selector);
click(selector + ' .select-kit-header');
click(selector + " .select-kit-header");
});
Ember.Test.registerAsyncHelper('selectKitFillInFilter', function(app, filter, selector) {
Ember.Test.registerAsyncHelper("selectKitFillInFilter", function(
app,
filter,
selector
) {
checkSelectKitIsNotCollapsed(selector);
fillIn(selector + ' .filter-input', filter);
fillIn(selector + " .filter-input", filter);
});
Ember.Test.registerAsyncHelper('selectKitSelectRowByValue', function(app, value, selector) {
Ember.Test.registerAsyncHelper("selectKitSelectRowByValue", function(
app,
value,
selector
) {
checkSelectKitIsNotCollapsed(selector);
click(selector + " .select-kit-row[data-value='" + value + "']");
});
Ember.Test.registerAsyncHelper('selectKitSelectRowByName', function(app, name, selector) {
Ember.Test.registerAsyncHelper("selectKitSelectRowByName", function(
app,
name,
selector
) {
checkSelectKitIsNotCollapsed(selector);
click(selector + " .select-kit-row[data-name='" + name + "']");
});
Ember.Test.registerAsyncHelper('selectKitSelectNoneRow', function(app, selector) {
Ember.Test.registerAsyncHelper("selectKitSelectNoneRow", function(
app,
selector
) {
checkSelectKitIsNotCollapsed(selector);
click(selector + " .select-kit-row.none");
});
Ember.Test.registerAsyncHelper('selectKitSelectRowByIndex', function(app, index, selector) {
Ember.Test.registerAsyncHelper("selectKitSelectRowByIndex", function(
app,
index,
selector
) {
checkSelectKitIsNotCollapsed(selector);
click(find(selector + " .select-kit-row").eq(index));
});
function selectKit(selector) { // eslint-disable-line no-unused-vars
// eslint-disable-next-line no-unused-vars
function selectKit(selector) {
selector = selector || ".select-kit";
function rowHelper(row) {
return {
name: function() { return row.attr('data-name'); },
icon: function() { return row.find('.d-icon'); },
title: function() { return row.attr('title'); },
value: function() { return row.attr('data-value'); },
exists: function() { return exists(row); },
el: function() { return row; }
name: function() {
return row.attr("data-name");
},
icon: function() {
return row.find(".d-icon");
},
title: function() {
return row.attr("title");
},
value: function() {
return row.attr("data-value");
},
exists: function() {
return exists(row);
},
el: function() {
return row;
}
};
}
function headerHelper(header) {
return {
value: function() { return header.attr('data-value'); },
name: function() { return header.attr('data-name'); },
label: function() { return header.text().trim(); },
icon: function() { return header.find('.icon'); },
title: function() { return header.attr('title'); },
el: function() { return header; }
value: function() {
return header.attr("data-value");
},
name: function() {
return header.attr("data-name");
},
label: function() {
return header.text().trim();
},
icon: function() {
return header.find(".icon");
},
title: function() {
return header.attr("title");
},
el: function() {
return header;
}
};
}
function filterHelper(filter) {
return {
icon: function() { return filter.find('.d-icon'); },
exists: function() { return exists(filter); },
el: function() { return filter; }
icon: function() {
return filter.find(".d-icon");
},
exists: function() {
return exists(filter);
},
el: function() {
return filter;
}
};
}
@ -85,24 +139,40 @@ function selectKit(selector) { // eslint-disable-line no-unused-vars
options = options || {};
andThen(function() {
var type = options.type || 'keydown';
var type = options.type || "keydown";
var event = jQuery.Event(type);
event.keyCode = keyCode;
if (options && options.metaKey) { event.metaKey = true; }
if (options && options.metaKey) {
event.metaKey = true;
}
find(eventSelector).trigger(event);
});
}
return {
down: function(target) { createEvent(target, 40); },
up: function(target) { createEvent(target, 38); },
escape: function(target) { createEvent(target, 27); },
enter: function(target) { createEvent(target, 13); },
tab: function(target) { createEvent(target, 9); },
backspace: function(target) { createEvent(target, 8); },
selectAll: function(target) { createEvent(target, 65, {metaKey: true}); },
};
down: function(target) {
createEvent(target, 40);
},
up: function(target) {
createEvent(target, 38);
},
escape: function(target) {
createEvent(target, 27);
},
enter: function(target) {
createEvent(target, 13);
},
tab: function(target) {
createEvent(target, 9);
},
backspace: function(target) {
createEvent(target, 8);
},
selectAll: function(target) {
createEvent(target, 65, { metaKey: true });
}
};
}
return {
expandAwait: function() {
@ -114,6 +184,10 @@ function selectKit(selector) { // eslint-disable-line no-unused-vars
return selectKit(selector);
},
collapseAwait: function() {
return collapseSelectKit(selector);
},
collapse: function() {
collapseSelectKit(selector);
return selectKit(selector);
@ -143,57 +217,75 @@ function selectKit(selector) { // eslint-disable-line no-unused-vars
return selectKit(selector);
},
selectNoneRowAwait: function() {
return selectKitSelectNoneRow(selector);
},
fillInFilter: function(filter) {
selectKitFillInFilter(filter, selector);
return selectKit(selector);
},
keyboard: function() { return keyboardHelper(selector); },
fillInFilterAwait: function(filter) {
return selectKitFillInFilter(filter, selector);
},
keyboard: function() {
return keyboardHelper(selector);
},
isExpanded: function() {
return find(selector).hasClass('is-expanded');
return find(selector).hasClass("is-expanded");
},
isFocused: function() {
return find(selector).hasClass('is-focused');
return find(selector).hasClass("is-focused");
},
isHidden: function() {
return find(selector).hasClass('is-hidden');
return find(selector).hasClass("is-hidden");
},
header: function() {
return headerHelper(find(selector).find('.select-kit-header'));
return headerHelper(find(selector).find(".select-kit-header"));
},
filter: function() {
return filterHelper(find(selector).find('.select-kit-filter'));
return filterHelper(find(selector).find(".select-kit-filter"));
},
rows: function() {
return find(selector).find('.select-kit-row');
return find(selector).find(".select-kit-row");
},
rowByValue: function(value) {
return rowHelper(find(selector).find('.select-kit-row[data-value="' + value + '"]'));
return rowHelper(
find(selector).find('.select-kit-row[data-value="' + value + '"]')
);
},
rowByName: function(name) {
return rowHelper(find(selector).find('.select-kit-row[data-name="' + name + '"]'));
return rowHelper(
find(selector).find('.select-kit-row[data-name="' + name + '"]')
);
},
rowByIndex: function(index) {
return rowHelper(find(selector).find('.select-kit-row:eq(' + index + ')'));
return rowHelper(
find(selector).find(".select-kit-row:eq(" + index + ")")
);
},
el: function() { return find(selector); },
el: function() {
return find(selector);
},
noneRow: function() {
return rowHelper(find(selector).find('.select-kit-row.none'));
return rowHelper(find(selector).find(".select-kit-row.none"));
},
validationMessage: function() {
var validationMessage = find(selector).find('.validation-message');
var validationMessage = find(selector).find(".validation-message");
if (validationMessage.length) {
return validationMessage.html().trim();
@ -203,13 +295,15 @@ function selectKit(selector) { // eslint-disable-line no-unused-vars
},
selectedRow: function() {
return rowHelper(find(selector).find('.select-kit-row.is-selected'));
return rowHelper(find(selector).find(".select-kit-row.is-selected"));
},
highlightedRow: function() {
return rowHelper(find(selector).find('.select-kit-row.is-highlighted'));
return rowHelper(find(selector).find(".select-kit-row.is-highlighted"));
},
exists: function() { return exists(selector); }
exists: function() {
return exists(selector);
}
};
}