Skip to content
This repository has been archived by the owner on Jul 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #28 from truecoach/task/compact-flatten-models
Browse files Browse the repository at this point in the history
Compact and Flatten Positional Params
  • Loading branch information
flexyford authored Oct 31, 2019
2 parents 93d3652 + 32b1ff6 commit 62dc1d9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
11 changes: 8 additions & 3 deletions addon/components/fit-form/component.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import Component from '@ember/component';
import layout from './template';

import { A as emberArray, makeArray } from '@ember/array';
import { A, makeArray } from '@ember/array';
import { computed } from '@ember/object';
import { inject } from '@ember/service';

/**
* Wraps a native `<form>` element and provides abstractions for working with models and model validations.
*/
function flat(arr) { return [].concat(...arr) }
function compact(arr) { return arr.filter(v => v != null) }
function flattenAndCompact(o) {
return A(compact(flat(makeArray(o))));
}

const FitFormComponent = Component.extend({
fitFormService: inject('fit-form'),
Expand Down Expand Up @@ -63,7 +68,7 @@ const FitFormComponent = Component.extend({

formObject: computed('models.[]', 'adapter', function() {
const Adapter = this.get('fitFormService').lookupAdapter(this.get('adapter'));
const emberModelArray = emberArray(makeArray(this.get('models')));
const modelArray = flattenAndCompact(this.get('models'));

const hooks = [
'oncancel', 'onerror', 'oninvalid', 'onsubmit', 'onsuccess', 'onvalidate'
Expand All @@ -74,7 +79,7 @@ const FitFormComponent = Component.extend({
}, {});

const adapter = Adapter.create({
models: emberModelArray,
models: modelArray,
...hooks
});

Expand Down
52 changes: 52 additions & 0 deletions tests/integration/components/fit-form/component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,58 @@ module('Integration | Component | fit-form', function(hooks) {
assert.dom('form').hasText('template block text');
});

test('a form with models', async function(assert) {
assert.expect(5);

const changeset = new Changeset({});

this.setProperties({
changeset,
changesets: [ changeset, changeset ]
});

await render(hbs`
{{#fit-form undefined as |form|}}
{{#each form.models as |m i|}}
index-{{i}}
{{/each}}
{{/fit-form}}
`);

assert.dom('form').doesNotIncludeText('index-0', 'renders no models with an undefined param');

await render(hbs`
{{#fit-form changeset as |form|}}
{{#each form.models as |m i|}}
index-{{i}}
{{/each}}
{{/fit-form}}
`);

assert.dom('form').includesText('index-0', 'accepts an object as a param');
assert.dom('form').doesNotIncludeText('index-1');

await render(hbs`
{{#fit-form changeset changeset as |form|}}
{{#each form.models as |m i|}}
index-{{i}}
{{/each}}
{{/fit-form}}
`);

assert.dom('form').includesText('index-1', 'accepts many objects as params');

await render(hbs`
{{#fit-form changesets as |form|}}
{{#each form.models as |m i|}}
index-{{i}}
{{/each}}
{{/fit-form}}
`);

assert.dom('form').includesText('index-1', 'accepts an array as a param');
});

test('Submitting a form', async function(assert) {
assert.expect(6);

Expand Down

0 comments on commit 62dc1d9

Please sign in to comment.