Skip to content

Commit

Permalink
test: fix tests where confirm-task decorator in passive use
Browse files Browse the repository at this point in the history
  • Loading branch information
derrabauke committed Feb 1, 2022
1 parent 8b385ae commit 4647f4d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
27 changes: 19 additions & 8 deletions addon/decorators/confirm-task.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { assert } from "@ember/debug";
import UIkit from "uikit";

import ENV from "ember-emeis/config/environment";

/**
* This decorator makes heavy use of the UIKit modal. If anything breaks in future releases, it could be
* caused by the use of non-public API in this decorator.
Expand All @@ -19,15 +21,15 @@ async function confirm(text, options) {
}
}

function validate(args) {
function initOptions(args) {
if (args.length === 1) {
assert(
"You should pass the confirm-task options as an object looking like this: { message: 'emeis.form.deleteMessage', cancel: 'emeis.form.back' } ",
typeof args[0] === "object"
);
return true;
return args[0];
}
return false;
return {};
}

function validateIntl(context) {
Expand All @@ -40,7 +42,7 @@ function validateIntl(context) {
function translateOptions(context, options) {
const translated = {};
for (const key in options) {
if (LABELS.includes(key)) {
if (LABELS.includes(key) && this.intl.exists(options[key])) {
translated[key] = context.intl.t(options[key]);
}
}
Expand All @@ -56,6 +58,17 @@ function translateOptions(context, options) {
};
}

function filterOptions(options) {
const filteredOptions = Object.fromEntries(
Object.entries(options).filter(([key]) => !LABELS.includes(key))
);

if (ENV.environment === "test") {
filteredOptions.container = "#ember-testing";
}
return filteredOptions;
}

// make sure that decorator can be called with or without arguments
const makeFlexibleDecorator = (decorateFn, args) => {
if (args.length === 3 && !args[0].message) {
Expand All @@ -67,7 +80,7 @@ const makeFlexibleDecorator = (decorateFn, args) => {
};

export function confirmTask(...decoratorArgs) {
const options = validate(decoratorArgs) ? decoratorArgs[0] : {};
const options = initOptions(decoratorArgs);

function decorate(target, property, desc) {
const gen = desc.value;
Expand All @@ -77,9 +90,7 @@ export function confirmTask(...decoratorArgs) {
const translatedOptions = translateOptions(this, options);
// append other options which are not confirm-labels to the modal object
// that way you can modify the modal with further options like "container"
const filteredOptions = Object.fromEntries(
Object.entries(options).filter(([key]) => !LABELS.includes(key))
);
const filteredOptions = filterOptions(options);
if (
!(yield confirm(translatedOptions.labels.message, {
...translatedOptions,
Expand Down
4 changes: 4 additions & 0 deletions tests/acceptance/permissions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
currentURL,
fillIn,
click,
waitFor,
waitUntil,
settled,
} from "@ember/test-helpers";
Expand Down Expand Up @@ -151,6 +152,9 @@ module("Acceptance | permissions", function (hooks) {
});
await click("[data-test-delete]");

await waitFor(".uk-modal.uk-open");
await click(".uk-modal .uk-button-primary");

// For some reason the await click is not actually waiting for the delete task to finish.
// Probably some runloop issue.
await waitUntil(() => currentURL() !== `/permissions/${permission.id}`);
Expand Down
3 changes: 3 additions & 0 deletions tests/acceptance/roles-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
click,
fillIn,
findAll,
waitFor,
waitUntil,
settled,
} from "@ember/test-helpers";
Expand Down Expand Up @@ -131,6 +132,8 @@ module("Acceptance | roles", function (hooks) {
assert.strictEqual(role.id, request.params.id);
});
await click("[data-test-delete]");
await waitFor(".uk-modal.uk-open");
await click(".uk-modal .uk-button-primary");

// For some reason the await click is not actually waiting for the delete task to finish.
// Probably some runloop issue.
Expand Down
3 changes: 3 additions & 0 deletions tests/acceptance/scopes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
currentURL,
fillIn,
click,
waitFor,
waitUntil,
settled,
} from "@ember/test-helpers";
Expand Down Expand Up @@ -140,6 +141,8 @@ module("Acceptance | scopes", function (hooks) {
assert.strictEqual(scope.id, request.params.id);
});
await click("[data-test-delete]");
await waitFor(".uk-modal.uk-open");
await click(".uk-modal .uk-button-primary");

// For some reason the await click is not actually waiting for the delete task to finish.
// Probably some runloop issue.
Expand Down
5 changes: 4 additions & 1 deletion tests/integration/components/edit-form-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Service from "@ember/service";
import { render, click } from "@ember/test-helpers";
import { render, click, waitFor } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { setupIntl } from "ember-intl/test-support";
import { setupRenderingTest } from "ember-qunit";
Expand Down Expand Up @@ -89,6 +89,9 @@ module("Integration | Component | edit-form", function (hooks) {
await render(hbs`<EditForm @model={{this.model}}/>`);

await click("[data-test-delete]");
await waitFor(".uk-modal.uk-open");
await click(".uk-modal .uk-button-primary");

assert.verifySteps(["destroyRecord"]);
});

Expand Down

0 comments on commit 4647f4d

Please sign in to comment.