Refactor key value comp (#2180)

* refactor: 💡 keyvalue comp
This commit is contained in:
Dharini Jeeva authored and GitHub committed 2024-06-03 13:38:42 -07:00
1 parent 9b403b3159
commit 6397cd80a6
22 files changed
+531 -405

No files matched your search

@@ -5,7 +5,7 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { click, render, select } from '@ember/test-helpers';
import { click, findAll, render, select } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import { setupIntl } from 'ember-intl/test-support';
@@ -55,7 +55,14 @@ module('Integration | Component | list-wrapper', function (hooks) {
await render(hbs`
<Form::Field::ListWrapper>
<:field as |F|>
<F.KeyValue @options={{this.options}}></F.KeyValue>
<F.KeyValue @options={{this.options}}>
<:key as |K|>
<K.text />
</:key>
<:value as |V|>
<V.text />
</:value>
</F.KeyValue>
</:field>
</Form::Field::ListWrapper>
`);
@@ -165,42 +172,56 @@ module('Integration | Component | list-wrapper', function (hooks) {
});
test('it renders multiple options with select and text input as key value pair', async function (assert) {
this.options = {
username_attribute: 'user',
password_attribute: 'pass',
};
this.options = [
{
key: 'username_attribute',
value: 'user',
},
{
key: 'password_attribute',
value: 'pass',
},
];
this.selectOptions = ['username_attribute', 'password_attribute'];
this.selectOptions = {
username_attribute: 'User Key',
password_attribute: 'Pass Key',
};
await render(hbs`
<Form::Field::ListWrapper>
<:field as |F|>
<F.SelectText @name="credential_mapping_overrides" @options={{this.options}} @removeDuplicates='true' @selectOptions={{this.selectOptions}}></F.SelectText>
<F.KeyValue @name="credential_mapping_overrides">
<:key as |K|>
<K.select @selectOptions={{this.selectOptions}}/>
</:key>
<:value as |V|>
<V.text/>
</:value>
</F.KeyValue>
</:field>
</Form::Field::ListWrapper>
`);
assert.dom('tbody tr').exists({ count: 2 });
assert
.dom('.list-wrapper-field tbody tr:last-child input')
.hasValue('pass');
assert
.dom('.list-wrapper-field tbody tr:last-child select')
.hasValue('password_attribute');
assert.dom('tbody tr').exists({ count: 1 });
});
test('it does not render new rows when the select option limit is reached by passing in @removeDuplicates', async function (assert) {
this.options = { username_attribute: 'user', password_attribute: 'pass' };
this.selectOptions = {
username_attribute: 'User Key',
password_attribute: 'Pass Key',
};
test('it does not render new rows when the select option limit is reached when showNewRow is passed', async function (assert) {
this.options = [
{ key: 'username_attribute', value: 'user' },
{ key: 'password_attribute', value: 'pass' },
];
this.selectOptions = ['username_attribute', 'password_attribute'];
this.showNewRow = () => false;
await render(hbs`
<Form::Field::ListWrapper>
<:field as |F|>
<F.SelectText @name="credential_mapping_overrides" @options={{this.options}} @selectOptions={{this.selectOptions}} @removeDuplicates='true'></F.SelectText>
<F.KeyValue @name="credential_mapping_overrides" @options={{this.options}} @showNewRow={{this.showNewRow}}>
<:key as |K|>
<K.select @selectOptions={{this.selectOptions}}/>
</:key>
<:value as |V|>
<V.text/>
</:value>
</F.KeyValue>
</:field>
</Form::Field::ListWrapper>
`);
@@ -208,20 +229,77 @@ module('Integration | Component | list-wrapper', function (hooks) {
assert.dom('tbody tr').exists({ count: 2 });
});
test('it does render unlimited new rows when @removeDuplicates is not passed', async function (assert) {
this.options = { username_attribute: 'user', password_attribute: 'pass' };
this.selectOptions = {
username_attribute: 'User Key',
password_attribute: 'Pass Key',
};
test('it does render unlimited new rows when @showNewRow is not passed', async function (assert) {
this.options = [
{
key: 'username_attribute',
value: 'user',
},
{
key: 'password_attribute',
value: 'pass',
},
];
this.selectOptions = ['username_attribute', 'password_attribute'];
await render(hbs`
<Form::Field::ListWrapper>
<:field as |F|>
<F.SelectText @name="credential_mapping_overrides" @options={{this.options}} @selectOptions={{this.selectOptions}}></F.SelectText>
<F.KeyValue @name="credential_mapping_overrides" @options={{this.options}} >
<:key as |K|>
<K.text/>
</:key>
<:value as |V|>
<V.text/>
</:value>
</F.KeyValue>
</:field>
</Form::Field::ListWrapper>
`);
assert.dom('tbody tr').exists({ count: 3 });
});
test('it renders `newKey` and `newValue` named blocks when passed instead of key and value blocks', async function (assert) {
this.options = ['option1', 'option2', 'option3'];
this.selectOptionsNewValue = [
'username_attribute_new_val',
'password_attribute_new_val',
];
this.selectOptionsNewKey = [
'username_attribute_new_key',
'password_attribute_new_key',
];
await render(hbs`
<Form::Field::ListWrapper>
<:field as |F|>
<F.KeyValue @name="credential_mapping_overrides">
<:key as |K|>
<K.select @selectOptions = {{this.options}}/>
</:key>
<:newKey as |N|>
<N.select @selectOptions= {{this.selectOptionsNewKey}}/>
</:newKey>
<:newValue as |S|>
<S.select @selectOptions= {{this.selectOptionsNewValue}}/>
</:newValue>
<:value as |V|>
<V.select @selectOptions = {{this.options}}/>
</:value>
</F.KeyValue>
</:field>
</Form::Field::ListWrapper>
`);
assert.strictEqual(
findAll('.list-wrapper-field tbody td:nth-of-type(1) select option')
.length,
3,
);
assert.strictEqual(
findAll('.list-wrapper-field tbody td:nth-of-type(2) select option')
.length,
3,
);
});
});