Skip to content

Commit

Permalink
Add defaults-to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ctjhoa committed Jul 29, 2019
1 parent 4539d26 commit 4ea35d7
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 15 deletions.
6 changes: 6 additions & 0 deletions addon/helpers/defaults-to.js
@@ -0,0 +1,6 @@
import { helper } from '@ember/component/helper';

export default helper(function defaultsTo(params/*, hash*/) {
let [param, defaultParam] = params;
return param === undefined ? defaultParam : param;
});
30 changes: 15 additions & 15 deletions addon/templates/components/content-loader.hbs
@@ -1,23 +1,23 @@
<ContentLoaderSvg
@animate={{or @animate true}}
@ariaLabel={{or @ariaLabel "Loading interface..."}}
@baseUrl={{or @baseUrl ""}}
@gradientRatio={{or @gradientRatio 2}}
@height={{or @height 130}}
@interval={{or @interval 0.25}}
@preserveAspectRatio={{or @preserveAspectRatio "none"}}
@primaryColor={{or @primaryColor "#f0f0f0"}}
@primaryOpacity={{or @primaryOpacity 1}}
@rtl={{or @rtl false}}
@secondaryColor={{or @secondaryColor "#e0e0e0"}}
@secondaryOpacity={{or @secondaryOpacity 1}}
@speed={{or @speed 2}}
@width={{or @width 400}}
@animate={{defaults-to @animate true}}
@ariaLabel={{defaults-to @ariaLabel "Loading interface..."}}
@baseUrl={{defaults-to @baseUrl ""}}
@gradientRatio={{defaults-to @gradientRatio 2}}
@height={{defaults-to @height 130}}
@interval={{defaults-to @interval 0.25}}
@preserveAspectRatio={{defaults-to @preserveAspectRatio "none"}}
@primaryColor={{defaults-to @primaryColor "#f0f0f0"}}
@primaryOpacity={{defaults-to @primaryOpacity 1}}
@rtl={{defaults-to @rtl false}}
@secondaryColor={{defaults-to @secondaryColor "#e0e0e0"}}
@secondaryOpacity={{defaults-to @secondaryOpacity 1}}
@speed={{defaults-to @speed 2}}
@width={{defaults-to @width 400}}
...attributes
>
{{#if (has-block)}}
{{yield}}
{{else}}
<rect x="0" y="0" rx="5" ry="5" width={{or @width 400}} height={{or @height 130}} />
<rect x="0" y="0" rx="5" ry="5" width={{defaults-to @width 400}} height={{defaults-to @height 130}} />
{{/if}}
</ContentLoaderSvg>
1 change: 1 addition & 0 deletions app/helpers/defaults-to.js
@@ -0,0 +1 @@
export { default, defaultsTo } from 'ember-content-loader/helpers/defaults-to';
49 changes: 49 additions & 0 deletions tests/integration/helpers/defaults-to-test.js
@@ -0,0 +1,49 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Helper | defaults-to', function(hooks) {
setupRenderingTest(hooks);

// Replace this with your real tests.
test('it defaults if undefined', async function(assert) {
this.set('inputValue', undefined);

await render(hbs`{{defaults-to inputValue '1234'}}`);

assert.equal(this.element.textContent.trim(), '1234');
});

test('it does not defaults if 0', async function(assert) {
this.set('inputValue', 0);

await render(hbs`{{defaults-to inputValue '1234'}}`);

assert.equal(this.element.textContent.trim(), '0');
});

test('it does not defaults if empty string', async function(assert) {
this.set('inputValue', '');

await render(hbs`{{defaults-to inputValue '1234'}}`);

assert.equal(this.element.textContent.trim(), '');
});

test('it does not defaults if null', async function(assert) {
this.set('inputValue', null);

await render(hbs`{{defaults-to inputValue '1234'}}`);

assert.equal(this.element.textContent.trim(), '');
});

test('it does not defaults if false', async function(assert) {
this.set('inputValue', false);

await render(hbs`{{defaults-to inputValue '1234'}}`);

assert.equal(this.element.textContent.trim(), 'false');
});
});

0 comments on commit 4ea35d7

Please sign in to comment.