2018-04-18 20:02:58 +00:00
|
|
|
import { find, click } from 'ember-native-dom-helpers';
|
2019-03-13 00:04:16 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import { render, settled } from '@ember/test-helpers';
|
2018-04-18 20:02:58 +00:00
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
|
|
|
import sinon from 'sinon';
|
2019-05-21 16:23:47 +00:00
|
|
|
import { create } from 'ember-cli-page-object';
|
|
|
|
import twoStepButton from 'nomad-ui/tests/pages/components/two-step-button';
|
|
|
|
|
|
|
|
const TwoStepButton = create(twoStepButton());
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
module('Integration | Component | two step button', function(hooks) {
|
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const commonProperties = () => ({
|
|
|
|
idleText: 'Idle State Button',
|
|
|
|
cancelText: 'Cancel Action',
|
|
|
|
confirmText: 'Confirm Action',
|
|
|
|
confirmationMessage: 'Are you certain',
|
|
|
|
awaitingConfirmation: false,
|
2019-05-17 00:41:40 +00:00
|
|
|
disabled: false,
|
2019-03-13 00:04:16 +00:00
|
|
|
onConfirm: sinon.spy(),
|
|
|
|
onCancel: sinon.spy(),
|
|
|
|
});
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
const commonTemplate = hbs`
|
|
|
|
{{two-step-button
|
|
|
|
idleText=idleText
|
|
|
|
cancelText=cancelText
|
|
|
|
confirmText=confirmText
|
|
|
|
confirmationMessage=confirmationMessage
|
|
|
|
awaitingConfirmation=awaitingConfirmation
|
2019-05-17 00:41:40 +00:00
|
|
|
disabled=disabled
|
2019-03-13 00:04:16 +00:00
|
|
|
onConfirm=onConfirm
|
|
|
|
onCancel=onCancel}}
|
|
|
|
`;
|
|
|
|
|
|
|
|
test('presents as a button in the idle state', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-idle-button]'), 'Idle button is rendered');
|
2019-05-21 16:23:47 +00:00
|
|
|
assert.equal(TwoStepButton.idleText, props.idleText, 'Button is labeled correctly');
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
assert.notOk(find('[data-test-cancel-button]'), 'No cancel button yet');
|
|
|
|
assert.notOk(find('[data-test-confirm-button]'), 'No confirm button yet');
|
|
|
|
assert.notOk(find('[data-test-confirmation-message]'), 'No confirmation message yet');
|
|
|
|
});
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
test('clicking the idle state button transitions into the promptForConfirmation state', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
return settled().then(() => {
|
|
|
|
assert.ok(find('[data-test-cancel-button]'), 'Cancel button is rendered');
|
2019-05-21 16:23:47 +00:00
|
|
|
assert.equal(TwoStepButton.cancelText, props.cancelText, 'Button is labeled correctly');
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
assert.ok(find('[data-test-confirm-button]'), 'Confirm button is rendered');
|
2019-05-21 16:23:47 +00:00
|
|
|
assert.equal(TwoStepButton.confirmText, props.confirmText, 'Button is labeled correctly');
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
assert.equal(
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.confirmationMessage,
|
2019-03-13 00:04:16 +00:00
|
|
|
props.confirmationMessage,
|
|
|
|
'Confirmation message is shown'
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.notOk(find('[data-test-idle-button]'), 'No more idle button');
|
|
|
|
});
|
2018-04-18 20:02:58 +00:00
|
|
|
});
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
test('canceling in the promptForConfirmation state calls the onCancel hook and resets to the idle state', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
return settled().then(() => {
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.cancel();
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
return settled().then(() => {
|
|
|
|
assert.ok(props.onCancel.calledOnce, 'The onCancel hook fired');
|
|
|
|
assert.ok(find('[data-test-idle-button]'), 'Idle button is back');
|
|
|
|
});
|
2018-04-18 20:02:58 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
test('confirming the promptForConfirmation state calls the onConfirm hook and resets to the idle state', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
return settled().then(() => {
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.confirm();
|
2018-04-18 20:02:58 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
return settled().then(() => {
|
|
|
|
assert.ok(props.onConfirm.calledOnce, 'The onConfirm hook fired');
|
|
|
|
assert.ok(find('[data-test-idle-button]'), 'Idle button is back');
|
|
|
|
});
|
2018-04-18 20:02:58 +00:00
|
|
|
});
|
|
|
|
});
|
2018-08-24 20:41:14 +00:00
|
|
|
|
2019-03-13 00:04:16 +00:00
|
|
|
test('when awaitingConfirmation is true, the cancel and submit buttons are disabled and the submit button is loading', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
props.awaitingConfirmation = true;
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-03-13 00:04:16 +00:00
|
|
|
|
|
|
|
return settled().then(() => {
|
2019-05-21 16:23:47 +00:00
|
|
|
assert.ok(TwoStepButton.cancelIsDisabled, 'The cancel button is disabled');
|
|
|
|
assert.ok(TwoStepButton.confirmIsDisabled, 'The confirm button is disabled');
|
|
|
|
assert.ok(TwoStepButton.isRunning, 'The confirm button is in a loading state');
|
2019-03-13 00:04:16 +00:00
|
|
|
});
|
2018-08-24 20:41:14 +00:00
|
|
|
});
|
2019-05-17 00:41:40 +00:00
|
|
|
|
|
|
|
test('when in the prompt state, clicking outside will reset state back to idle', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-05-17 00:41:40 +00:00
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-cancel-button]'), 'In the prompt state');
|
|
|
|
|
|
|
|
click(document.body);
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-idle-button]'), 'Back in the idle state');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when in the prompt state, clicking inside will not reset state back to idle', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-05-17 00:41:40 +00:00
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-cancel-button]'), 'In the prompt state');
|
|
|
|
|
|
|
|
click('[data-test-confirmation-message]');
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.notOk(find('[data-test-idle-button]'), 'Still in the prompt state');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when awaitingConfirmation is true, clicking outside does nothing', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
props.awaitingConfirmation = true;
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-05-17 00:41:40 +00:00
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(find('[data-test-cancel-button]'), 'In the prompt state');
|
|
|
|
|
|
|
|
click(document.body);
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.notOk(find('[data-test-idle-button]'), 'Still in the prompt state');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('when disabled is true, the idle button is disabled', async function(assert) {
|
|
|
|
const props = commonProperties();
|
|
|
|
props.disabled = true;
|
|
|
|
this.setProperties(props);
|
|
|
|
await render(commonTemplate);
|
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
assert.ok(TwoStepButton.isDisabled, 'The idle button is disabled');
|
2019-05-17 00:41:40 +00:00
|
|
|
|
2019-05-21 16:23:47 +00:00
|
|
|
TwoStepButton.idle();
|
2019-05-17 00:41:40 +00:00
|
|
|
assert.ok(find('[data-test-idle-button]'), 'Still in the idle state after clicking');
|
|
|
|
});
|
2018-08-24 20:41:14 +00:00
|
|
|
});
|