Files
Jarek RadoszandGitHub 7bce40879c DEV: Introduce a helper for handling events (#25433)
Instead of

```hbs
{{on "input" (action this.foo value="target.value")}}
{{on "input" (action (mut this.bar) value="target.value")}}
```

you can use:

```hbs
{{on "input" (with-event-value this.foo)}}
{{on "input" (with-event-value (fn (mut this.bar)))}}
```

or in gjs:

```gjs
import { fn } from "@ember/helper";
import { on } from "@ember/modifier";
import withEventValue from "discourse/helpers/with-event-value";
…
{{on "input" (withEventValue (fn (mut this.bar)))}}
```
2024-02-28 14:00:53 +01:00

54 lines
1.5 KiB
JavaScript

import { fillIn, render } from "@ember/test-helpers";
import { hbs } from "ember-cli-htmlbars";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
module("Integration | Component | char-counter", function (hooks) {
setupRenderingTest(hooks);
test("shows the number of characters", async function (assert) {
this.value = "Hello World";
this.max = 12;
await render(
hbs`<CharCounter @value={{this.value}} @max={{this.max}}></CharCounter>`
);
assert.dom(this.element).includesText("11/12");
});
test("updating value updates counter", async function (assert) {
this.max = 50;
await render(hbs`
<CharCounter
@value={{this.charCounterContent}}
@max={{this.max}}
>
<textarea {{on "input" (with-event-value (fn (mut this.charCounterContent)))}}></textarea>
</CharCounter>
`);
assert
.dom(this.element)
.includesText("/50", "initial value appears as expected");
await fillIn("textarea", "Hello World, this is a longer string");
assert
.dom(this.element)
.includesText("36/50", "updated value appears as expected");
});
test("exceeding max length", async function (assert) {
this.max = 10;
this.value = "Hello World";
await render(
hbs`<CharCounter @value={{this.value}} @max={{this.max}}></CharCounter>`
);
assert.dom(".char-counter.exceeded").exists("exceeded class is applied");
});
});