-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(confirm): add modal service with configuration options
Include a modal service and enable configuration options for modal dialog functions. The modal container can be configured through the environment.js file.
- Loading branch information
Showing
5 changed files
with
143 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { getOwner } from "@ember/application"; | ||
import Service from "@ember/service"; | ||
import UIkit from "uikit"; | ||
|
||
function modal(type) { | ||
return function () { | ||
return { | ||
async value(message, options = {}) { | ||
try { | ||
const config = | ||
getOwner(this).resolveRegistration("config:environment")[ | ||
"ember-uikit" | ||
]?.modal ?? {}; | ||
|
||
await UIkit.modal[type](message, { | ||
...config, | ||
...options, | ||
}); | ||
|
||
return true; | ||
} catch (e) { | ||
return false; | ||
} | ||
}, | ||
}; | ||
}; | ||
} | ||
|
||
export default class ModalService extends Service { | ||
@modal("alert") alert; | ||
@modal("confirm") confirm; | ||
@modal("prompt") prompt; | ||
@modal("dialog") dialog; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from "ember-uikit/services/modal"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { click, waitUntil, find } from "@ember/test-helpers"; | ||
import { setupTest } from "ember-qunit"; | ||
import { module, test } from "qunit"; | ||
import UIkit from "uikit"; | ||
|
||
module("Unit | Service | modal", function (hooks) { | ||
setupTest(hooks); | ||
|
||
hooks.beforeEach(function () {}); | ||
|
||
test("can display modals", async function (assert) { | ||
assert.expect(9); | ||
|
||
const _original = UIkit.modal; | ||
const service = this.owner.lookup("service:modal"); | ||
|
||
UIkit.modal = { | ||
alert: (msg) => { | ||
assert.strictEqual(msg, "Test"); | ||
assert.step("alert"); | ||
}, | ||
confirm: (msg) => { | ||
assert.strictEqual(msg, "Test"); | ||
assert.step("confirm"); | ||
}, | ||
prompt: (msg) => { | ||
assert.strictEqual(msg, "Test"); | ||
assert.step("prompt"); | ||
}, | ||
dialog: (msg) => { | ||
assert.strictEqual(msg, "Test"); | ||
assert.step("dialog"); | ||
}, | ||
}; | ||
|
||
await service.alert("Test"); | ||
await service.confirm("Test"); | ||
await service.prompt("Test"); | ||
await service.dialog("Test"); | ||
|
||
assert.verifySteps(["alert", "confirm", "prompt", "dialog"]); | ||
UIkit.modal = _original; | ||
}); | ||
|
||
test("can pass options", async function (assert) { | ||
assert.expect(2); | ||
|
||
const _original = UIkit.modal; | ||
const service = this.owner.lookup("service:modal"); | ||
|
||
UIkit.modal = { | ||
confirm: (_, options) => { | ||
assert.strictEqual(options.timeout, 100); | ||
assert.strictEqual(options.container, "#modal-container"); | ||
}, | ||
}; | ||
await service.confirm("Test", { | ||
container: "#modal-container", | ||
timeout: 100, | ||
}); | ||
|
||
UIkit.modal = _original; | ||
}); | ||
|
||
test("returns the selected response of the modal", async function (assert) { | ||
assert.expect(6); | ||
|
||
const service = this.owner.lookup("service:modal"); | ||
let response = service.confirm("confirm"); | ||
|
||
await waitUntil(() => find(".uk-modal.uk-open")); | ||
assert.dom(".uk-modal-body").hasText("confirm"); | ||
await click(".uk-modal-footer .uk-button-primary"); | ||
|
||
response = await response; | ||
assert.strictEqual(typeof response, "boolean"); | ||
assert.true(response); | ||
|
||
response = service.confirm("confirm"); | ||
|
||
await waitUntil(() => find(".uk-modal.uk-open")); | ||
assert.dom(".uk-modal-body").hasText("confirm"); | ||
await click(".uk-modal-footer .uk-button-default"); | ||
|
||
response = await response; | ||
assert.strictEqual(typeof response, "boolean"); | ||
assert.false(response); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters