From 64f34dbf59636ba4820f17624671915d9b0b33c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Har=C4=99=C5=BClak?= Date: Sat, 25 Aug 2018 12:33:26 +0200 Subject: [PATCH] Add is-present helper --- README.md | 3 +- addon/helpers/is-present.js | 6 ++++ app/helpers/is-present.js | 1 + tests/unit/helpers/is-present-test.js | 48 +++++++++++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 addon/helpers/is-present.js create mode 100644 app/helpers/is-present.js create mode 100644 tests/unit/helpers/is-present-test.js diff --git a/README.md b/README.md index f027a52..26038a8 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ gte | `if (a >= b)` | `{{if (gte a b)}}` lt | `if (a < b)` | `{{if (lt a b)}}` | No | lte | `if (a <= b)` | `{{if (lte a b)}}` | No | is-array | `if (Ember.isArray(a))` | `{{if (is-array a)}}` | Yes | -is-empty | `if (Ember.isEmpty(a))` | `{{if (is-empty a)}}` | No | +is-empty | `if (Ember.isEmpty(a))` | `{{if (is-empty a)}}` | No | is-equal | `if (Ember.isEqual(a, b))` | `{{if (is-equal a b)}}` | No | +is-present | `if (Ember.isPresent(a))` | `{{if (is-present a)}}` | No | ### API diff --git a/addon/helpers/is-present.js b/addon/helpers/is-present.js new file mode 100644 index 0000000..b163239 --- /dev/null +++ b/addon/helpers/is-present.js @@ -0,0 +1,6 @@ +import { helper } from '@ember/component/helper'; +import { isPresent } from '@ember/utils'; + +export default helper(function([obj]) { + return isPresent(obj) +}) diff --git a/app/helpers/is-present.js b/app/helpers/is-present.js new file mode 100644 index 0000000..d54955b --- /dev/null +++ b/app/helpers/is-present.js @@ -0,0 +1 @@ +export { default } from 'ember-truth-helpers/helpers/is-present'; diff --git a/tests/unit/helpers/is-present-test.js b/tests/unit/helpers/is-present-test.js new file mode 100644 index 0000000..02c75da --- /dev/null +++ b/tests/unit/helpers/is-present-test.js @@ -0,0 +1,48 @@ +import { module, test } from 'qunit'; +import { setupRenderingTest } from 'ember-qunit'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + +module('helper:is-present', function(hooks) { + setupRenderingTest(hooks); + + test('undefined/null values', async function(assert) { + this.set('thisIsUndefined', undefined) + this.set('thisIsNull', null) + this.set('thisIsNotNull', new Date()) + await render(hbs("[{{is-present thisIsUndefined}}] [{{is-present thisIsNull}}] [{{is-present thisIsNotNull}}]")); + + assert.equal(this.$().text(), '[false] [false] [true]', 'value should be "[false] [false] [true]"'); + }); + + test('boolean values', async function(assert) { + await render(hbs("[{{is-present true}}] [{{is-present false}}]")); + + assert.equal(this.$().text(), '[true] [true]', 'value should be "[true] [true]"'); + }); + + test('objects', async function(assert) { + this.set('presentObject', {}) + this.set('notpresentObject', { some: 'object' }) + await render(hbs("[{{is-present presentObject}}] [{{is-present notpresentObject}}]")); + + assert.equal(this.$().text(), '[true] [true]', 'value should be "[true] [true]"'); + }); + + test('arrays', async function(assert) { + this.set('presentArray', []) + this.set('notpresentArray', [ 'a', 8, {} ]) + await render(hbs("[{{is-present presentArray}}] [{{is-present notpresentArray}}]")); + + assert.equal(this.$().text(), '[false] [true]', 'value should be "[false] [true]"'); + }); + + test('strings', async function(assert) { + this.set('presentString', '') + this.set('whitespaceString', ' ') + this.set('notpresentString', 'full of text') + await render(hbs("[{{is-present presentString}}] [{{is-present whitespaceString}}] [{{is-present notpresentString}}]")); + + assert.equal(this.$().text(), '[false] [false] [true]', 'value should be "[false] [false] [true]"'); + }); +});