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 bc6c8d1 commit cc51c0a
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 11 deletions.
33 changes: 25 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) && context.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,19 +80,23 @@ 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;

desc.value = function* (...args) {
const event = args.find((arg) => arg instanceof Event);

if (event) {
event.preventDefault();
}

validateIntl(this);
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
3 changes: 1 addition & 2 deletions addon/templates/users/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
{{t "emeis.scopes.title"}}
</Column>
<Column>
{{t "emeis.actions"}}
</Column>
</table.head>
<table.body as |body|>
Expand Down Expand Up @@ -92,7 +91,7 @@
{{/each}}
</ul>
</td>
<td>
<td class="uk-text-right small-padding-right">
<LinkTo @route="users.edit" @model={{user}} class="uk-link-reset uk-margin-small-right">
<UkIcon @icon="pencil"/>
</LinkTo>
Expand Down
4 changes: 4 additions & 0 deletions app/styles/ember-emeis.scss
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ div.uk-grid-divider-fix.uk-grid-stack > .uk-grid-margin::before {
}
}

.uk-table td.small-padding-right {
padding-right: 1.75em;
}

.sortable-th {
cursor: pointer;

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 cc51c0a

Please sign in to comment.