|
| 1 | +import AuRadioGroup from '@appuniversum/ember-appuniversum/components/au-radio-group'; |
| 2 | +import { settled } from '@ember/test-helpers'; |
| 3 | +import { click, render } from '@ember/test-helpers'; |
| 4 | +import { tracked } from '@glimmer/tracking'; |
| 5 | +import { setupRenderingTest } from 'dummy/tests/helpers'; |
| 6 | +import { module, test } from 'qunit'; |
| 7 | + |
| 8 | +module('Integration | Component | au-radio-group', function (hooks) { |
| 9 | + setupRenderingTest(hooks); |
| 10 | + |
| 11 | + test('it can be given a name', async function (assert) { |
| 12 | + await render( |
| 13 | + <template> |
| 14 | + <AuRadioGroup @name="foo" as |Group|> |
| 15 | + <Group.Radio data-test-foo>Foo</Group.Radio> |
| 16 | + </AuRadioGroup> |
| 17 | + </template>, |
| 18 | + ); |
| 19 | + assert.dom('[data-test-foo]').hasAttribute('name', 'foo'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('if it is given no name, it will automatically generate a unique name', async function (assert) { |
| 23 | + await render( |
| 24 | + <template> |
| 25 | + <AuRadioGroup as |Group|> |
| 26 | + <Group.Radio data-test-foo>Foo</Group.Radio> |
| 27 | + </AuRadioGroup> |
| 28 | + </template>, |
| 29 | + ); |
| 30 | + assert.dom('[data-test-foo]').hasAttribute('name'); |
| 31 | + }); |
| 32 | + |
| 33 | + test('it can be given a value', async function (assert) { |
| 34 | + const state = new TestState(); |
| 35 | + state.value = 'bar'; |
| 36 | + |
| 37 | + await render( |
| 38 | + <template> |
| 39 | + <AuRadioGroup @selected={{state.value}} as |Group|> |
| 40 | + <Group.Radio @value="foo" data-test-foo>Foo</Group.Radio> |
| 41 | + <Group.Radio @value="bar" data-test-bar>Bar</Group.Radio> |
| 42 | + </AuRadioGroup> |
| 43 | + </template>, |
| 44 | + ); |
| 45 | + |
| 46 | + assert.dom('[data-test-bar]').isChecked(); |
| 47 | + assert.dom('[data-test-foo]').isNotChecked(); |
| 48 | + |
| 49 | + state.value = 'foo'; |
| 50 | + await settled(); |
| 51 | + assert.dom('[data-test-bar]').isNotChecked(); |
| 52 | + assert.dom('[data-test-foo]').isChecked(); |
| 53 | + }); |
| 54 | + |
| 55 | + test('it can be disabled', async function (assert) { |
| 56 | + const state = new TestState(); |
| 57 | + state.disabled = true; |
| 58 | + await render( |
| 59 | + <template> |
| 60 | + <AuRadioGroup @disabled={{state.disabled}} as |Group|> |
| 61 | + <Group.Radio data-test-foo>Foo</Group.Radio> |
| 62 | + </AuRadioGroup> |
| 63 | + </template>, |
| 64 | + ); |
| 65 | + |
| 66 | + assert.dom('[data-test-foo]').isDisabled(); |
| 67 | + |
| 68 | + state.disabled = false; |
| 69 | + await settled(); |
| 70 | + assert.dom('[data-test-foo]').isNotDisabled(); |
| 71 | + }); |
| 72 | + |
| 73 | + test('it calls the provided @onChange action when its state changes by user input', async function (assert) { |
| 74 | + let currentValue: string | undefined; |
| 75 | + |
| 76 | + const handleChange = (value: string | undefined, event: Event) => { |
| 77 | + currentValue = value; |
| 78 | + assert.true(event instanceof Event); |
| 79 | + }; |
| 80 | + |
| 81 | + await render( |
| 82 | + <template> |
| 83 | + <AuRadioGroup @onChange={{handleChange}} as |Group|> |
| 84 | + <Group.Radio @value="foo" data-test-foo>Foo</Group.Radio> |
| 85 | + <Group.Radio @value="bar" data-test-bar>Bar</Group.Radio> |
| 86 | + </AuRadioGroup> |
| 87 | + </template>, |
| 88 | + ); |
| 89 | + |
| 90 | + await click('[data-test-bar]'); |
| 91 | + assert.strictEqual(currentValue, 'bar'); |
| 92 | + |
| 93 | + await click('[data-test-foo]'); |
| 94 | + assert.strictEqual(currentValue, 'foo'); |
| 95 | + }); |
| 96 | + |
| 97 | + test('it adds any extra attributes to the group element', async function (assert) { |
| 98 | + await render( |
| 99 | + <template> |
| 100 | + <AuRadioGroup foo="bar" data-test-radio-group as |Group|> |
| 101 | + <Group.Radio>Foo</Group.Radio> |
| 102 | + </AuRadioGroup> |
| 103 | + </template>, |
| 104 | + ); |
| 105 | + assert.dom('[data-test-radio-group]').hasAttribute('foo', 'bar'); |
| 106 | + }); |
| 107 | +}); |
| 108 | + |
| 109 | +class TestState { |
| 110 | + @tracked disabled?: boolean; |
| 111 | + @tracked value?: string; |
| 112 | +} |
0 commit comments