Skip to content

Commit

Permalink
feat(form): improved syntax & tests for mandatory asterisk
Browse files Browse the repository at this point in the history
  • Loading branch information
tstoppe89 committed Jan 6, 2025
1 parent 4b2b973 commit ee684d3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
4 changes: 1 addition & 3 deletions packages/form/addon/components/cf-field/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export default class CfFieldLabelComponent extends Component {
const { USE_MANDATORY_ASTERISK = false } =
this.config["ember-caluma"] || {};

return [true, false].includes(this.args?.useMandatoryAsterisk)
? this.args?.useMandatoryAsterisk
: USE_MANDATORY_ASTERISK;
return this.args?.useMandatoryAsterisk ?? USE_MANDATORY_ASTERISK;
}
}
36 changes: 22 additions & 14 deletions packages/form/tests/integration/components/cf-field/label-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ module("Integration | Component | cf-field/label", function (hooks) {
{ raw, owner: this.owner },
);
this.set("field", document.fields[0]);
this.set("useMandatoryAsterisk", false);
});

test("it renders", async function (assert) {
Expand All @@ -49,9 +48,7 @@ module("Integration | Component | cf-field/label", function (hooks) {
test("it marks optional fields as such", async function (assert) {
assert.expect(2);

await render(
hbs`<CfField::Label @field={{this.field}} @useMandatoryAsterisk={{this.useMandatoryAsterisk}} />`,
);
await render(hbs`<CfField::Label @field={{this.field}} />`);

assert.dom("label").hasText("Test");

Expand All @@ -60,27 +57,38 @@ module("Integration | Component | cf-field/label", function (hooks) {
assert.dom("label").hasText("Test (Optional)");
});

test("it marks mandatory fields as such if useMandatoryAsterisk is configured", async function (assert) {
assert.expect(1);
test("it marks mandatory fields as such if useMandatoryAsterisk is configured in attribute", async function (assert) {
assert.expect(2);
this.set("useMandatoryAsterisk", true);

await render(
hbs`<CfField::Label @field={{this.field}} @useMandatoryAsterisk={{this.useMandatoryAsterisk}} />`,
hbs`<CfField::Label
@field={{this.field}}
@useMandatoryAsterisk={{this.useMandatoryAsterisk}}
/>`,
);
this.set("field.question.raw.isRequired", "true");

this.set("field.question.raw.isRequired", "false");
assert.dom("label").hasText("Test");

this.set("field.question.raw.isRequired", "true");
assert.dom("label").hasText("Test *");
});

test("it doesnt mark optional fields if useMandatoryAsterisk is configured true", async function (assert) {
assert.expect(1);
test("it marks mandatory fields as such if useMandatoryAsterisk is globally configured", async function (assert) {
this.owner.resolveRegistration("config:environment")["ember-caluma"] = {
USE_MANDATORY_ASTERISK: true,
};

assert.expect(2);
this.set("useMandatoryAsterisk", true);

await render(
hbs`<CfField::Label @field={{this.field}} @useMandatoryAsterisk={{this.useMandatoryAsterisk}} />`,
);
this.set("field.question.raw.isRequired", "false");
await render(hbs`<CfField::Label @field={{this.field}} />`);

this.set("field.question.raw.isRequired", "false");
assert.dom("label").hasText("Test");

this.set("field.question.raw.isRequired", "true");
assert.dom("label").hasText("Test *");
});
});

0 comments on commit ee684d3

Please sign in to comment.