From 1a2bf5265678a385bdc973cfdc1f8b3f28c3e304 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 15 Sep 2022 14:20:37 +0200 Subject: [PATCH] DEV: adds includes helper to templates (#18259) Usage : ``` {{#if (includes fooArray foo)}} ``` ``` {{#if (includes fooString foo)}} ``` --- .../integration/helpers/includes-test.js | 43 +++++++++++++++++++ .../truth-helpers/addon/helpers/includes.js | 7 +++ 2 files changed, 50 insertions(+) create mode 100644 app/assets/javascripts/discourse/tests/addons/truth-helpers/integration/helpers/includes-test.js create mode 100644 app/assets/javascripts/truth-helpers/addon/helpers/includes.js diff --git a/app/assets/javascripts/discourse/tests/addons/truth-helpers/integration/helpers/includes-test.js b/app/assets/javascripts/discourse/tests/addons/truth-helpers/integration/helpers/includes-test.js new file mode 100644 index 00000000000..adf2000528d --- /dev/null +++ b/app/assets/javascripts/discourse/tests/addons/truth-helpers/integration/helpers/includes-test.js @@ -0,0 +1,43 @@ +import { assert, module, test } from "qunit"; +import { setupRenderingTest } from "discourse/tests/helpers/component-test"; +import { render } from "@ember/test-helpers"; +import { hbs } from "ember-cli-htmlbars"; +import { exists } from "discourse/tests/helpers/qunit-helpers"; + +module("Addons | truth-helpers | Integration | includes", function (hooks) { + setupRenderingTest(hooks); + + test("when using an array", async function () { + this.foo = [1]; + this.bar = 1; + await render( + hbs`{{#if (includes foo bar)}}{{/if}}` + ); + + assert.ok(exists(".test"), "it returns true when element is found"); + + this.bar = 2; + await render( + hbs`{{#if (includes foo bar)}}{{/if}}` + ); + + assert.notOk(exists(".test"), "it returns false when element is not found"); + }); + + test("when using a string", async function () { + this.foo = "foo"; + this.bar = "f"; + await render( + hbs`{{#if (includes foo bar)}}{{/if}}` + ); + + assert.ok(exists(".test"), "it returns true when element is found"); + + this.bar = "b"; + await render( + hbs`{{#if (includes foo bar)}}{{/if}}` + ); + + assert.notOk(exists(".test"), "it returns false when element is not found"); + }); +}); diff --git a/app/assets/javascripts/truth-helpers/addon/helpers/includes.js b/app/assets/javascripts/truth-helpers/addon/helpers/includes.js new file mode 100644 index 00000000000..d2b0a057c7c --- /dev/null +++ b/app/assets/javascripts/truth-helpers/addon/helpers/includes.js @@ -0,0 +1,7 @@ +import Helper from "@ember/component/helper"; + +export function includes(params) { + return params[0].includes(params[1]); +} + +export default Helper.helper(includes);