2021-12-28 15:42:35 +00:00
|
|
|
import { find, render, settled } from '@ember/test-helpers';
|
2019-12-03 03:22:41 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2019-12-03 03:22:41 +00:00
|
|
|
import sinon from 'sinon';
|
|
|
|
import { create } from 'ember-cli-page-object';
|
|
|
|
import togglePageObject from 'nomad-ui/tests/pages/components/toggle';
|
|
|
|
|
|
|
|
const Toggle = create(togglePageObject());
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Integration | Component | toggle', function (hooks) {
|
2019-12-03 03:22:41 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
|
|
|
const commonProperties = () => ({
|
|
|
|
isActive: false,
|
|
|
|
isDisabled: false,
|
|
|
|
label: 'Label',
|
|
|
|
onToggle: sinon.spy(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const commonTemplate = hbs`
|
2020-07-09 17:30:11 +00:00
|
|
|
<Toggle
|
|
|
|
@isActive={{isActive}}
|
|
|
|
@isDisabled={{isDisabled}}
|
|
|
|
@onToggle={{onToggle}}>
|
2019-12-03 03:22:41 +00:00
|
|
|
{{label}}
|
2020-07-09 17:30:11 +00:00
|
|
|
</Toggle>
|
2019-12-03 03:22:41 +00:00
|
|
|
`;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('presents as a label with an inner checkbox and display span, and text', async function (assert) {
|
2019-12-03 03:22:41 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-12-03 03:22:41 +00:00
|
|
|
|
|
|
|
assert.equal(Toggle.label, props.label, `Label should be ${props.label}`);
|
|
|
|
assert.ok(Toggle.isPresent);
|
|
|
|
assert.notOk(Toggle.isActive);
|
|
|
|
assert.ok(find('[data-test-toggler]'));
|
|
|
|
assert.equal(
|
|
|
|
find('[data-test-input]').tagName.toLowerCase(),
|
|
|
|
'input',
|
|
|
|
'The input is a real HTML input'
|
|
|
|
);
|
|
|
|
assert.equal(
|
|
|
|
find('[data-test-input]').getAttribute('type'),
|
|
|
|
'checkbox',
|
|
|
|
'The input type is checkbox'
|
|
|
|
);
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-12-03 03:22:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the isActive property dictates the active state and class', async function (assert) {
|
2019-12-03 03:22:41 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-12-03 03:22:41 +00:00
|
|
|
|
|
|
|
assert.notOk(Toggle.isActive);
|
|
|
|
assert.notOk(Toggle.hasActiveClass);
|
|
|
|
|
|
|
|
this.set('isActive', true);
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(Toggle.isActive);
|
|
|
|
assert.ok(Toggle.hasActiveClass);
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-12-03 03:22:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the isDisabled property dictates the disabled state and class', async function (assert) {
|
2019-12-03 03:22:41 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-12-03 03:22:41 +00:00
|
|
|
|
|
|
|
assert.notOk(Toggle.isDisabled);
|
|
|
|
assert.notOk(Toggle.hasDisabledClass);
|
|
|
|
|
|
|
|
this.set('isDisabled', true);
|
|
|
|
await settled();
|
|
|
|
|
|
|
|
assert.ok(Toggle.isDisabled);
|
|
|
|
assert.ok(Toggle.hasDisabledClass);
|
2020-08-25 15:56:02 +00:00
|
|
|
|
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-12-03 03:22:41 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('toggling the input calls the onToggle action', async function (assert) {
|
2019-12-03 03:22:41 +00:00
|
|
|
const props = commonProperties();
|
|
|
|
this.setProperties(props);
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-12-03 03:22:41 +00:00
|
|
|
|
|
|
|
await Toggle.toggle();
|
|
|
|
assert.equal(props.onToggle.callCount, 1);
|
|
|
|
});
|
|
|
|
});
|